zoukankan      html  css  js  c++  java
  • HDU 5730 Shell Necklace

    分治,$FFT$。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    const double pi=acos(-1.0),eps=1e-8;
    void File()
    {
        freopen("D:\in.txt","r",stdin);
        freopen("D:\out.txt","w",stdout);
    }
    template <class T>
    inline void read(T &x)
    {
        char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
        while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar();  }
    }
    
    #define L(x) (1 << (x))
    const double PI = acos(-1.0);
    const int Maxn = 300010;
    double ax[Maxn], ay[Maxn], bx[Maxn], by[Maxn];
    int revv(int x, int bits)
    {
        int ret = 0;
        for (int i = 0; i < bits; i++)
        {
            ret <<= 1;
            ret |= x & 1;
            x >>= 1;
        }
        return ret;
    }
    void fft(double * a, double * b, int n, bool rev)
    {
        int bits = 0;
        while (1 << bits < n) ++bits;
        for (int i = 0; i < n; i++)
        {
            int j = revv(i, bits);
            if (i < j)
                swap(a[i], a[j]), swap(b[i], b[j]);
        }
        for (int len = 2; len <= n; len <<= 1)
        {
            int half = len >> 1;
            double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len);
            if (rev) wmy = -wmy;
            for (int i = 0; i < n; i += len)
            {
                double wx = 1, wy = 0;
                for (int j = 0; j < half; j++)
                {
                    double cx = a[i + j], cy = b[i + j];
                    double dx = a[i + j + half], dy = b[i + j + half];
                    double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx;
                    a[i + j] = cx + ex, b[i + j] = cy + ey;
                    a[i + j + half] = cx - ex, b[i + j + half] = cy - ey;
                    double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx;
                    wx = wnx, wy = wny;
                }
            }
        }
        if (rev)
        {
            for (int i = 0; i < n; i++)
                a[i] /= n, b[i] /= n;
        }
    }
    int solve(int a[],int na,int b[],int nb,int ans[])
    {
        int len = max(na, nb), ln;
        for(ln=0; L(ln)<len; ++ln);
        len=L(++ln);
        for (int i = 0; i < len ; ++i)
        {
            if (i >= na) ax[i] = 0, ay[i] =0;
            else ax[i] = a[i], ay[i] = 0;
        }
        fft(ax, ay, len, 0);
        for (int i = 0; i < len; ++i)
        {
            if (i >= nb) bx[i] = 0, by[i] = 0;
            else bx[i] = b[i], by[i] = 0;
        }
        fft(bx, by, len, 0);
        for (int i = 0; i < len; ++i)
        {
            double cx = ax[i] * bx[i] - ay[i] * by[i];
            double cy = ax[i] * by[i] + ay[i] * bx[i];
            ax[i] = cx, ay[i] = cy;
        }
        fft(ax, ay, len, 1);
        for (int i = 0; i < len; ++i)
            ans[i] = (int)(ax[i] + 0.5)%313;
        return len;
    }
    
    int p[Maxn],n,dp[Maxn];
    int a[Maxn],b[Maxn],c[Maxn],lena,lenb;
    
    void get(int L,int R)
    {
        if(L==R) return;
        int m=(L+R)/2;
        get(L,m);
    
        lena=R-L; lenb=lena;
        for(int i=0;i<m-L+1;i++) b[i]=dp[i+L];
        for(int i=m-L+1;i<lena;i++) b[i]=0;
        for(int i=0;i<lena;i++) c[i]=0;
    
        solve(a,lena,b,lenb,c);
    
        int st=lena/2;
        for(int i=m+1;i<=R;i++) dp[i]=(dp[i]+c[st++])%313;
    
        get(m+1,R);
    }
    
    int main()
    {
        while(~scanf("%d",&n))
        {
            if(n==0) break;
            p[0]=1; for(int i=1;i<=n;i++) { scanf("%d",&p[i]); p[i]=p[i]%313; }
            for(int i=0;i<n;i++) a[i]=p[i+1];
            memset(dp,0,sizeof dp); for(int i=1;i<=n;i++) dp[i]=p[i];
            get(1,n); printf("%d
    ",dp[n]);
        }
        return 0;
    }
  • 相关阅读:
    数据恢复:解决ORA600[kghstack_free2][kghstack_err+0068]一例
    Oracle latch闩原理示意图
    MySQL Query Analyzer查询分析器
    Oracle GoldenGate Monitor架构图
    Oracle Row cache lock图解
    没有Metalink账号的同学可以观赏下,My Oracle Support的主界面
    Oracle Goldengate Director软件截面图
    Oracle Unbreakable Enterprise Kernel Faster than Redhat?
    从Win32过渡到MFC
    naked 函数调用
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5795221.html
Copyright © 2011-2022 走看看