1028 大数乘法 V2
程序员文章站
2022-05-22 19:20:15
...
FFT简单应用
#include<iostream>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
typedef complex<double> cd;
const int MAXN = 2094153;
const double PI = acos(-1);
cd a[MAXN], b[MAXN];
int rev[MAXN];
void getrev(int bit){
for(int i = 0; i < (1 << bit); ++i){
rev[i] = (rev[i>>1]>>1)|((i&1)<<(bit - 1));
}
}
void fft(cd* a, int n, int dft){
for(int i = 0; i < n; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
for(int step = 1; step < n; step<<=1){
cd wn = exp(cd(0, PI * dft / step));
for(int j = 0; j < n; j += step<<1){
cd wnk(1, 0);
for(int k = j; k < j + step; ++k){
cd x = a[k];
cd y = wnk * a[k + step];
a[k] = x + y;
a[k + step] = x -y;
wnk *= wn;
}
}
}
if(dft == -1) for(int i = 0; i < n; ++i) a[i] /= n;
}
char s1[MAXN], s2[MAXN];
int output[MAXN];
int main(){
scanf("%s%s", s1, s2);
int l1 = strlen(s1), l2 = strlen(s2);
int bit = 1, s = 2;
for(bit = 1; (1<<bit) < l1 + l2 - 1; ++bit) s <<= 1;
for(int i = 0; i < l1; ++i) a[i] = (double)(s1[l1 - i - 1] - '0');
for(int i = 0; i < l2; ++i) b[i] = (double)(s2[l2 - i - 1] - '0');
getrev(bit); fft(a, s, 1); fft(b, s, 1);
for(int i = 0; i < s; ++i) a[i] *= b[i];
fft(a, s, -1);
for(int i = 0; i < s; ++i){
output[i] += (int)(a[i].real() + 0.5);
output[i + 1] += output[i] / 10;
output[i] %= 10;
}
int i;
for(i = s; !output[i] && i >= 0; --i);
if(i == -1) printf("0");
for(; i >= 0; --i) printf("%d", output[i]);
printf("\n");
return 0;
}
上一篇: 多项式乘法与FFT——学习笔记
下一篇: 关于信号的频谱分析