[FFT 模板题] HDU 1402:A*B Problem Plus
来源:互联网 发布:淘宝代运营公司 编辑:程序博客网 时间:2024/06/06 05:13
题意
实现高精乘法。位数<=50000
题解
FFT 模板题。
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const double PI=acos(-1.0);const int maxn=(1<<18)+5;struct E{ double real,imag; E(double real=0,double imag=0):real(real),imag(imag){} void operator /=(double x){ real/=x; imag/=x; }};E operator + (E &a,E &b){ return E(a.real+b.real,a.imag+b.imag); } E operator - (E &a,E &b){ return E(a.real-b.real,a.imag-b.imag); } E operator * (E &a,E &b){ return E(a.real*b.real-a.imag*b.imag,a.imag*b.real+a.real*b.imag); }int rev[maxn];void build_rev(int n){ rev[0]=0; int log2n=log2(n); for(int i=1;i<=n-1;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(log2n-1));}const double eps=1e-10;int dcmp(double x){ if(fabs(x)<eps) return 0; return x>0?1:-1;}int b[maxn];struct Complex_Line{ int n; E a[maxn]; void mem(){ for(int i=0;i<=n-1;i++) a[i].real=a[i].imag=0; n=0; } void FFT(int k){ build_rev(n); for (int i=0;i<=n-1;i++) if (i<rev[i]) swap(a[i],a[rev[i]]); for(int m=2;m<=n;m<<=1){ E wm(cos(2*PI/m),k*sin(2*PI/m)); for(int k=0;k<n-1;k+=m){ E w(1,0),t0,t1; for(int j=0;j<=m/2-1;j++,w=w*wm) t0=a[k+j], t1=w*a[k+j+m/2], a[k+j]=t0+t1, a[k+j+m/2]=t0-t1; } } if(k==-1) for(int i=0;i<=n;i++) a[i]/=n; } void write(){ memset(b,0,sizeof b); for(int i=0;i<=n-1;i++) b[i]+=(int)(a[i].real+0.5), b[i+1]+=b[i]/10, b[i]%=10; int m=n; while(m>1&&!b[m-1]) m--; for(int i=m-1;i>=0;i--) printf("%d",b[i]); }} A,B,C;char s[maxn];int main(){ freopen("hdoj1402.in","r",stdin); freopen("hdoj1402.out","w",stdout); for(scanf("%s",s);s[0]!='\000';s[0]='\000',scanf("%s",s)){ A.mem(); A.n=strlen(s); for(int i=0;i<=A.n-1;i++) A.a[i].real=s[A.n-1-i]-'0'; scanf("%s",s); B.mem(); B.n=strlen(s); for(int i=0;i<=B.n-1;i++) B.a[i].real=s[B.n-1-i]-'0'; C.n=1; while(C.n<A.n+B.n) C.n<<=1; A.n=C.n; A.FFT(1); B.n=C.n; B.FFT(1); for(int i=0;i<=C.n-1;i++) C.a[i]=A.a[i]*B.a[i]; C.FFT(-1); C.write(); printf("\n"); } return 0;}
0 0
- HDU 1402 A * B Problem Plus (FFT模板题)
- HDU 1402 A * B Problem Plus (FFT模板题)
- [FFT 模板题] HDU 1402:A*B Problem Plus
- hdu 1402 A * B Problem Plus FFT模板
- hdu 1402 A * B Problem Plus FFT模板
- hdu 1402 A * B Problem Plus FFT
- HDU 1402 A * B Problem Plus FFT
- [HDU 1402]A * B Problem Plus(FFT)
- HDU 1402 A * B Problem Plus FFT
- 【HDU】1402 A * B Problem Plus 【FFT】
- 【HDU】1402 A * B Problem Plus 【FFT】
- HDU 1402 A * B Problem Plus FFT
- hdu 1402 A * B Problem Plus[【FFT】
- HDU 1402 A * B Problem Plus FFT
- hdu 1402 A * B Problem Plus(FFT)
- [HDU]1402 A * B Problem Plus [FFT]
- hdu 1402 A * B Problem Plus(FFT)
- HDU 1402 A * B Problem Plus (大数乘法,FFT模板题)
- Ubuntu 16.04安装使用GNU GLOBAL
- LeetCode 100 Same Tree
- List去重排序
- [UVA122]Trees on the level
- 数列有序! 杭电2019
- [FFT 模板题] HDU 1402:A*B Problem Plus
- 基于Javascript的简单图片管理
- 【JQuery】熟能生巧JQuery(四)——基本选择器
- Android Studio一直卡在refreshing gradle project 中
- java_sdut_计算长方形的周长和面积(类和对象)
- Collections.sort()和Arrays.sort()排序算法选择
- js获取地址栏内容
- B1067. 试密码(20)
- PHP 导出网页表格如何对<td>标签中的内容设置属性