经过一晚上把论文上面的东西全部试着证明了一遍搞明白了卷积= =
实际上是因为昨天连FWT求卷积的题都出来了,如果连FFT都不会的话还是人?
Problem : 1402 ( A * B Problem Plus ) Judge Status : Accepted
RunId : 16778031 Language : G++ Author : BPMThor
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
/* ***********************************************
Author :BPM136
Created Time :2016/4/5 18:25:52
File Name :1402.cpp
************************************************ */
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<cstring>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define efo(i,x) for(int i=last[x];i!=0;i=e[i].next)
using namespace std;
LL read()
{
LL d=0,f=1;char s=getchar();
while(s<48||s>57){if(s==45)f=-1;s=getchar();}
while(s>=48&&s<=57){d=d*10+s-48;s=getchar();}
return d*f;
}
#define DB double
struct comple
{
DB r,i;
comple(DB x=0.0,DB y=0.0)
{
r=x,i=y;
}
comple operator+(comple &a)const
{
return comple(r+a.r,i+a.i);
}
comple operator-(comple &a)const
{
return comple(r-a.r,i-a.i);
}
comple operator*(comple &a)const
{
return comple((DB)r*a.r-i*a.i,(DB)r*a.i+i*a.r);
}
};
void change(comple y[],int len)
{
int i,j,k;
for(i=1,j=len/2;i<len-1;i++)
{
if(i<j)swap(y[i],y[j]);
k=len>>1;
while(j>=k)
{
j-=k;
k>>=1;
}
if(j<k)j+=k;
}
}
const DB pi=3.1415926525;
void FFT(comple y[],int len,int on)
{
change(y,len);
for(int n=2;n<=len;n<<=1)
{
comple w(cos(-on*2*pi/n),sin(-on*2*pi/n));
for(int j=0;j<len;j+=n)
{
comple wt(1,0);
for(int k=j;k<j+n/2;k++)
{
comple u=y[k];
comple v=wt*y[k+n/2];
y[k]=u+v;
y[k+n/2]=u-v;
wt=wt*w;
}
}
}
if(on==-1)fo(i,0,len-1)y[i].r/=len;
}
#define N 200005
comple x1[N],x2[N];
char str1[N],str2[N];
int sum[N];
int n,m;
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
while(scanf("%s%s",str1,str2)!=EOF)
{
int len1=strlen(str1),len2=strlen(str2);
int len;for(len=1;len<len1*2||len<len2*2;len<<=1);
fo(i,0,len1-1)x1[i]=comple(str1[len1-i-1]-'0',0);
fo(i,len1,len)x1[i]=comple(0,0);
fo(i,0,len2-1)x2[i]=comple(str2[len2-i-1]-'0',0);
fo(i,len2,len)x2[i]=comple(0,0);
FFT(x1,len,1);FFT(x2,len,1);
fo(i,0,len-1)x1[i]=x1[i]*x2[i];
FFT(x1,len,-1);
fo(i,0,len-1)sum[i]=(int)(x1[i].r+0.5);
fo(i,0,len-1)sum[i+1]+=sum[i]/10,sum[i]%=10;
for(len=len1+len2-1;sum[len]<=0&&len;len--);
fd(i,len,0)putchar((char)sum[i]+'0');putchar('\n');
}
return 0;
}