这两天刚开始看FFT,有点云里雾里的,今天看了个模板题,觉得这位聚聚写得特别好,总算是看懂了多项式相乘之类的运算,存一下。
http://blog.csdn.net/sdj222555/article/details/9786527
好点复杂一点的算法,数据结构都不知道,还要慢慢学习。
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) { int ret = 0; for (int i = 0; i < bits; i++) { ret <<= 1; ret |= x & 1; x >>= 1; } return ret; } void fft(double * a, double * b, int n, bool rev) { int bits = 0; while (1 << bits < n) ++bits; for (int i = 0; i < n; i++) { int j = revv(i, bits); if (i < j) swap(a[i], a[j]), swap(b[i], b[j]); } for (int len = 2; len <= n; len <<= 1) { int half = len >> 1; double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len); if (rev) wmy = -wmy; for (int i = 0; i < n; i += len) { double wx = 1, wy = 0; for (int j = 0; j < half; j++) { double cx = a[i + j], cy = b[i + j]; double dx = a[i + j + half], dy = b[i + j + half]; double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx; a[i + j] = cx + ex, b[i + j] = cy + ey; a[i + j + half] = cx - ex, b[i + j + half] = cy - ey; double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx; wx = wnx, wy = wny; } } } if (rev) { for (int i = 0; i < n; i++) a[i] /= n, b[i] /= n; } } int solve(int a[],int na,int b[],int nb,int ans[]) //两个数组求卷积,有时ans数组要开成long long { int len = max(na, nb), ln; for(ln=0; L(ln)<len; ++ln); len=L(++ln); for (int i = 0; i < len ; ++i) { if (i >= na) ax[i] = 0, ay[i] =0; else ax[i] = a[i], ay[i] = 0; } fft(ax, ay, len, 0); for (int i = 0; i < len; ++i) { if (i >= nb) bx[i] = 0, by[i] = 0; else bx[i] = b[i], by[i] = 0; } fft(bx, by, len, 0); for (int i = 0; i < len; ++i) { double cx = ax[i] * bx[i] - ay[i] * by[i]; double cy = ax[i] * by[i] + ay[i] * bx[i]; ax[i] = cx, ay[i] = cy; } 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 { int len = na, ln; for(ln = 0; L(ln) < na; ++ln); len=L(++ln); for(int i = 0; i < len; ++i) { if (i >= na) ax[i] = 0, ay[i] = 0; else ax[i] = a[i], ay[i] = 0; } fft(ax, ay, len, 0); for(int i=0; i<len; ++i) { double cx = ax[i] * ax[i] - ay[i] * ay[i]; double cy = 2 * ax[i] * ay[i]; ax[i] = cx, ay[i] = cy; } fft(ax, ay, len, 1); for(int i=0; i<len; ++i) ans[i] = ax[i] + 0.5; return len; } 大数相乘模板(按照FFT的规则,低位要放在前面,其实多项式相乘就是多项式系数的相乘,而多项式系数的相乘跟我们这里的大数相乘就是一样的东西了,最后再倒序输出就好) #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <map> #include <queue> #include <set> #include <vector> 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) { int ret = 0; for (int i = 0; i < bits; i++) { ret <<= 1; ret |= x & 1; x >>= 1; } return ret; } void fft(double * a, double * b, int n, bool rev) { int bits = 0; while (1 << bits < n) ++bits; for (int i = 0; i < n; i++) { int j = revv(i, bits); if (i < j) swap(a[i], a[j]), swap(b[i], b[j]); } for (int len = 2; len <= n; len <<= 1) { int half = len >> 1; double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len); if (rev) wmy = -wmy; for (int i = 0; i < n; i += len) { double wx = 1, wy = 0; for (int j = 0; j < half; j++) { double cx = a[i + j], cy = b[i + j]; double dx = a[i + j + half], dy = b[i + j + half]; double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx; a[i + j] = cx + ex, b[i + j] = cy + ey; a[i + j + half] = cx - ex, b[i + j + half] = cy - ey; double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx; wx = wnx, wy = wny; } } } if (rev) { for (int i = 0; i < n; i++) a[i] /= n, b[i] /= n; } } int solve(int a[],int na,int b[],int nb,int ans[]) { int len = max(na, nb), ln; for(ln=0; L(ln)<len; ++ln); len=L(++ln); for (int i = 0; i < len ; ++i) { if (i >= na) ax[i] = 0, ay[i] =0; else ax[i] = a[i], ay[i] = 0; } fft(ax, ay, len, 0); for (int i = 0; i < len; ++i) { if (i >= nb) bx[i] = 0, by[i] = 0; else bx[i] = b[i], by[i] = 0; } fft(bx, by, len, 0); for (int i = 0; i < len; ++i) { double cx = ax[i] * bx[i] - ay[i] * by[i]; double cy = ax[i] * by[i] + ay[i] * bx[i]; ax[i] = cx, ay[i] = cy; } fft(ax, ay, len, 1); for (int i = 0; i < len; ++i) ans[i] = (int)(ax[i] + 0.5); return len; } int main() { int l1,l2,l; int i; while(gets(sa)) { gets(sb); memset(sum, 0, sizeof(sum)); l1 = strlen(sa); l2 = strlen(sb); for(i = 0; i < l1; i++) x1[i] = sa[l1 - i - 1]-'0'; for(i = 0; i < l2; i++) x2[i] = sb[l2-i-1]-'0'; l = solve(x1, l1, x2, l2, sum); for(i = 0; i<l || sum[i] >= 10; i++) // 进位 { sum[i + 1] += sum[i] / 10; sum[i] %= 10; } l = i; while(sum[l] <= 0 && l>0) l--; // 检索最高位 for(i = l; i >= 0; i--) putchar(sum[i] + '0'); // 倒序输出 putchar('\n'); } return 0; }