hdu 1402 A * B Problem Plus(FFT)
Posted jpphy0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1402 A * B Problem Plus(FFT)相关的知识,希望对你有一定的参考价值。
目录
问题
hdu 1402 A * B Problem Plus - http://acm.hdu.edu.cn/showproblem.php?pid=1402
分析
- fft
代码
#include<bits/stdc++.h>
using namespace std;
typedef complex<double> cd;
typedef long long ll;
const double DFT = 2.0, IDFT = -2.0, PI = acos(-1);
const int MXN = 5e4+10, MXL = MXN << 2;
cd p1[MXL], p2[MXL];
int n, r[MXL], ans[MXN<<1];
char a[MXN], b[MXN];
void fft(cd p[], int len, double mode)
for(int i = 0; i < len; ++i) if(i < r[i]) swap(p[i], p[r[i]]);
for(int i = 2; i <= len; i <<= 1)
cd wn(cos(mode*PI/i), sin(mode*PI/i));
for(int j = 0; j < len; j += i)
cd w(1.0, 0.0);
for(int k = j; k < j+(i>>1); ++k, w *= wn)
cd l = p[k], r = w*p[k+(i>>1)];
p[k] = l + r, p[k+(i>>1)] = l - r;
if(mode == DFT) return;
for(int i = 0; i <= len; ++i) p[i] /= len;
int getnum(char c[], cd p[])
int len = strlen(c)-1;
for(int i = 0; i <= len; ++i) p[len-i] = c[i] - '0';
return len;
int main()
int t, lim, bit;
while(scanf("%s%s", a, b) == 2)
for(int i = 0; i < MXL; ++i) p1[i] = p2[i] = 0;
lim = getnum(a, p1), lim += getnum(b, p2);
bit = 1;
while(lim >>= 1) ++bit;
lim = 1 << bit;
for(int i = 0; i < lim; ++i) r[i] = (r[i>>1]>>1)|((i&1)<<(bit-1));
fft(p1, lim, DFT), fft(p2, lim, DFT);
for(int i = 0; i < lim; ++i) p1[i] *= p2[i];
fft(p1, lim, IDFT);
for(int i = 0; i < (MXN<<1); ++i) ans[i] = (int)(p1[i].real()+0.5);
for(int i = 0; i < (MXN<<1)-1; ++i) ans[i+1] += ans[i]/10, ans[i] %= 10;
int i = (MXN<<1)-1;
while(ans[i] == 0 && i >= 1) --i;
while(i >= 0) printf("%d", ans[i--]);
printf("\\n");
return 0;
以上是关于hdu 1402 A * B Problem Plus(FFT)的主要内容,如果未能解决你的问题,请参考以下文章
hdu 1402 A * B Problem Plus(FFT)
A * B Problem Plus HDU - 1402 (FFT)
HDU 1402 A * B Problem Plus FFT
A * B Problem Plus HDU - 1402 (FFT)