传送门:点击打开链接
题意:求大数乘法
思路:fft套模板
#include <map> #include <set> #include <cmath> #include <ctime> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <bitset> #include <string> #include <vector> #include <cstring> #include <iostream> #include <algorithm> #include <functional> #define fuck(x) cout<<"["<<x<<"]"; #define FIN freopen("input.txt","r",stdin); #define FOUT freopen("output.txt","w+",stdout); //#pragma comment(linker, "/STACK:102400000,102400000") using namespace std; typedef long long LL; typedef pair<int, int> PII; const int MX = 3e5 + 5; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const double pi = acos(-1.0); struct cp { double x, y; cp() {} cp (double x, double y): x(x), y(y) {} inline cp operator + (const cp &b) { return cp(x + b.x, y + b.y); } inline cp operator - (const cp &b) { return cp(x - b.x, y - b.y); } inline cp operator * (const cp &b) { return cp(x * b.x - y * b.y, x * b.y + y * b.x); } } a[MX], b[MX]; int r[MX], ans[MX]; void fft(cp a[], int opt, int n) { for(int i = 0; i < n; i++) { if(i < r[i]) swap(a[i], a[r[i]]); } for(int i = 1; i < n; i <<= 1) { cp wn(cos(pi / i), opt * sin(pi / i)); for(int p = i << 1, j = 0; j < n; j += p) { cp w(1, 0); for(int k = 0; k < i; k++, w = wn * w) { cp x = a[j + k], y = w * a[j + k + i]; a[j + k] = x + y; a[j + k + i] = x - y; } } } } void solve(cp a[], cp b[], int n, int m) { int l = 0, nn, nm = n + m; for(nn = 1; nn <= nm; nn <<= 1) l++; for(int i = n + 1; i <= nn; i++) a[i] = cp(0, 0); for(int i = m + 1; i <= nn; i++) b[i] = cp(0, 0); n = nn; m = nm; for(int i = 0; i < n; i++) { r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1)); } fft(a, 1, n); fft(b, 1, n); for(int i = 0; i <= n; i++) { a[i] = a[i] * b[i]; } fft(a, -1, n); for(int i = 0; i <= m; i++) { a[i].x /= n; } } char A[MX], B[MX]; int main() { //FIN; while(~scanf("%s%s", A, B)) { int n = strlen(A), m = strlen(B); n--; m--; for(int i = 0; i <= n; i++) a[i] = cp(A[n - i] - '0', 0); for(int i = 0; i <= m; i++) b[i] = cp(B[m - i] - '0', 0); solve(a, b, n, m); int sz = 0, add = 0; for(sz = 0; add || sz <= n + m; sz++) { int t = add; if(sz <= n + m) t += a[sz].x + 0.5; ans[sz] = t % 10; add = t / 10; } while(sz - 1 > 0 && !ans[sz - 1]) sz--; for(int i = sz - 1; i >= 0; i--) { printf("%d", ans[i]); } printf("\n"); } return 0; }