P1919 FFT加速高精度乘法

2022-05-21 02:11:14 字數 1856 閱讀 8303

傳送門:

給出兩個n位10進製整數x和y,你需要計算x*y。

對於十進位制數我們可以將其轉換成

\(a0*10^0+a1*10^1+a2*10^2...an*10^n\)

那麼對於兩個數,我們就可以求出兩個的係數表示後得到a的點乘式和b的點乘式

最後得到的答案就是a和b的多項式的係數,這個問題o(n)掃一遍,

處理一下輸出即可

#include #include #include #include #include #include #include #include #include using namespace std;

typedef long long ll;

typedef pairpii;

typedef unsigned long long ull;

#define ls rt<<1

#define rs rt<<1|1

#define lson l,mid,rt<<1

#define rson mid+1,r,rt<<1|1

#define bug printf("*********\n")

#define fin freopen("input.txt","r",stdin);

#define fon freopen("output.txt","w+",stdout);

#define io ios::sync_with_stdio(false),cin.tie(0)

#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"

#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"

#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<>= 1;

} return ans;

}struct complex

} a[maxn], b[maxn];

complex operator + (complex a, complex b)

complex operator - (complex a, complex b)

complex operator * (complex a, complex b)

int n, m;

int l, r[maxn];

int limit = 1;

void fft(complex *a, int type)

for(int mid = 1; mid < limit; mid <<= 1) }}

}int ans[maxn];

char numa[maxn], numb[maxn];

int main()

for(int i = n - 1; i >= 0; i--)

while(limit < n + n) limit <<= 1, l++;

for(int i = 0; i <= limit; i++)

fft(a, 1);

fft(b, 1);

for(int i = 0; i <= limit; i++)

fft(a, -1);

int tot = 0;

for(int i = 0; i <= limit; i++)

}while(!ans[limit] && limit >= 1) limit--;

limit++;

while(--limit >= 0) cout << ans[limit];

}return 0;

}

FFT高精度乘法

眾所周知,fft fast fourier transformation 即為快速傅氏變換,是離散傅氏變換的快速演算法,它是根據離散傅氏變換的奇 偶 虛 實等特性,對離散傅利葉變換的演算法進行改進獲得的.那麼我們用這個nb的演算法解決一些什麼沙雕問題呢qwq?沒錯!就是大名鼎鼎的高精度乘法 fft太...

高精度乘法 FFT

學會了fft之後感覺自己征服了世界 當然是幻覺.不過fft還是很有用的,在優化大規模的動規問題的時候有極大效果.一般比較 的計數動規題都需要fft n 1e9 下面是高精度乘法的板子.include include include include include include include in...

模板 FFT與NTT高精度乘法模板

fft高精度乘法 與ntt高精度乘法 搞得不甚明了,不予解釋 fft include include include includeusing namespace std const int maxn 300000 const double pi 3.1415926535 typedef compl...