51Nod 1028 大数乘法 V2

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028

分析:

FFT/NTT板子题...

代码:

NTT板子:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;

const int maxn=500000+5,mod=998244353,G=3;

int n,m,L,len1,len2,R[maxn],a[maxn],b[maxn];

char ch[2][maxn];

inline int power(long long x,int y){
	long long res=1;
	while(y){
		if(y&1)
			res=res*x%mod;
		x=x*x%mod,y>>=1;
	}
	return res;
}

inline void NTT(int *a,int f){
	for(int i=0;i<n;i++)
		if(i<R[i]) swap(a[i],a[R[i]]);
	for(int i=1;i<n;i<<=1){
		int wn=power(G,(mod-1)/(i<<1));
		if(f==-1) wn=power(wn,mod-2);
		for(int j=0;j<n;j+=(i<<1)){
			int w=1;
			for(int k=0;k<i;k++,w=1LL*w*wn%mod){
				int x=a[j+k],y=1LL*w*a[j+k+i]%mod;
				a[j+k]=((x+y)%mod+mod)%mod;
				a[j+k+i]=((x-y)%mod+mod)%mod;
			}
		}
	}
	if(f==-1){
		int tmp=power(n,mod-2);
		for(int i=0;i<n;i++)
			a[i]=1LL*a[i]*tmp%mod;
	}
}

signed main(void){
	scanf("%s",ch[0]);len1=strlen(ch[0])-1;
	scanf("%s",ch[1]);len2=strlen(ch[1])-1;
	for(int i=0;i<=len1;i++) a[i]=ch[0][len1-i]-'0';
	for(int i=0;i<=len2;i++) b[i]=ch[1][len2-i]-'0';
	n=max(len1,len2);m=n<<1;for(n=1;n<=m;n<<=1) L++;
	for(int i=0;i<n;i++)
		R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
	NTT(a,1),NTT(b,1);
	for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i]%mod;
	NTT(a,-1);
	for(int i=0;i<m;i++)
		if(a[i]>=10)
			a[i+1]+=a[i]/10,a[i]%=10;
	while(!a[m]) m--;
	for(int i=m;i>=0;i--) printf("%d",a[i]);puts("");
	return 0;
}

  


By NeighThorn

posted @ 2017-03-02 16:34  NeighThorn  阅读(211)  评论(0编辑  收藏  举报