hdu5730 Shell Necklace

来源:互联网 发布:网络唤醒数据包 编辑:程序博客网 时间:2024/05/16 11:45

题目:一段长为i的项链有a[i]种表达爱意的装饰方法,问长度为n的项链有多少种用上述方式组成的方法。

思路:dp[i]=∑dp[j]*a[i-j],(1<=j<=i-1)

要用fft+CDQ分治

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;//#define PI acos(-1.0)#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f// 0x3f3f3f3fconst int maxn=200050;const int mod=313;const double PI=acos(-1.0);struct Complex{//复数结构体    double x,y;    Complex(double _x=0.0,double _y=0.0){        x=_x;        y=_y;    }    Complex operator-(const Complex &b)const{        return Complex(x-b.x,y-b.y);    }    Complex operator+(const Complex &b)const{        return Complex(x+b.x,y+b.y);    }    Complex operator*(const Complex &b)const{        return Complex(x*b.x-y*b.y,x*b.y+y*b.x);    }};/**进行FFT和IFFT前的反转变换.*位置i和 (i二进制反转后位置)互换*len必须是2的幂*/void change(Complex 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;    }}/**做FFT*len必须为2^k形式,*on==1时是DFT,on==-1时是IDFT*/void fft(Complex y[],int len,int on){    change(y,len);    for(int h=2;h<=len;h<<=1){        Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/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[maxn],x2[maxn];int arr[maxn];int dp[maxn];void cdq(int L,int R){    if(L==R){        dp[L]=(dp[L]+arr[L])%mod;        return;    }    int mid=(L+R)>>1;    cdq(L,mid);    int len=1;    while(len<(R-L+1)) len<<=1;    for(int i=0;i<len;i++)        x1[i]=x2[i]=Complex(0,0);    for(int i=L;i<=mid;i++)        x1[i-L]=Complex(dp[i],0);    for(int i=0;i<R-L+1;i++)        x2[i]=Complex(arr[i+1],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=mid+1;i<=R;i++){        dp[i]+=(int)(x1[i-L-1].x+0.5);        dp[i]%=mod;    }    cdq(mid+1,R);}int main(){//    freopen("D:\\input.txt","r",stdin);//    freopen("D:\\output.txt","w",stdout);    int n;    while(~scanf("%d",&n)){        if(!n)            break;        for(int i=1;i<=n;i++){            scanf("%d",&arr[i]);            arr[i]%=mod;            dp[i]=0;        }        cdq(1,n);        printf("%d\n",dp[n]);    }    return 0;}


阅读全文
0 0