HDU 1402 FFT模板題,求大數乘法

2021-07-23 06:20:45 字數 1955 閱讀 2147

這兩天剛開始看fft,有點雲裡霧裡的,今天看了個模板題,覺得這位聚聚寫得特別好,總算是看懂了多項式相乘之類的運算,存一下。

好點複雜一點的演算法,資料結構都不知道,還要慢慢學習。

fft模板:

#define l(x) (1 << (x))

const double pi = acos(-1.0);

const int maxn = 133015;

double ax[maxn], ay[maxn], bx[maxn], by[maxn];

int revv(int x, int bits)

return ret;

}void fft(double * a, double * b, int n, bool rev)

for (int len = 2; len <= n; len <<= 1)}}

if (rev)

}int solve(int a,int na,int b,int nb,int ans) //兩個陣列求卷積,有時ans陣列要開成long long

fft(ax, ay, len, 0);

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

fft(bx, by, len, 0);

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

fft(ax, ay, len, 1);

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

ans[i] = (int)(ax[i] + 0.5);

return len;

}int solve(long long a, int na, int ans) //自己跟自己求卷積,有時候ans陣列要開成long long

fft(ax, ay, len, 0);

for(int i=0; i

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

#define l(x) (1 << (x))

const double pi = acos(-1.0);

const int maxn = 133015;

double ax[maxn], ay[maxn], bx[maxn], by[maxn];

char sa[maxn/2],sb[maxn/2];

int sum[maxn];

int x1[maxn],x2[maxn];

int revv(int x, int bits)

return ret;

}void fft(double * a, double * b, int n, bool rev)

for (int len = 2; len <= n; len <<= 1)}}

if (rev)

}int solve(int a,int na,int b,int nb,int ans)

fft(ax, ay, len, 0);

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

fft(bx, by, len, 0);

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

fft(ax, ay, len, 1);

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

ans[i] = (int)(ax[i] + 0.5);

return len;

}int main()

l = i;

while(sum[l] <= 0 && l>0) l--; // 檢索最高位

for(i = l; i >= 0; i--) putchar(sum[i] + '0'); // 倒序輸出

putchar('\n');

}return 0;

}

HDU 1402 大數乘法 FFT

大整數乘法,給定兩個長度不超過10000的整數,返回乘法的結果。char multi char number a,char number b 有疑問歡迎提問,本人學通訊的,手上有 數字訊號處理 書,可以把圖搬出來解答的 include stdafx.h include include include...

FFT的迭代程式實現 hdu1402

快速傅利葉變換fft的迭代實現 描述了最簡單的fft的迭代實現,在此基礎上可以用它進行大整數乘法或者多項式乘法。不過,還需要考慮idft的快速實現。idft有2種實現方式。第一種仿照fft,觀察idft的定義式,和dft的定義本質上沒有區別,利用單位復根的性質可以寫出ifft。第二種方法則利用共軛的...

劃分樹模板 模板題 hdu4251

劃分樹解決的是快速求區間中第k大值的問題,演算法的主要思想是基於線段樹和快排的劃分方法,可以實現在logn時間內求出任意區間的第k大值。下面這份 是基於hud4251的乙份模板。如下 include include include include using namespace std const ...