Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct number {
- double x, y;
- number() {}
- number(double a, double b) :
- x(a),
- y(b) {}
- };
- number operator+(const number a, const number b) {
- return {a.x + b.x, a.y + b.y};
- }
- number operator-(const number a, const number b) {
- return {a.x - b.x, a.y - b.y};
- }
- number operator*(const number a, const number b) {
- return {a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x};
- }
- ostream &operator<<(ostream &out, const number a) {
- return out << a.x << " " << a.y;
- }
- const int N = 4;
- number root[N];
- void init_fft() {
- for (int i = 0; i < N; ++i) {
- double w = (double)i / N;
- root[i] = {cos(w), sin(w)};
- }
- }
- vector<number> fft(vector<number> a) {
- if (a.size() == 1) {
- return a;
- }
- assert(a.size());
- vector<number> a0, a1;
- a0.reserve(a.size() / 2);
- a1.reserve(a.size() / 2);
- for (int i = 0; i < (int)a.size(); ++i) {
- if (i & 1) {
- a1.push_back(a[i]);
- } else {
- a0.push_back(a[i]);
- }
- }
- a0 = fft(a0);
- a1 = fft(a1);
- int scale = N / (int)a.size();
- int half = (int)a.size() / 2;
- for (int i = 0; i < half; ++i) {
- a[i] = a0[i] + root[i * scale] * a1[i];
- a[i + half] = a0[i] - root[i * scale] * a1[i];
- }
- return a;
- }
- number a[N];
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout << fixed << setprecision(1);
- cerr << fixed << setprecision(1);
- a[0] = {1, 0};
- clock_t st = clock();
- init_fft();
- vector<number> f = fft({a, a + N});
- clock_t fn = clock();
- cout << "---------" << endl;
- for (int i = 0; i < N; ++i) {
- cout << f[i] << endl;
- }
- cout << endl;
- cout << 1000 * (fn - st) / CLOCKS_PER_SEC << " ms\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement