【FFT】大整数乘法

本文介绍了使用快速傅里叶变换(FFT)进行大整数乘法的方法,包括如何处理精度问题和位数压缩。特别提到了在HDU1402题目中的应用,以及由于位数压缩过多可能导致的精度不足和整数溢出问题。
摘要由CSDN通过智能技术生成

http://www.cnblogs.com/skyivben/archive/2008/07/23/1248413.html

整理一下模板

hdu1402

sincos需要手写,因为hdu没有

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const double pi = acos(-1.0);
const int maxn = 1 << 18;

struct Complex {
    double x, y;

    Complex (double real = 0, double imag = 0) : x(real), y(imag) {}

    double &real() {
        return x;
    }

    double &imag() {
        return y;
    }
    
    void print()
    {
        cout<<"real="<<x<<" imag=%.7lf\n"<<y<<endl;
    }
}Pa[500000],Pb[500000],Pc[500000];
long long ans[500000],la,lb,N,A[500000],B[500000];
char a[500000],b[500000];
int t;
Complex operator+(const Complex &a, const Complex &b) {
    return Complex(a.x + b.x, a.y + b.y);
}

Complex operator-(const Complex &a, const Complex &b) {
    return Complex(a.x - b.x, a.y - b.y);
}

Complex operator*(const Complex &a, const Complex &b) {
    return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}

inline void sincos(double theta,double &p0,double &p1)
{
    p0=sin(theta);
    p1=cos(theta);
}

void FFT(Complex P[], int n, int oper)
{
    for (int i = 1, j = 0; i < n - 1; i++) {
        for (int s = n; j ^= s >>= 1, ~j & s;);
        if (i < j) {
            swap(P[i], P[j]);
        }
    }
    Complex unit_p0;
    for (int d = 0; (1 << d) < n; d++) {
        int m = 1 << d, m2 = m * 2;
        double p0 = pi / m * oper;
        sincos(p0, unit_p0.y, unit_p0.x);
        for (int i = 0; i < n; i += m2) {
            Complex unit = 1;
            for (int j = 0; j < m; j++) {
                Complex &P1 = P[i + j &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值