HDU1402:A * B Problem Plus——题解

http://acm.hdu.edu.cn/showproblem.php?pid=1402

给出两个高精度正整数,求它们的积,最长的数长度不大于5e4。

FFT裸题,将每个数位看做是多项式的系数即可。

我们最后就是要求出两个多项式相乘的系数。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef double dl;
const dl pi=acos(-1.0);
const int N=2e5+10;
struct complex{//定义复数
    dl x,y;
    complex(dl xx=0.0,dl yy=0.0){
        x=xx;y=yy;
    }
    complex operator +(const complex &b)const{
        return complex(x+b.x,y+b.y);
    }
    complex operator -(const complex &b)const{
        return complex(x-b.x,y-b.y);
    }
    complex operator *(const complex &b)const{
        return complex(x*b.x-y*b.y,x*b.y+y*b.x);
    }
};
void FFT(complex a[],int n,int on){
    for(int i=1,j=n>>1;i<n-1;i++){
        if(i<j)swap(a[i],a[j]);
        int k=n>>1;
        while(j>=k){j-=k;k>>=1;}
        if(j<k)j+=k;
    }
    for(int i=2;i<=n;i<<=1){
        complex res(cos(-on*2*pi/i),sin(-on*2*pi/i));
        for(int j=0;j<n;j+=i){
            complex w(1,0);
            for(int k=j;k<j+i/2;k++){
                complex u=a[k],t=w*a[k+i/2];
                a[k]=u+t;
                a[k+i/2]=u-t;
                w=w*res;
            }
        }
    }
    if(on==-1)
        for(int i=0;i<n;i++)a[i].x/=n;
}
char a[N],b[N];
complex x[N],y[N];
int ans[N];
int main(){
    while(cin>>a>>b){
        int len1=strlen(a),len2=strlen(b);
        int n=1;
        while(n<len1*2||n<len2*2)n<<=1;
        for(int i=0;i<len1;i++)x[i]=complex(a[len1-1-i]-‘0‘,0);
        for(int i=len1;i<n;i++)x[i]=complex(0,0);
        for(int i=0;i<len2;i++)y[i]=complex(b[len2-1-i]-‘0‘,0);
        for(int i=len2;i<n;i++)y[i]=complex(0,0);
        FFT(x,n,1);FFT(y,n,1);
        for(int i=0;i<n;i++)x[i]=x[i]*y[i];
        FFT(x,n,-1);
        for(int i=0;i<n;i++)ans[i]=(int)(x[i].x+0.5);
        for(int i=0;i<n;i++){
            ans[i+1]+=ans[i]/10;
            ans[i]%=10;
        }
        n=len1+len2-1;
        while(ans[n]<=0&&n>0)n--;
        for(int i=n;i>=0;i--)printf("%d",ans[i]);
        puts("");
    }
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

原文地址:https://www.cnblogs.com/luyouqi233/p/8448969.html

时间: 2024-08-30 14:17:09

HDU1402:A * B Problem Plus——题解的相关文章

programming-challenges Shoemaker&amp;#39;s Problem (110405) 题解

Greedy. 证明: Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., tn, fn and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn So this is the objective schedule. Now we change 1 with m (1 < m

programming-challenges Shoemaker&#39;s Problem (110405) 题解

Greedy. 证明: Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., tn, fn and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn So this is the objective schedule. Now we change 1 with m (1 < m

Light OJ 1004 - Monkey Banana Problem dp题解

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dim

Lintcode1 A+B Problem solution 题解

[题目描述] Write a function that add two numbers A and B. You should not use + or any arithmetic operators. Notice:There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and

hdu----(1402)A * B Problem Plus(FFT模板)

A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12665    Accepted Submission(s): 2248 Problem Description Calculate A * B. Input Each line will contain two integers A and B. P

【NTT】hdu1402 A * B Problem Plus

r·2^k+1 r k g 3 1 1 2 5 1 2 2 17 1 4 3 97 3 5 5 193 3 6 5 257 1 8 3 7681 15 9 17 12289 3 12 11 40961 5 13 3 65537 1 16 3 786433 3 18 10 5767169 11 19 3 7340033 7 20 3 23068673 11 21 3 104857601 25 22 3 167772161 5 25 3 469762049 7 26 3 998244353 119

【FFT】hdu1402 A * B Problem Plus

FFT板子. 将大整数看作多项式,它们的乘积即多项式的乘积在x=10处的取值. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define EPS 1e-8 const double PI = acos(-1.0); struct Complex{ double real,image; Complex(double _r

HDU1402 A * B Problem Plus FFT

分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来解决数据规模较大时的办法,可以达到nlogn的效率,大体原理就是运用了n次单位复根的折半引理 具体可以看算法导论 高度仰慕kuangbin大神的模板,实在是不想自己写 对于这个题,我们10^k的系数看成多项式系数,然后求卷积,进位就好了 #include <stdio.h> #include &l

[HDU1402]A * B Problem Plus(FFT)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意:大数乘法. 数太大,O(n^2)的不行,得用fft对乘法加速. 手推了一遍FFT的公式,感觉欧拉和分治很强,纪念我的第一发FFT. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const double PI = acos(-1.0); 5 //复数结构体 6 typedef struct Complex { 7 doubl