CODEVS 3123 高精度练习之超大整数乘法

题目描述 Description

给出两个正整数A和B,计算A*B的值。保证A和B的位数不超过100000位。

输入描述 Input Description

读入两个用空格隔开的正整数

输出描述 Output Description

输出A*B的值

样例输入 Sample Input

4 9

样例输出 Sample Output

36

数据范围及提示 Data Size & Hint

两个正整数的位数不超过100000位

分析

FFT板子题

代码

#include 

using namespace std;

const double _2pi = 3.1415926535 * 2;

const int maxn = 10000000;
int ans[maxn];

struct complexNumber
{
    double real, image;
};

complexNumber add(complexNumber a, complexNumber b)
{
    return (complexNumber){a.real + b.real, a.image + b.image};
}

complexNumber subtract(complexNumber a, complexNumber b)
{
    return (complexNumber){a.real - b.real, a.image - b.image};
}

complexNumber multiply(complexNumber a, complexNumber b)
{
    return (complexNumber){a.real * b.real - a.image * b.image, a.real * b.image + a.image * b.real};
}

vector A, B;

void init()
{
    ios::sync_with_stdio(false);
    string n, m;
    cin >> n >> m;
    for(int i = n.length() - 1; i >= 0; i--)
        A.push_back((complexNumber){double(n[i] - '0'), 0.0});
    for(int i = m.length() - 1; i >= 0; i--)
        B.push_back((complexNumber){double(m[i] - '0'), 0.0});
}

vector FFT(vector X, bool inverse)
{
    int n = X.size();

    for(int i = 0, j = 0; i < n; i++)
    {
        if(i < j)
            swap(X[i], X[j]);
        int k = n;
        while(j & (k >>= 1))
            j &= ~k;
        j |= k;
    }

    double theta = inverse == false ? _2pi : -_2pi;

    for(int s = 1; s <= log2((double)n); s++)
    {
        int m = pow(2, s);
        complexNumber omega_n = (complexNumber){cos(theta / double(m)), sin(theta / double(m))};
        for(int k = 0; k < n; k += m)
        {
            complexNumber omega = (complexNumber){1.0, 0.0};
            for(int j = 0; j < m / 2; j++)
            {
                complexNumber u = X[k + j], t = multiply(omega, X[k + j + m / 2]);
                X[k + j] = add(u, t);
                X[k + j + m / 2] = subtract(u, t);
                omega = multiply(omega, omega_n);
            }
        }
    }

    if(inverse == true)
        for(int i = 0; i < n; i++)
            X[i] = multiply(X[i], (complexNumber){1.0 / n, 0.0});

    return X;
}

vector Convolution(vector A, vector B)
{
    vector C;
    int s1 = A.size(), s2 = B.size(), n = 2;
    while(n < s1 + s2)
        n <<= 1;

    for(int i = s1; i < n; i++)
        A.push_back((complexNumber){0.0, 0.0});
    vector alpha = FFT(A, false);

    for(int i = s2; i < n; i++)
        B.push_back((complexNumber){0.0, 0.0});
    vector beta = FFT(B, false);

    for(int i = 0; i < n; i++)
        C.push_back(multiply(alpha[i], beta[i]));
    C = FFT(C, true);

    return C;
}

void solve()
{
    vector C = Convolution(A, B);
    int len = A.size() + B.size() - 1;
    for(int i = 0; i < len; i++)
        ans[i] = (int)round(C[i].real);
    for(int i = 0; i < len; i++)
        ans[i + 1] += ans[i] / 10, ans[i] %= 10;
    while(ans[len] == 0 && len > 0)
        len--;
    for(int i = len; i >= 0; i--)
        cout << ans[i];
}

int main()
{
    init();
    solve();
    return 0;
}

你可能感兴趣的:(FFT,fft-c)