给出两个n位
FFT模板题
自己写了一个十分简陋功能不全的complex
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <complex>
using namespace std;
//typedef complex<double> P;
struct P {
double r, v;
inline double real(void) {
return r;
}
inline P(double _r = 0, double _v = 0):r(_r), v(_v) {}
inline P operator =(double a) {
this->r = a; this->v = 0; return *this;
}
inline P operator *(const P &a) {
P c(r * a.r - v * a.v, r * a.v + v * a.r);
return c;
}
inline P operator +(const P &a) {
return P(r + a.r, v + a.v);
}
inline P operator -(const P &a) {
return P(r - a.r, v - a.v);
}
inline P operator *=(const P &a) {
*this = *this * a; return *this;
}
inline P operator /(double a) {
return P(r / a, v / a);
}
inline P operator /=(double a) {
*this = *this / a; return *this;
}
};
const int N = 140100;
const double pi = acos(-1);
int n, L, m;
int Rev[N], c[N];
P a[N], b[N];
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
inline void read(int &x) {
static char c; x = 0;
for (c = get(); c < '0' || c > '9'; c = get());
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
inline char GetNum(void) {
static char c;
for (c = get(); c < '0' || c > '9'; c = get());
return c;
}
void FFT(P *a, int f) {
for (int i = 0; i < n; i++)
if (Rev[i] > i) swap(a[i], a[Rev[i]]);
for (int i = 1; i < n; i <<= 1) {
P wn(cos(pi / i), f * sin(pi / i));
for (int j = 0; j < n; j += (i << 1)) {
P w(1, 0);
for (int k = 0; k < i; k++) {
P x = a[j + k], y = w * a[j + i + k];
a[j + k] = x + y; a[j + i + k] = x - y;
w *= wn;
}
}
}
if (f == -1) for (int i = 0; i < n; i++) a[i] /= n;
}
int main(void) {
read(n); n--; m = 2 * n;
for (int i = 0; i <= n; i++) a[n - i] = GetNum() - '0';
for (int i = 0; i <= n; i++) b[n - i] = GetNum() - '0';
for (n = 1; n <= m; n <<= 1) L++;
for (int i = 1; i < n; i++)
Rev[i] = (Rev[i >> 1] >> 1) | ((i & 1) << (L - 1));
FFT(a, 1); FFT(b, 1);
for (int i = 0; i <= n; i++) a[i] *= b[i];
FFT(a, -1);
for (int i = 0; i <= m; i++) c[i] = int(a[i].real() + 0.1);
for (int i = 0; i <= m; i++)
if (c[i] >= 10) {
c[i + 1] += c[i] / 10;
c[i] %= 10;
if (i == m) m++;
}
for (int i = m; i >= 0; i--) putchar(c[i] + '0');
return 0;
}