FFT入门题hdu1402A * B Problem Plus 挖坑。

讲解:https://www.cnblogs.com/RabbitHu/p/FFT.html
待填坑。。。
HDU1402

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int AX = 200010;
const double PI = acos(-1);
typedef complex <double> cp;
char sa[AX], sb[AX];
int n , lena, lenb, res[AX];
cp a[AX], b[AX], omg[AX], inv[AX];
void init(){
    for(int i = 0; i < n; i++){
        omg[i] = cp(cos(2 * PI * i / n), sin(2 * PI * i / n));
        inv[i] = conj(omg[i]);
    }
}
void fft(cp *a, cp *omg){
    int lim = 0;
    while((1 << lim) < n) lim++;
    for(int i = 0; i < n; i++){
        int t = 0;
        for(int j = 0; j < lim; j++)
            if((i >> j) & 1) t |= (1 << (lim - j - 1));
        if(i < t) swap(a[i], a[t]); 
    }
    for(int l = 2; l <= n; l *= 2){
        int m = l / 2;
    for(cp *p = a; p != a + n; p += l)
        for(int i = 0; i < m; i++){
            cp t = omg[n / l * i] * p[i + m];
            p[i + m] = p[i] - t;
            p[i] += t;
        }
    }
}

int main(){
    while( ~scanf("%s%s", sa, sb) ){
        n = 1 ;
        int lena = strlen(sa), lenb = strlen(sb);
        int len = lena + lenb;
        while(n < len) n <<= 1;
        init();
        memset( a , 0 , sizeof(a) );
        memset( b , 0 , sizeof(b) );
        for(int i = 0; i < lena; i++)
            a[i].real(sa[lena - 1 - i] - '0');
        for(int i = 0; i < lenb; i++)
            b[i].real(sb[lenb - 1 - i] - '0');
        memset( res , 0 , sizeof(res) );
        fft( a, omg );
        fft( b, omg );
        for(int i = 0; i < n; i++)
            a[i] *= b[i];
        fft( a , inv );
        for( int i = 0; i < n; i++ ){
            res[i] += floor(a[i].real() / n + 0.5);
            res[i + 1] += res[i] / 10;
            res[i] %= 10;
        }   
        while( !res[len] ) len--; 
        for(int i = len ; i >= 0; i--)
            printf("%d",res[i]);
        if( len < 0 ) printf("0"); 
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/FrankAx/article/details/81268259
今日推荐