实现了两个长度为n的大数相乘。
#include <cstdio>
#include <cmath>
#include <complex>
using namespace std;
#define pi acos(-1) typedef complex<double> C;
const int N = ;
char s[N],t[N];
int n,m,l,r[N],c[N];
C a[N],b[N]; void fft(C *a, int f) {
for(int i = ; i < n; i++) if(r[i] > i) swap(a[i], a[r[i]]);
for(int i = ; i < n; i <<= ) {
C wn(cos(pi/i), f*sin(pi/i));
for(int j = ; j < n; j += i<<) {
C w = ;
for(int k = ; k < i; k++, w *= wn) {
C x = a[j+k], y = w*a[j+k+i];
a[j+k] = x+y, a[j+k+i] = x-y;
}
}
}
} int main() {
scanf("%d%s%s", &m, s, t);
for(int i = ; i < m; i++) a[i] = s[m-i-]-'', b[i] = t[m-i-]-'';
for(n = , m <<= ; n < m; n <<= ) l++;
for(int i = ; i < n; i++) r[i] = (r[i>>]>>)|((i&)<<(l-));
fft(a, ), fft(b, );
for(int i = ; i < n; i++) a[i] *= b[i];
fft(a, -);
for(int i = ; i < n; i++) a[i] /= n;
for(int i = ; i < m; i++) c[i] = (int)(a[i].real()+0.1);
for(int i = ; i < m; i++) if(c[i] >= ) {
c[i+] += c[i]/, c[i] %= ;
} else if(!c[i] && i == m-) m--;
for(int i = m-; ~i; i--) printf("%d", c[i]);
return ;
}