zoukankan      html  css  js  c++  java
  • 洛谷P1919 (模板)A*B Problem升级版(FFT快速傅里叶)

    #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 = 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];
    
    void solve()
    {
        string a, b;
        cin >> a >> b;
    
        n = a.size() - 1, m = b.size() - 1;
    
        int len = 1;
        while(len <= n + m + 1) len <<= 1;
    
        for(int i = 0; i <= n; i ++) x1[i] = Complex(a[n - i] - '0', 0);
    
        for(int i = 0; i <= m; i ++) x2[i] = Complex(b[m - i] - '0', 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 = 0; i < len; i ++){
            ans[i] = (int)(x1[i].x + 0.5); 
        }
    
        for(int i = 0; i < len; i ++){
            ans[i + 1] += ans[i] / 10;
            ans[i] %= 10;
        }
    
        while(ans[len] == 0 && len > 0) len --;
    
        for(int i = len; i >= 0; i --){
            cout << ans[i];
        }
        puts("");
    
    }
    
    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;
    }
    

      

  • 相关阅读:
    CMDB开发
    运维相关开源项目
    SendMessage,BroadcastMessage
    C# System.IO.Path
    Unity[C#] Reflection Use
    Unity By Reflection Update Scripts
    Dictionary CovertTo List
    Unity 脚本的未来发展
    使用Doxygen生成C#帮助文档
    Spine Skeleton Animation(2D骨骼动画)
  • 原文地址:https://www.cnblogs.com/jungu/p/14367924.html
Copyright © 2011-2022 走看看