P1919 【模板】A*B Problem升级版(FFT快速傅里叶)

题目大意:

两个大整数相乘(位数60000)

思路:

快速傅立叶变换!!
给一个大佬blog:https://blog.csdn.net/zxn0803/article/details/51361111

程序:

#include
#include
#include
#include
#include
#include
#include
#define pi acos(-1)
#define N 131072

using namespace std;
typedef complex<double> com;

com a[N],b[N];
int sum[N],rev[N],n,lg,m;
char c[N];

void bitrev(){
    for (int i=0;i>1]>>1)|((i&1)<<(lg-1));    
}

void fft(com *a,int f){
    for (int i=0;iif (ifor (int i=1;i1){
        com wn(cos(pi/i),f*sin(pi/i));
        for (int j=0;j1)){
            com w(1,0);
            for (int k=0;kif (f==-1){
        for (int i=0;iint main(){
    scanf("%d",&n);
    scanf("%s",c);
    for (int i=0;i1]=c[i]-'0';
    scanf("%s",c);
    for (int i=0;i1]=c[i]-'0';
    m=n*2-2;
    for (n=1;n<=m;n*=2,lg++);
    bitrev();
    fft(a,1);
    fft(b,1);
    for (int i=0;i1);
    for (int i=0;i<=n;i++) sum[i]=(int)(a[i].real()+0.1);
    for (int i=0;i<=m;i++)
    if (sum[i]>=10){
        sum[i+1]+=sum[i]/10; sum[i]%=10;
        if (i==m) m++;
    }
    while (sum[m]==0) m--;
    for (int i=m;i>=0;i--) printf("%d",sum[i]);
    return 0;
}

你可能感兴趣的:(快速傅立叶变化)