FFT Algorithm

There are many different FFT algorithms based on a wide range of published theory, from simple complex-number arithmetical to group theory and number theory. The best-known FFT algorithms depend upon the factorization of N, but there are FFTs with O(N log N) complexity for all N, even for prime As Tukey make not work at IBM, the patentability of the idea was doubted and the algorithm go into the public domain, which, through the computing revolution of the next ten, made FFT one of the indispensable algorithms in digital signal processing. In discussion with Tukey, Richard Garwin recognized the general applicability of the algorithm not exactly to national security problems, but also to a wide range of problems including one of immediate interest to him, determine the periodicities of the spin orientations in a 3D crystal of Helium-3.James Cooley and John Tukey published a more general version of FFT in 1965 that is applicable when N is composite and not inevitably a power of 2.
/********************************************************************************

    Fast Fourier transformation used to multiply long numbers.
    Fast non-recursive version. O(NlogN).
    Based on problem 317 from E-Olimp: http://www.e-olimp.com.ua/problems/317

********************************************************************************/

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
#include <complex>

using namespace std;

typedef complex<double> comp;
const double pi = acos(-1.0);
const int MAXN = 505000;

int rev[MAXN * 2];

void fft( comp p[], int n, bool invert) {
    int dig = 0;    
    while ((1 << dig) < n)
        dig++;

    for (int i = 0; i < n; i++) {
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (dig - 1));
        if (rev[i] > i)
            swap(p[i], p[rev[i]]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double angle = 2 * pi / len;
        if (invert)
            angle *= -1;
        comp wgo(cos(angle), sin(angle));
        for (int i = 0; i < n; i += len) {
            comp w(1);
            for (int j = 0; j < (len >> 1); j++) {
                comp a = p[i + j], b = w * p[i + j  + (len >> 1)];
                p[i + j] = a + b;
                p[i + j + (len >> 1)] = a - b;
                w *= wgo;
            }
        }
    }
    if (invert)
        for (int i = 0; i < n; i++)
            p[i] /= n;  
}

comp a[MAXN * 2], b[MAXN * 2];
int ans[MAXN * 2];
int alen, blen, total;

void readPolynom(comp p[], int &len, bool space) {
    string s;
    if (space)
        getline(cin, s, ' ');
    else
        getline(cin, s);
    len = 1;
    while (len < (int) s.length())
        len *= 2;

    memset(p, 0, sizeof(p));

    int pos = 0;
    for (int i = (int) s.length() - 1; i >= 0; i--) {
        comp coeff(s[i] - '0');
        p[pos] = coeff;
        pos++;
    }
}

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    readPolynom(a, alen, true);
    readPolynom(b, blen, false);

    total = max(alen, blen);
    total *= 2;

    fft(a, total, false);
    fft(b, total, false);

    for (int i = 0; i < total; i++)
        a[i] = a[i] * b[i];

    fft(a, total, true);

    for (int i = 0; i < total; i++)
        ans[i] = (int) floor(a[i].real() + 0.5);

    int go = 0;
    for (int i = 0; i < total; i++) {
        ans[i] += go;
        go = ans[i] / 10;
        ans[i] %= 10;
    }

    int ans_ind = total - 1;
    while (ans_ind > 0 && ans[ans_ind] == 0)
        ans_ind--;

    for (int i = ans_ind; i >= 0; i--)
        printf("%d", ans[i]);
    printf("\n");

    return 0;
}

LANGUAGE:

DARK MODE: