BZOJ 2179 [FFT]

本文介绍了一种基于快速傅立叶变换(FFT)的高效多项式乘法算法,并提供了详细的C++实现代码。该算法利用复数运算加速了大整数乘法过程,适用于处理大规模数据集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给出两个n10进制整数xy,你需要计算xyn60000

FFT模板题
自己写了一个十分简陋功能不全的complex

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <complex>
using namespace std;

//typedef complex<double> P;

struct P {
    double r, v;
    inline double real(void) {
        return r;
    }
    inline P(double _r = 0, double _v = 0):r(_r), v(_v) {}
    inline P operator =(double a) {
        this->r = a; this->v = 0; return *this;
    }
    inline P operator *(const P &a) {
        P c(r * a.r - v * a.v, r * a.v + v * a.r);
        return c;
    }
    inline P operator +(const P &a) {
        return P(r + a.r, v + a.v);
    }
    inline P operator -(const P &a) {
        return P(r - a.r, v - a.v);
    }
    inline P operator *=(const P &a) {
        *this = *this * a; return *this;
    }
    inline P operator /(double a) {
        return P(r / a, v / a);
    }
    inline P operator /=(double a) {
        *this = *this / a; return *this;
    }
};

const int N = 140100;
const double pi = acos(-1);

int n, L, m;
int Rev[N], c[N];
P a[N], b[N];

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
inline void read(int &x) {
    static char c; x = 0;
    for (c = get(); c < '0' || c > '9'; c = get());
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
inline char GetNum(void) {
    static char c;
    for (c = get(); c < '0' || c > '9'; c = get());
    return c;
}

void FFT(P *a, int f) {
    for (int i = 0; i < n; i++)
        if (Rev[i] > i) swap(a[i], a[Rev[i]]);
    for (int i = 1; i < n; i <<= 1) {
        P wn(cos(pi / i), f * sin(pi / i));
        for (int j = 0; j < n; j += (i << 1)) {
            P w(1, 0);
            for (int k = 0; k < i; k++) {
                P x = a[j + k], y = w * a[j + i + k];
                a[j + k] = x + y; a[j + i + k] = x - y;
                w *= wn;
            }
        }
    }
    if (f == -1) for (int i = 0; i < n; i++) a[i] /= n;
}

int main(void) {
    read(n); n--; m = 2 * n;
    for (int i = 0; i <= n; i++) a[n - i] = GetNum() - '0';
    for (int i = 0; i <= n; i++) b[n - i] = GetNum() - '0';
    for (n = 1; n <= m; n <<= 1) L++;
    for (int i = 1; i < n; i++)
        Rev[i] = (Rev[i >> 1] >> 1) | ((i & 1) << (L - 1));
    FFT(a, 1); FFT(b, 1);
    for (int i = 0; i <= n; i++) a[i] *= b[i];
    FFT(a, -1);
    for (int i = 0; i <= m; i++) c[i] = int(a[i].real() + 0.1);
    for (int i = 0; i <= m; i++)
        if (c[i] >= 10) {
            c[i + 1] += c[i] / 10;
            c[i] %= 10;
            if (i == m) m++;
        }
    for (int i = m; i >= 0; i--) putchar(c[i] + '0');
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值