zoukankan      html  css  js  c++  java
  • hdu 1402 A * B Problem Plus fft

    题目链接

    A * B Problem Plus

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 16382    Accepted Submission(s): 3325


    Problem Description
    Calculate A * B.
     
    Input
    Each line will contain two integers A and B. Process to end of file.

    Note: the length of each integer will not exceed 50000.
     
    Output
    For each case, output A * B in one line.
     
    Sample Input
    1 2 1000 2
     
    Sample Output
    2 2000
     
     
    fft模板题。
     
    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <complex>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    typedef complex <double> cmx;
    void change(cmx y[],int len)
    {
        int i,j,k;
        for(i = 1, j = len/2;i < len-1; i++)
        {
            if(i < j)
                swap(y[i], y[j]);
            k = len / 2;
            while( j >= k)
            {
                j -= k;
                k /= 2;
            }
            if(j < k) j += k;
        }
    }
    void fft(cmx y[],int len,int on)
    {
        change(y,len);
        for(int h = 2; h <= len; h <<= 1)
        {
            cmx wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
            for(int j = 0;j < len;j+=h)
            {
                cmx w(1,0);
                for(int k = j;k < j+h/2;k++)
                {
                    cmx u = y[k];
                    cmx 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] /= len;
    }
    const int maxn = 200010;
    int ans[maxn];
    cmx x1[maxn], x2[maxn];
    char s1[50005], s2[50005];
    int main()
    {
        while(~scanf("%s%s", s1, s2)) {
            int len1 = strlen(s1);
            int len2 = strlen(s2);
            mem(ans);
            int len = 1;
            while(len<len1*2 || len<len2*2)
                len<<=1;
            for(int i = 0; i<len1; i++) {
                x1[i] = cmx(s1[len1-i-1]-'0', 0);
            }
            for(int i = len1; i<len; i++)
                x1[i] = cmx(0, 0);
            for(int i = 0; i<len2; i++) {
                x2[i] = cmx(s2[len2-i-1]-'0', 0);
            }
            for(int i = len2; i<len; i++)
                x2[i] = cmx(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].real() + 0.5);
            for(int i = 0; i<len; i++) {
                ans[i+1] += ans[i]/10;
                ans[i]%=10;
            }
            len = len1+len2-1;
            while(len>0&&ans[len]==0)
                len--;
            for(int i = len; i>=0; i--)
                printf("%d", ans[i]);
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    题解 AT5228 【[ABC162A] Lucky 7】
    题解 P6467 【[COCI2008-2009#6] BUKA】
    2020 Codeforces 愚人节比赛题解 A~D
    题解 AT4251 【[ABC110A] Maximize the Formula】
    题解 AT5638 【November 30】
    题解 AT4164 【[ABC102A] Multiple of 2 and N】
    多项式全家桶
    烂题推荐
    NOIP 2020 游记
    P5048 题解
  • 原文地址:https://www.cnblogs.com/yohaha/p/5234521.html
Copyright © 2011-2022 走看看