HDU 1402 A * B Problem Plus(FFT加速优化乘法)

传送门

A * B Problem Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18312    Accepted Submission(s): 4106


Problem Description
Calculate A * B.
 

Input
Each line will contain two integers A and B. Process to end of file.

Note: the length of each integer will not exceed 50000.
 

Output
For each case, output A * B in one line.
 

Sample Input
    
    
    
    
1
2
1000
2
 

Sample Output
    
    
    
    
2
2000

题目大意:

给定两个大整数,求 AB 的值。

吐槽:

其实这个题目完全可以用 Java 做,但是为了训练 FFT ,建议还是用 C 写一发。

解题思路:

因为大整数的位数太大啦,如果暴力算的话肯定会超时,所以我们要用到一种加速方法 FFT ,举个例子, 1212 我们可以写成 (x+2)(x+2) ==> x2+4x+4 所以就是 144 我们将两个大整数的乘法变换成多项式的乘法,那么就可以用 FFT 来优化了,但是我们会遇到一些问题: 1818 ==> (x+8)(x+8) 本来 1818 应该得 324 但是在多项式中的话就会变成 x2+36x+64 所以应该是每一位的对 10 取模并且加上后面一项除以 10 ,然后就基本上就是模板了。

My Code

#include 
#include 
#include 
#include 
#include 
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;
const LL MAXN = 2e5+5;
struct complex {
    double a, b;
    complex(double aa = 0.0, double bb = 0.0) { a = aa; b = bb; }
    complex operator +(const complex &e) { return complex(a + e.a, b + e.b); }
    complex operator -(const complex &e) { return complex(a - e.a, b - e.b); }
    complex operator *(const complex &e) { return complex(a * e.a - b * e.b, a * e.b + b * e.a); }
}x1[MAXN], x2[MAXN], x[MAXN];

void change(complex * y, LL len) {
    LL i, j, k;
    for (i = 1, j = len / 2; i < len - 1; i++) {
        if (i < j) swap(y[i], y[j]);
        k = len / 2;
        while (j >= k) {
            j -= k;
            k /= 2;
        }
        if (j < k) j += k;
    }
}

void fft(complex *y, LL len, LL on) {
    change(y, len);
    for (LL h = 2; h <= len; h <<= 1) {
        complex wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
        for (LL j = 0; j < len; j += h) {
            complex w(1, 0);
            for (LL k = j; k < j + h / 2; k++) {
                complex u = y[k];
                complex t = w * y[k + h / 2];
                y[k] = u + t;
                y[k + h / 2] = u - t;
                w = w * wn;
            }
        }
    }
    if (on == -1)
        for (LL i = 0; i < len; i++)
            y[i].a /= len;
}
char A[MAXN], B[MAXN];
LL f[MAXN], g[MAXN];
LL ans[MAXN];
int main()
{
    while(~scanf("%s%s",A,B))
    {
        LL len = 1LL;
        LL len1 = strlen(A);
        LL len2 = strlen(B);
        memset(f, 0, sizeof(f));
        memset(g, 0, sizeof(g));
        for(LL i=0; i'0');
        for(LL i=0; i'0');
        while(len<2*len1 || len<2*len2)len<<=1LL;
        ///cout<
        for(LL i=0; i1].a = f[i], x1[i].b = 0;
        for(LL i=len1; i0, x1[i].b = 0;
        for(LL i=0; i1-i].a = g[i], x2[i].b = 0;
        for(LL i=len2; i0, x2[i].b = 0;
        fft(x1, len, 1), fft(x2, len, 1);
        for(LL i=0; i1);
        memset(ans, 0, sizeof(ans));
        for(LL i=0; i0.5);
        for(LL i=0; i1] += ans[i] / 10;
            ans[i] %= 10;
        }
        LL tmp = 0;
        for(LL i=len-1; i>=0; i--)
            if(ans[i]){
                tmp = i;
                break;
            }
        for(LL i=tmp; i>=0; i--)
            putchar(ans[i]+'0');///(输出加速)
        puts("");
    }
    return 0;
}

你可能感兴趣的:(ACM_数论,ACM-FFT,ACM_HDU,ACM_模板,ITAK的ACM之路)