本文主要是介绍【HDU】1402 A * B Problem Plus 【FFT】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门:【HDU】1402 A * B Problem Plus
题目分析:
这就是大数乘法题,问两个大数相乘的结果,由于O(n2)的算法复杂度太大,所以我们用FFT来优化他。关于FFT网上资料很多,我就不多说啦。
这是我做的第一道FFT,FFT是看算法导论学来的,前面几篇文章是从july大神那边转载来的,感觉都讲的很不错,简单易懂~
// whn6325689
// Mr.Phoebe
// http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
#define speed std::ios::sync_with_stdio(false);typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;#define CLR(x,y) memset(x,y,sizeof(x))
#define CPY(x,y) memcpy(x,y,sizeof(x))
#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))
#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))#define MID(x,y) (x+((y-x)>>1))
#define ls (idx<<1)
#define rs (idx<<1|1)
#define lson ls,l,mid
#define rson rs,mid+1,r
#define root 1,1,ntemplate<class T>
inline bool read(T &n)
{T x = 0, tmp = 1;char c = getchar();while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();if(c == EOF) return false;if(c == '-') c = getchar(), tmp = -1;while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();n = x*tmp;return true;
}
template <class T>
inline void write(T n)
{if(n < 0){putchar('-');n = -n;}int len = 0,data[20];while(n){data[len++] = n%10;n /= 10;}if(!len) data[len++] = 0;while(len--) putchar(data[len]+48);
}
//-----------------------------------const int MAXN=200010;struct Complex
{double r,i;Complex(){}Complex(double r ,double i):r(r),i(i) {}Complex operator + (const Complex& t) const{return Complex(r+t.r,i+t.i) ;}Complex operator - (const Complex& t) const{return Complex(r-t.r,i-t.i);}Complex operator * (const Complex& t) const{return Complex(r*t.r-i*t.i,r*t.i+i*t.r);}
} ;void FFT(Complex y[],int n,int rev)//rev=-1表示逆变换
{for(int i=1,j,k,t; i<n; i++) //进行蝶型变换{for(j=0,k=n>>1,t=i; k; k>>=1,t>>=1) j=j<<1|t&1;if(i<j ) swap(y[i],y[j]);}for ( int s = 2 , ds = 1 ; s <= n ; ds = s , s <<= 1 ){Complex wn=Complex(cos(rev*2*PI/s),sin(rev*2*PI/s)),w=Complex(1,0),t;for(int k=0; k<ds; k++,w=w*wn){for(int i=k; i<n; i+=s){y[i+ds]=y[i]-(t=w*y[i+ds]);y[i]=y[i]+t;}}}if(rev==-1) for(int i=0; i<n; i++) y[i].r/=n;
}char s1[MAXN],s2[MAXN];
Complex x1[MAXN],x2[MAXN];
int num[MAXN];int main()
{while(~scanf("%s%s",s1,s2)){int n1=strlen(s1);int n2=strlen(s2);int n=1;while(n<n1+n2) n<<=1; //进行FFT的级数大小for(int i=0; i<n1; i++) x1[i]=Complex(s1[n1-i-1]-'0',0); //初始化数组for(int i=n1; i<n; i++) x1[i]=Complex(0,0);for(int i=0 ; i<n2 ; i++) x2[i]=Complex(s2[n2-i-1]-'0',0) ;for(int i=n2; i<n; i++) x2[i]=Complex(0,0);FFT(x1,n,1);FFT(x2,n,1);for(int i=0; i<n; i++) x1[i]=x1[i]*x2[i];FFT(x1,n,-1);int t=0;for(int i=0; i<n; i++,t/=10){t+=(int)(x1[i].r+0.1);num[i]=t%10;}for(; t; t/=10) num[n++]=t%10;while(n>1 && !num[n-1]) --n;for(int i=n-1; i>=0; i--) printf("%d",num[i]);printf("\n");}return 0 ;
}
这篇关于【HDU】1402 A * B Problem Plus 【FFT】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!