[51NOD] 1028 大数乘法 V2 [NTT]

给出2个大整数A,B,计算A*B的结果。
Input
第1行:大数A
第2行:大数B
(A,B的长度 <= 100000,A,B >= 0)
Output
输出A * B
Input示例
123456
234567
Output示例
28958703552

这是FNTT或FFT的模板题
值得参考的FNTT资料
http://blog.csdn.net/acdreamers/article/details/39026505

学FNTT之前建议先学FFT
值得参考的FFT资料
https://www.zybuluo.com/397915842/note/37965
http://blog.miskcoo.com/2015/04/polynomial-multiplication-and-fast-fourier-transform#mjx-eqn-IDFT-equation

#include
#include
#include
using namespace std;

const int g = 3;
const int p = (119 << 23) + 1;
const int MAXN = 1 << 18;
const int NUM = 23;
int A[MAXN], B[MAXN], wn[NUM];
char str_1[MAXN], str_2[MAXN];
int res[MAXN], len, len_1, len_2;

int mod_pow(int a, int n, int mod) {
    int num = 1;
    while(n) {
        if(n&1) num = 1LL * num * a % mod;
        a = 1LL * a * a % mod;
        n >>= 1;
    }
    return num;
}

void init_wn() {
    for(int i = 0; i < NUM; ++i) {
        int t = 1 << i;
        wn[i] = mod_pow(g, (p - 1) / t, p);
    }
}

void Init() {
    len_1 = strlen(str_1), len_2 = strlen(str_2);
    int ml = max(len_1, len_2);
    for(len = 1; len < (ml << 1); len <<= 1);
    for(int i = 0; i < len_1; ++i) {
        A[i] = str_1[len_1 - i - 1] - '0';
    }
    for(int i = 0; i < len_2; ++i) {
        B[i] = str_2[len_2 - i - 1] - '0';
    }
    for(int i = len_1; i < len; ++i) {
        A[i] = 0;
    }
    for(int i = len_2; i < len; ++i) {
        B[i] = 0;
    }
}

void Rader(int y[]) {
    for(int i = 1, j = len >> 1, k; i < len - 1; ++ i) {
        if(i < j) swap(y[i], y[j]);
        k = len >> 1;
        while(j >= k) {
            j -= k;
            k >>= 1;
        }
        if(j < k) j += k;
    }
}

void NTT(int y[], int op) {
    Rader(y);
    int id = 0;
    for(int h = 2; h <= len; h <<= 1) {
        ++id;
        for(int i = 0; i < len; i += h) {
            int w = 1;
            for(int j = i; j < i + h / 2; ++j) {
                int u = y[j] % p;
                int t = 1LL * w * y[j + h / 2] % p;
                y[j] = (u + t) % p;
                y[j + h / 2] = (u - t) % p;
                w = 1LL * w * wn[id] % p;
            }
        }
    }
    if(op == -1) {
        for(int i = 1; i < len / 2; ++i) {
            swap(y[i], y[len - i]);
        }
        int inv = mod_pow(len, p - 2, p);
        for(int i = 0; i < len; ++i) {
            y[i] = 1LL * y[i] * inv % p;
            y[i] += p;
            y[i] %= p;
        }
    }
}

void Output() {
    int pos = len - 1;
    while(pos && res[pos] == 0) --pos;
    while(~pos) putchar(res[pos--] + '0');
    putchar('\n');
}

void Mul(int x[], int y[]) {
    for(int i = 0; i < len; ++i) {
        x[i] = 1LL * x[i] * y[i] % p;
    }
}

void Slove() {
    NTT(A, 1);
    NTT(B, 1);
    Mul(A, B);
    NTT(A, -1);
    res[0] = 0;
    for(int i = 0; i < len; ++i) {
        res[i] += A[i];
        res[i + 1] = res[i] / 10;
        res[i] %= 10;
    }
    Output();
}

int main()
{
    init_wn();
    while(scanf("%s%s", str_1, str_2) != EOF) {
        Init();
        Slove();
    }
    return 0;
}

你可能感兴趣的:(FFT,NTT)