ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

3123. 高精度乘法II

2022-07-23 00:34:41  阅读:134  来源: 互联网

标签:10 const int 3123 II define return cp 乘法


题目链接

3123. 高精度乘法II

给定两个正整数 \(A\) 和 \(B\),请你计算 \(A \times B\) 的值。

输入格式

共两行,第一行包含整数 \(A\),第二行包含整数 \(B\)。

输出格式

共一行,包含 \(A \times B\) 的值。

数据范围

\(1 \le A与B的长度 \le 10^5\)。

输入样例:

2
3

输出样例:

6

解题思路

fft

设 \(A\) 由高位到低位的值分别为 \(a_0,a_1,\dots,a_{n-1}\),构造一个 \(n-1\) 次多项式 \(f(x)=a_0+a_1x+a_2x^2+\dots +a_{n-1}x^{n-1}\),同理由 \(B\) 构造的多项式 \(g(x)=b_0+b_1x+b_2x^2+\dots +b_{m-1}x^{m-1}\),而 \(A=f(10),B=g(10)\),要求 \(A\times B=f(10)\times g(10)\),而 \(fft\) 可求得 \(h(x)=f(x)\times g(x)\),最后即求 \(h(10)\)。注意求解的 \(h(x)\) 的系数并不是最后的答案,其系数也可能不是一位数

  • 时间复杂度:\(O(nlogn)\)

代码

// Problem: 高精度乘法II
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/3126/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=3e5+5;
const double pi=acos(-1);
int n,m,bit,tot,rev[N],res[N];
char A[N],B[N];
struct cp
{
	double x,y;
	cp operator+(const cp &o)const
	{
		return {x+o.x,y+o.y};
	}
	cp operator-(const cp &o)const
	{
		return {x-o.x,y-o.y};
	}
	cp operator*(const cp &o)const
	{
		return {x*o.x-y*o.y,x*o.y+y*o.x};
	}
}a[N],b[N];
void fft(cp a[],int inv)
{
	for(int i=0;i<tot;i++)
		if(i<rev[i])swap(a[i],a[rev[i]]);
	for(int mid=1;mid<tot;mid<<=1)
	{
		cp w1={cos(pi/mid),inv*sin(pi/mid)};
		for(int i=0;i<tot;i+=mid<<1)
		{
			cp wk={1,0};
			for(int j=0;j<mid;j++,wk=wk*w1)
			{
				cp x=a[i+j],y=a[i+j+mid];
				a[i+j]=x+wk*y,a[i+j+mid]=x-wk*y;
			}
		}
	}
}
int main()
{
    help;
    cin>>A>>B;
    n=strlen(A),m=strlen(B);
    n--,m--;
    for(int i=0;i<=n;i++)a[i].x=A[n-i]-'0';
    for(int i=0;i<=m;i++)b[i].x=B[m-i]-'0';
    while((1<<bit)<n+m+1)bit++;
    tot=1<<bit;
    for(int i=0;i<tot;i++)rev[i]=rev[i>>1]>>1|((i&1)<<(bit-1));
    fft(a,1),fft(b,1);
    for(int i=0;i<tot;i++)a[i]=a[i]*b[i];
    fft(a,-1);
    int t=0,k=0;
    for(int i=0;i<tot||t;i++)
    {
    	t+=a[i].x/tot+0.5;
    	res[k++]=t%10;
    	t/=10;
    }
    while(k>1&&!res[k-1])k--;
    for(int i=k-1;~i;i--)cout<<res[i];
    return 0;
}

标签:10,const,int,3123,II,define,return,cp,乘法
来源: https://www.cnblogs.com/zyyun/p/16508328.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有