zoukankan      html  css  js  c++  java
  • HDU1402 A * B Problem Plus

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

    本文作者:ljh2000
    作者博客:http://www.cnblogs.com/ljh2000-jump/
    转载请注明出处,侵权必究,保留最终解释权!

    题目链接:HDU1402

    正解:FFT

    解题报告:

      FFT模板题,注意一下进位处理。

      重要的话说三遍:去掉多余的0!!!WA了几遍…

    //It is made by ljh2000
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <ctime>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <string>
    #include <complex>
    using namespace std;
    typedef long long LL;
    typedef complex<double> C;
    const int MAXN = 300011;
    const double pi = acos(-1);
    char ch[MAXN],s[MAXN];
    int n,m,ans[MAXN];
    C a[MAXN],b[MAXN];
    
    inline int getint(){
        int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
        if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
    }
    
    inline void fft(C *a,int n,int f){
    	if(n==1) return ;
    	C a0[n>>1],a1[n>>1],wn(cos(2*pi/n),sin(2*pi*f/n)),w(1,0),t;
    	for(int i=0;i<n>>1;i++) a0[i]=a[i<<1],a1[i]=a[i<<1|1];
    	fft(a0,n>>1,f); fft(a1,n>>1,f);
    	for(int i=0;i<n>>1;i++,w*=wn) {
    		t=a1[i]*w;
    		a[i]=a0[i]+t;
    		a[i+(n>>1)]=a0[i]-t;
    	}
    }
    
    inline void work(){
    	while(scanf("%s",ch)!=EOF) {
    		scanf("%s",s); memset(ans,0,sizeof(ans));
    		memset(a,0,sizeof(a)); memset(b,0,sizeof(b));//记得清空!
    		n=strlen(ch); m=strlen(s); n--; m--;
    		for(int i=n;i>=0;i--) a[n-i]=(int)ch[i]-'0';
    		for(int i=m;i>=0;i--) b[m-i]=(int)s[i]-'0';
    		m+=n; for(n=1;n<=m;n<<=1);
    		fft(a,n,1); fft(b,n,1);
    		for(int i=0;i<=n;i++) a[i]*=b[i];
    		fft(a,n,-1);
    		for(int i=0;i<=m;i++) ans[i]=(int)(a[i].real()/n+0.5);
    		for(int i=0;i<=m;i++) ans[i+1]+=ans[i]/10,ans[i]%=10;
    		while(ans[m+1]>0) m++,ans[m+1]+=ans[m]/10,ans[m]%=10;
    		while(ans[m]==0) m--;//注意去掉多余的0!!!
    		if(m>=0) { for(int i=m;i>=0;i--) printf("%d",ans[i]); puts(""); }
    		else puts("0");
    	}
    }
    
    int main()
    {
        work();
        return 0;
    }
    

      

  • 相关阅读:
    poj 1456 Supermarket (最短路程)
    poj 1251Jungle Roads (最小生成树:prime+krusual)
    poj 2677 Tour (最短路)
    poj 2485Highways (求最长路)
    一位ACMer过来人的心得
    poj 2395 Out of Hay (最小生成树的最大边)
    poj 1703 Find them, Catch them (并查集)
    poj 1308 Is It A Tree? (最小生成树)
    poj1258 AgriNet (最小生成树prime)
    poj1789 Truck History (最小生成树)
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/6435712.html
Copyright © 2011-2022 走看看