日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 1402 A * B Problem Plus FFT

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 1402 A * B Problem Plus FFT 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

A * B Problem Plus

題目連接:

http://acm.hdu.edu.cn/showproblem.php?pid=1402

Description

Calculate A * B.

Input

Each line will contain two integers A and B. Process to end of file.

Note: the length of each integer will not exceed 50000.

Output

For each case, output A * B in one line.

Sample Input

1
2
1000
2

Sample Output

2
2000

Hint

題意

題解:

考慮變成系數的形式,顯然就是兩個的多項式乘法

然后轉化成FFT,直接莽一波就完了。

代碼

#include<cstdio> #include<cmath> #include<cstring> #include<algorithm>using namespace std;const int N = 500005; const double pi = acos(-1.0);char s1[N],s2[N]; int len,res[N];struct Complex {double r,i;Complex(double r=0,double i=0):r(r),i(i) {};Complex operator+(const Complex &rhs){return Complex(r + rhs.r,i + rhs.i);}Complex operator-(const Complex &rhs){return Complex(r - rhs.r,i - rhs.i);}Complex operator*(const Complex &rhs){return Complex(r*rhs.r - i*rhs.i,i*rhs.r + r*rhs.i);} } va[N],vb[N];void rader(Complex F[],int len) //len = 2^M,reverse F[i] with F[j] j為i二進制反轉 {int j = len >> 1;for(int i = 1;i < len - 1;++i){if(i < j) swap(F[i],F[j]); // reverseint k = len>>1;while(j>=k){j -= k;k >>= 1;}if(j < k) j += k;} }void FFT(Complex F[],int len,int t) {rader(F,len);for(int h=2;h<=len;h<<=1){Complex wn(cos(-t*2*pi/h),sin(-t*2*pi/h));for(int j=0;j<len;j+=h){Complex E(1,0); //旋轉因子for(int k=j;k<j+h/2;++k){Complex u = F[k];Complex v = E*F[k+h/2];F[k] = u+v;F[k+h/2] = u-v;E=E*wn;}}}if(t==-1) //IDFTfor(int i=0;i<len;++i)F[i].r/=len; }void Conv(Complex a[],Complex b[],int len) //求卷積 {FFT(a,len,1);FFT(b,len,1);for(int i=0;i<len;++i) a[i] = a[i]*b[i];FFT(a,len,-1); }void init(char *s1,char *s2) {int n1 = strlen(s1),n2 = strlen(s2);len = 1;while(len < 2*n1 || len < 2*n2) len <<= 1;int i;for(i=0;i<n1;++i){va[i].r = s1[n1-i-1]-'0';va[i].i = 0;}while(i<len){va[i].r = va[i].i = 0;++i;}for(i=0;i<n2;++i){vb[i].r = s2[n2-i-1]-'0';vb[i].i = 0;}while(i<len){vb[i].r = vb[i].i = 0;++i;} }void gao() {Conv(va,vb,len);memset(res,0,sizeof res);for(int i=0;i<len;++i){res[i]=va[i].r + 0.5;}for(int i=0;i<len;++i){res[i+1]+=res[i]/10;res[i]%=10;}int high = 0;for(int i=len-1;i>=0;--i){if(res[i]){high = i;break;}}for(int i=high;i>=0;--i) putchar('0'+res[i]);puts(""); }int main() {while(scanf("%s %s",s1,s2)==2){init(s1,s2);gao();}return 0; }

轉載于:https://www.cnblogs.com/qscqesze/p/5379677.html

總結

以上是生活随笔為你收集整理的HDU 1402 A * B Problem Plus FFT的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。