关闭

HDU 1402 A * B Problem Plus (FFT求高精度乘法)

标签: fftHDU-1420数学
407人阅读 评论(0) 收藏 举报
分类:

存一波代码:题目比较简单。

#include <bits/stdc++.h>
#define LL long long
#define FOR(i,x,y)  for(int i = x;i < y;++ i)
#define IFOR(i,x,y) for(int i = x;i > y;-- i)

using namespace std;

//FFT copy from kuangbin
const double pi = acos (-1.0);
// Complex  z = a + b * i
struct Complex {
    double a, b;

    Complex(double _a=0.0,double _b=0.0):a(_a),b(_b){}

    Complex operator + (const Complex &rhs) const {
        return Complex(a + rhs.a , b + rhs.b);
    }

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

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

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

// FFT
// len = 2 ^ k
// on = 1  DFT    on = -1 IDFT
void FFT (Complex y[], int len , int on) {
    change (y , len);
    for (int h = 2 ; h <= len ; h <<= 1) {
        Complex wn(cos (-on * 2 * pi / h), sin (-on * 2 * pi / h));
        for (int j = 0 ; j < len ; j += h) {
            Complex w(1 , 0);
            for (int 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 (int i = 0 ; i < len ; i ++) {
            y[i].a /= len;
        }
    }
}

const int maxn = 50050;
const int mm = maxn<<2;

Complex a[maxn<<2],b[maxn<<2],c[maxn<<2];
LL num[maxn<<2];
int ans[maxn<<2];
char s1[maxn<<1],s2[maxn<<1];
int len;
int flag;

void work(){
    memset(num,0,sizeof(num));
    FFT(a,len,1);
    FFT(b,len,1);
    FOR(i,0,len)    c[i] = a[i]*b[i];
    FFT(c,len,-1);
    FOR(i,0,len)    num[i] = (LL)(c[i].a+0.5);
    LL car = 0;
    FOR(i,0,mm){
        ans[i] = (num[i]+car)%10;
        car = (num[i]+car)/10;
    }
    int tot = 0;
    IFOR(i,mm-1,-1){
        if(ans[i])  {tot = i;break;}
    }
    if(!flag)   printf("-");
    IFOR(i,tot,-1){
        printf("%d",ans[i]);
    }
    printf("\n");
}

int main()
{
    //freopen("test.in","r",stdin);
    while(~scanf("%s%s",s1,s2)){
        flag = 1;
        int len1 = strlen(s1),len2 = strlen(s2);
        FOR(i,1,len1)   a[len1-1-i] = Complex(s1[i] - '0',0);
        FOR(i,1,len2)   b[len2-1-i] = Complex(s2[i] - '0',0);
        if(s1[0] == '-') {flag ^= 1;-- len1;}
        else    a[len1-1] = s1[0] - '0';
        if(s2[0] == '-') {flag ^= 1;-- len2;}
        else    b[len2-1] = s2[0] - '0';
        int mx_len = 1+max(len1,len2);
        mx_len <<= 1;
        len = 1;
        while(len < mx_len) len <<= 1;
        FOR(i,len1,len) a[i] = Complex(0,0);
        FOR(i,len2,len) b[i] = Complex(0,0);
        work();
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:60462次
    • 积分:2053
    • 等级:
    • 排名:第19762名
    • 原创:150篇
    • 转载:0篇
    • 译文:0篇
    • 评论:8条
    最新评论