HDU 1402 A * B Problem Plus ( FFT )

Posted rubbishes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1402 A * B Problem Plus ( FFT )相关的知识,希望对你有一定的参考价值。

题意 : 求两个大数相乘的结果

 

分析 :

可以将数拆成多项式的形式

例如 12345

(1 * x^4) + (2 * x^3) + (3 * x^2) + (4 * x^1) + (5 * x^0)

其中 x == 10

那么两个数的相乘就可以变成两个多项式的相乘

可以利用 FFT 来优化

注意最后的结果每个系数可能是两位数(不可能超过两位)

所以需要手动进位、具体看代码

 

技术分享图片
#include<bits/stdc++.h>
using namespace std;

#define L(x) (1 << (x))
const double PI = acos(-1.0);
const int maxn = (1<<17) + (int)1e3;
double ax[maxn], ay[maxn], bx[maxn], by[maxn];
int revv(int x, int bits)
{
    int ret = 0;
    for (int i = 0; i < bits; i++){
        ret <<= 1;
        ret |= x & 1;
        x >>= 1;
    }
    return ret;
}

void fft(double * a, double * b, int n, bool rev)
{
    int bits = 0;
    while (1 << bits < n) ++bits;
    for (int i = 0; i < n; i++){
        int j = revv(i, bits);
        if (i < j) swap(a[i], a[j]), swap(b[i], b[j]);
    }

    for (int len = 2; len <= n; len <<= 1){
        int half = len >> 1;
        double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len);
        if (rev) wmy = -wmy;
        for (int i = 0; i < n; i += len){
            double wx = 1, wy = 0;
            for (int j = 0; j < half; j++){
                double cx = a[i + j], cy = b[i + j];
                double dx = a[i + j + half], dy = b[i + j + half];
                double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx;
                a[i + j] = cx + ex, b[i + j] = cy + ey;
                a[i + j + half] = cx - ex, b[i + j + half] = cy - ey;
                double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx;
                wx = wnx, wy = wny;
            }
        }
    }
    if (rev){
        for (int i = 0; i < n; i++)
            a[i] /= n, b[i] /= n;
    }
}

int Convolution(int a[],int na,int b[],int nb,int ans[]) //两个数组求卷积,有时ans数组要开成long long
{
    int len = max(na, nb), ln;
    for(ln=0; L(ln)<len; ++ln);
    len=L(++ln);
    for (int i = 0; i < len ; ++i){
        if (i >= na) ax[i] = 0, ay[i] =0;
        else ax[i] = a[i], ay[i] = 0;
    }
    fft(ax, ay, len, 0);
    for (int i = 0; i < len; ++i){
        if (i >= nb) bx[i] = 0, by[i] = 0;
        else bx[i] = b[i], by[i] = 0;
    }
    fft(bx, by, len, 0);
    for (int i = 0; i < len; ++i){
        double cx = ax[i] * bx[i] - ay[i] * by[i];
        double cy = ax[i] * by[i] + ay[i] * bx[i];
        ax[i] = cx, ay[i] = cy;
    }
    fft(ax, ay, len, 1);
    for (int i = 0; i < len; ++i)
        ans[i] = (int)(ax[i] + 0.5);
    return len;
}

char strA[maxn], strB[maxn];
int A[maxn], B[maxn], C[maxn];

int main(void)
{
    while(~scanf("%s %s", strA, strB)){
        int lenA = strlen(strA);
        int lenB = strlen(strB);
        for(int i=0; i<lenA; i++) A[i] = strA[lenA-i-1] - 0;
        for(int i=0; i<lenB; i++) B[i] = strB[lenB-i-1] - 0;

        int lenC = Convolution(A, lenA, B, lenB, C);

        for(int i=0; i<lenC; i++){
            C[i+1] += C[i] / 10;
            C[i] %= 10;
        }

        lenC--;
        while(lenC > 0 && C[lenC] <= 0) lenC--;

        for(int i=lenC; i>=0; i--) putchar(C[i] + 0);

        puts("");
    }
    return 0;
}
View Code

 

以上是关于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)

FFT(快速傅立叶变换):HDU 1402 A * B Problem Plus

HDU - 1402 A * B Problem Plus FFT裸题