HDU 1402 A * B Problem Plus [FFT]【数论】

68 篇文章 0 订阅
26 篇文章 0 订阅

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402
———————————————————————————.
A * B Problem Plus

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19099 Accepted Submission(s): 4406

Problem Description
Calculate A * B.

Input
Each line will contain two integers A and B. Process to end of file.

Note: the length of each integer will not exceed 50000.

Output
For each case, output A * B in one line.

Sample Input
1
2
1000
2

Sample Output
2
2000

Author
DOOM III
———————————————————————————.

就是个FFT入门题 。
因为题目给的字符串长度达到了 50000 ,如果普通的大数乘法复杂度为 O(lenAlenB) 必定会超时.
FFT其实就是一个快速求取卷积的过程.
而一个乘法计算其实也就是一个卷积
对于个数 a0a1a2a3...an
也就是 A(x=10)=a0x0+a1x1+a2x2+a3x3+...anxn
所以这个题就可以用FFT来计算了。。
复杂度能降到 O(Nlog2N)

附本题代码
—————————————————————-.

#include <bits/stdc++.h>
using namespace std;
const int    N   = 50000+5;
const double PI  = acos(-1.0);
/***********************************************************************/
struct Complex{
    double real, image;
    Complex(double _real, double _image){
        real = _real;
        image = _image;
    }
    Complex(){}
};

Complex *AA = new Complex[N<<2],*BB = new Complex[N<<2];
Complex operator + (const Complex &c1, const Complex &c2){
    return Complex(c1.real + c2.real, c1.image + c2.image);
}
Complex operator - (const Complex &c1, const Complex &c2){
    return Complex(c1.real - c2.real, c1.image - c2.image);
}
Complex operator * (const Complex &c1, const Complex &c2){
    return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}

int rev(int id, int len){
    int ret = 0;
    for(int i = 0; (1 << i) < len; i++){
        ret <<= 1;
        if(id & (1 << i)) ret |= 1;
    }
    return ret;
}
Complex A[N<<2];
void FFT(Complex *a, int len, int DFT)
{
    for(int i = 0; i < len; i++)
        A[rev(i, len)] = a[i];
    for(int s = 1; (1 << s) <= len; s++)
    {
        int m = (1 << s);
        Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
        for(int k = 0; k < len; k += m)
        {
            Complex w = Complex(1, 0);
            for(int j = 0; j < (m >> 1); j++)
            {
                Complex t = w*A[k + j + (m >> 1)];
                Complex u = A[k + j];
                A[k + j] = u + t;
                A[k + j + (m >> 1)] = u - t;
                w = w*wm;
            }
        }
    }
    if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
    for(int i = 0; i < len; i++) a[i] = A[i];
    return;
}

char a[N],b[N];
int ans[N<<2];

int main(){
    while(~scanf("%s",a)){
        scanf("%s",b);
        int la = strlen(a);
        int lb = strlen(b);

        int sa,sb;
        sa=sb=0;
        while((1<<sa)<la) sa++;
        while((1<<sb)<lb) sb++;
        int len = (1<<(max(sa,sb)+1));
        for(int i=0;i<len;i++){
            AA[i]=Complex(((i<la)?(a[la-i-1]-'0'):0),0);
            BB[i]=Complex(((i<lb)?(b[lb-i-1]-'0'):0),0);
        }
        FFT(AA,len,1);
        FFT(BB,len,1);
        for(int i=0;i<len;i++)  AA[i]=AA[i]*BB[i],ans[i]=0;
        FFT(AA,len,-1);
        for(int i=0;i<len  ;i++) ans[i]+=(int)(AA[i].real+0.5);
        for(int i=0;i<len-1;i++) ans[i+1]+=ans[i]/10,ans[i]%=10;
        bool flag = false;
        for(int i=len-1;i>=0;--i){
            if(ans[i]) printf("%d",ans[i]),flag=true;
            else if(flag||0==i) printf("0");
        }
        puts("");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值