HDU 1402 A * B Problem Plus [FFT]【数论】
2017-01-13 23:52
531 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402
———————————————————————————.
A * B Problem Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19099 Accepted Submission(s): 4406
Problem Description
Calculate A * B.
Input
Each line will contain two integers A and B. Process to end of file.
Note: the length of each integer will not exceed 50000.
Output
For each case, output A * B in one line.
Sample Input
1
2
1000
2
Sample Output
2
2000
Author
DOOM III
———————————————————————————.
就是个FFT入门题 。
因为题目给的字符串长度达到了50000,如果普通的大数乘法复杂度为O(lenA∗lenB)必定会超时.
FFT其实就是一个快速求取卷积的过程.
而一个乘法计算其实也就是一个卷积
对于个数a0a1a2a3...an
也就是A(x=10)=a0∗x0+a1∗x1+a2∗x2+a3∗x3+...an∗xn
所以这个题就可以用FFT来计算了。。
复杂度能降到O(Nlog2N)
附本题代码
—————————————————————-.
———————————————————————————.
A * B Problem Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19099 Accepted Submission(s): 4406
Problem Description
Calculate A * B.
Input
Each line will contain two integers A and B. Process to end of file.
Note: the length of each integer will not exceed 50000.
Output
For each case, output A * B in one line.
Sample Input
1
2
1000
2
Sample Output
2
2000
Author
DOOM III
———————————————————————————.
就是个FFT入门题 。
因为题目给的字符串长度达到了50000,如果普通的大数乘法复杂度为O(lenA∗lenB)必定会超时.
FFT其实就是一个快速求取卷积的过程.
而一个乘法计算其实也就是一个卷积
对于个数a0a1a2a3...an
也就是A(x=10)=a0∗x0+a1∗x1+a2∗x2+a3∗x3+...an∗xn
所以这个题就可以用FFT来计算了。。
复杂度能降到O(Nlog2N)
附本题代码
—————————————————————-.
#include <bits/stdc++.h> using namespace std; const int N = 50000+5; const double PI = acos(-1.0); /***********************************************************************/ struct Complex{ double real, image; Complex(double _real, double _image){ real = _real; image = _image; } Complex(){} }; Complex *AA = new Complex[N<<2],*BB = new Complex[N<<2]; Complex operator + (const Complex &c1, const Complex &c2){ return Complex(c1.real + c2.real, c1.image + c2.image); } Complex operator - (const Complex &c1, const Complex &c2){ return Complex(c1.real - c2.real, c1.image - c2.image); } Complex operator * (const Complex &c1, const Complex &c2){ return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real); } int rev(int id, int len){ int ret = 0; for(int i = 0; (1 << i) < len; i++){ ret <<= 1; if(id & (1 << i)) ret |= 1; } return ret; } Complex A[N<<2]; void FFT(Complex *a, int len, int DFT) { for(int i = 0; i < len; i++) A[rev(i, len)] = a[i]; for(int s = 1; (1 << s) <= len; s++) { int m = (1 << s); Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m)); for(int k = 0; k < len; k += m) { Complex w = Complex(1, 0); for(int j = 0; j < (m >> 1); j++) { Complex t = w*A[k + j + (m >> 1)]; Complex u = A[k + j]; A[k + j] = u + t; A[k + j + (m >> 1)] = u - t; w = w*wm; } } } if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len; for(int i = 0; i < len; i++) a[i] = A[i]; return; } char a ,b ; int ans[N<<2]; int main(){ while(~scanf("%s",a)){ scanf("%s",b); int la = strlen(a); int lb = strlen(b); int sa,sb; sa=sb=0; while((1<<sa)<la) sa++; while((1<<sb)<lb) sb++; int len = (1<<(max(sa,sb)+1)); for(int i=0;i<len;i++){ AA[i]=Complex(((i<la)?(a[la-i-1]-'0'):0),0); BB[i]=Complex(((i<lb)?(b[lb-i-1]-'0'):0),0); } FFT(AA,len,1); FFT(BB,len,1); for(int i=0;i<len;i++) AA[i]=AA[i]*BB[i],ans[i]=0; FFT(AA,len,-1); for(int i=0;i<len ;i++) ans[i]+=(int)(AA[i].real+0.5); for(int i=0;i<len-1;i++) ans[i+1]+=ans[i]/10,ans[i]%=10; bool flag = false; for(int i=len-1;i>=0;--i){ if(ans[i]) printf("%d",ans[i]),flag=true; else if(flag||0==i) printf("0"); } puts(""); } return 0; }
相关文章推荐
- 【HDU】1402 A * B Problem Plus 【FFT】
- HDU 1402(A * B Problem Plus-FFT速度测试)
- 【HDU】1402 A * B Problem Plus 【FFT】
- [HDU]1402 A * B Problem Plus [FFT]
- hdu 1402 A * B Problem Plus[【FFT】
- hdu 1402 A * B Problem Plus 【快速傅里叶变换FFT】
- HDU 1402 A * B Problem Plus FFT
- HDU 1402 A * B Problem Plus FFT
- <模板> Hdu 1402 A * B Problem Plus 大数乘法
- HDU 1042 A * B Problem Plus [FFT]
- hdu 1402 A * B Problem Plus 快速傅里叶变换
- HDU 1402 A * B Problem Plus
- HDU 1402 A * B Problem Plus 高精度乘法
- hdu 1402 A * B Problem Plus 快速傅里叶变换/FFT
- 杭电 HDU 1402 A * B Problem Plus JavaJ大数
- hdu 1402 A * B Problem Plus
- HDOJ 1402 A * B Problem Plus -- FFT
- hdu 1402 A * B Problem Plus || poj 2389 Bull Math
- hdu 1402 A * B Problem Plus 快速傅里叶变换
- hdu 1402 A * B Problem Plus(fft)