洛谷P2553 [AHOI2001]多项式乘法
板子题,处理一下输入输出即可
Code:
#include <map> #include <set> #include <array> #include <queue> #include <stack> #include <cmath> #include <vector> #include <cstdio> #include <cstring> #include <sstream> #include <iostream> #include <stdlib.h> #include <algorithm> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> PII; #define sd(a) scanf("%d", &a) #define sdd(a, b) scanf("%d%d", &a, &b) #define slld(a) scanf("%lld", &a) #define slldd(a, b) scanf("%lld%lld", &a, &b) const int N = 100;//3e6 + 10; const int M = 1e6 + 20; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const double PI = acos(-1.0); int n, m; ll ans[N]; int rev[N]; struct Complex{ double x, y; Complex(double _x = 0.0, double _y = 0.0){ x = _x, y = _y; } Complex operator + (const Complex & b){ return Complex(x + b.x, y + b.y); } Complex operator - (const Complex &b){ return Complex(x - b.x, y - b.y); } Complex operator * (const Complex &b){ return Complex(x * b.x - y * b.y, x * b.y + y * b.x); } }; void change(Complex y[], int len){ for(int i = 0; i < len; i ++){ rev[i] = rev[i >> 1] >> 1; if(i & 1) rev[i] |= (len >> 1); } for(int i = 0; i < len; i ++){ if(i < rev[i]) swap(y[i], y[rev[i]]); } } void fft(Complex y[], int len, int on){ change(y, len); for(int h = 2; h <= len; h <<= 1){ Complex wn(cos(2 * PI / h), sin(2 * PI * on / h)); for(int j = 0; j < len; j += h){ Complex w(1, 0); for(int k = j; k < j + h / 2; k ++){ Complex u = y[k]; Complex t = w * y[k + h / 2]; y[k] = u + t; y[k + h / 2] = u - t; w = w * wn; } } } if(on == -1){ for(int i = 0; i < len; i ++){ y[i].x /= len; } } } Complex x1[N], x2[N]; int a[N], b[N]; void solve() { string s; while(cin >> s){ int i = 1; n = 0, m = 0; while(s[i] != ')'){ int x = 0; while(s[i] != 'a' && s[i] != ')' && s[i] != '+'){ x = x * 10 + s[i] - '0'; i ++; } n ++; if(s[i] != ')' && s[i] != '+'){ i += 2; int y = 0; while(s[i] != '+' && s[i] != ')'){ y = y * 10 + s[i] - '0'; i ++; } a[y] = x; } else a[0] = x; if(s[i] == '+') i ++; } i += 3; while(s[i] != ')'){ int x = 0; while(s[i] != 'a' && s[i] != ')' && s[i] != '+'){ x = x * 10 + s[i] - '0'; i ++; } m ++; if(s[i] != ')' && s[i] != '+'){ i += 2; int y = 0; while(s[i] != '+' && s[i] != ')'){ y = y * 10 + s[i] - '0'; i ++; } b[y] = x; } else{ b[0] = x; } if(s[i] == '+') i ++; } n --, m --; int len = 1; while(len <= n + m + 1) len <<= 1; for(int i = 0; i <= n; i ++) x1[i] = Complex(a[i], 0); for(int i = 0; i <= m; i ++) x2[i] = Complex(b[i], 0); for(int i = n + 1; i < len; i ++) x1[i] = Complex(0, 0); for(int i = m + 1; i < len; i ++) x2[i] = Complex(0, 0); fft(x1, len, 1); fft(x2, len, 1); for(int i = 0; i < len ; i ++) x1[i] = x1[i] * x2[i]; fft(x1, len, -1); for(int i = n + m; i >= 1; i --){ cout << (int)(x1[i].x + 0.5) << "a^" << i << "+"; } cout << (int)(x1[0].x + 0.5) << "\n"; } } int main() { #ifdef ONLINE_JUDGE #else freopen("/home/jungu/code/in.txt", "r", stdin); // freopen("/home/jungu/桌面/11.21/2/in9.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T = 1; // sd(T); // cin >> T; while (T--) { solve(); } return 0; }