题目链接:点这里
题目大意:A*B=???
解题思路:FFT模板,别问为什么,问就是不会(脑部滑稽)
#include
using namespace std;
typedef long long LL;
const double PI=acos(-1.0),eps=1e-8;
#define L(x) (1 << (x))
const int maxn = 200000;
double ax[maxn], ay[maxn], bx[maxn], by[maxn];
int revv(int x, int bits)
{
int ret = 0;
for (int i = 0; i < bits; i++)
{
ret <<= 1;
ret |= x & 1;
x >>= 1;
}
return ret;
}
void fft(double * a, double * b, int n, bool rev)
{
int bits = 0;
while (1 << bits < n) ++bits;
for (int i = 0; i < n; i++)
{
int j = revv(i, bits);
if (i < j)
swap(a[i], a[j]), swap(b[i], b[j]);
}
for (int len = 2; len <= n; len <<= 1)
{
int half = len >> 1;
double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len);
if (rev) wmy = -wmy;
for (int i = 0; i < n; i += len)
{
double wx = 1, wy = 0;
for (int j = 0; j < half; j++)
{
double cx = a[i + j], cy = b[i + j];
double dx = a[i + j + half], dy = b[i + j + half];
double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx;
a[i + j] = cx + ex, b[i + j] = cy + ey;
a[i + j + half] = cx - ex, b[i + j + half] = cy - ey;
double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx;
wx = wnx, wy = wny;
}
}
}
if (rev)
{
for (int i = 0; i < n; i++)
a[i] /= n, b[i] /= n;
}
}
int solve(int a[],int na,int b[],int nb,int ans[])
{
int len = max(na, nb), ln;
for(ln=0; L(ln)= na) ax[i] = 0, ay[i] =0;
else ax[i] = a[i], ay[i] = 0;
}
fft(ax, ay, len, 0);
for (int i = 0; i < len; ++i)
{
if (i >= nb) bx[i] = 0, by[i] = 0;
else bx[i] = b[i], by[i] = 0;
}
fft(bx, by, len, 0);
for (int i = 0; i < len; ++i)
{
double cx = ax[i] * bx[i] - ay[i] * by[i];
double cy = ax[i] * by[i] + ay[i] * bx[i];
ax[i] = cx, ay[i] = cy;
}
fft(ax, ay, len, 1);
for (int i = 0; i < len; ++i)
ans[i] = (int)(ax[i] + 0.5);
return len;
}
char s[maxn],t[maxn];
int a[maxn],b[maxn],c[maxn],lena,lenb;
int main()
{
while(~scanf("%s%s",s,t))
{
memset(c,0,sizeof c);
lena=strlen(s); lenb=strlen(t);
if(lena==1&&s[0]=='0') {printf("0\n"); continue;}//其中一个为0;
if(lenb==1&&t[0]=='0') {printf("0\n"); continue;}
for(int i=lena-1;i>=0;i--) a[lena-1-i]=s[i]-'0';
for(int i=lenb-1;i>=0;i--) b[lenb-1-i]=t[i]-'0';
solve(a,lena,b,lenb,c);//核心a*b存于c[]中;
int k=0;
for(int i=0;i<=100000;i++){int p=c[i]+k;c[i]=p%10,k=p/10; }//进位
int len; for(int i=0;i<=100000;i++) if(c[i]!=0) len=i;//去前导0;
for(int i=len;i>=0;i--) printf("%d",c[i]); printf("\n");
}
return 0;
}