HDU 5730 多校1 Shell Necklace (CDQ分治+FFT)

编程语言 来源:liangzhaoyang1 1371℃ 0评论

Shell Necklace

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 694    Accepted Submission(s): 310

Problem Description
Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell necklace with n beautiful shells contains the most sincere feeling for my best lover Arrietty, but even that is not enough.
Suppose the shell necklace is a sequence of shells (not a chain end to end). Considering i continuous shells in the shell necklace, I know that there exist different schemes to decorate the i shells together with one declaration of love.
I want to decorate all the shells with some declarations of love and decorate each shell just one time. As a problem, I want to know the total number of schemes.
Input
There are multiple test cases(no more than 20 cases and no more than 1 in extreme case), ended by 0.

For each test cases, the first line contains an integer n , meaning the number of shells in this shell necklace, where 1n105 . Following line is a sequence with n non-negative integer a1,a2,,an , and ai107 meaning the number of schemes to decorate i continuous shells together with a declaration of love.
Output
For each test case, print one line containing the total number of schemes module 313 (Three hundred and thirteen implies the march 13th, a special and purposeful day).
Sample Input
   
3 1 3 7 4 2 2 2 2 0
Sample Output
   
14 54
Hint
For the first test case in Sample Input, the Figure 1 provides all schemes about it. The total number of schemes is 1 + 3 + 3 + 7 = 14.
Author
HIT
Source
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5932 5931 5930 5929 5928 

题意:给你一段长为 i 的项链有 a[i] 种装饰方法,问长度为n的项链有多少种装饰方式。
题解:令dp[i]表示用这些珠子串成长度为i的链的方案数,并令dp[0]=1,可以得到转移方程 
这里写图片描述 
由上式暴力求dp[n]时间复杂度O(n^2),显然不行,数量级在1e5,考虑到上式右边是一个卷积形式,所以用CDQ分治+FFT来降低复杂度。
假设CDQ(l,r)为求出dp[l],dp[l+1],…,dp[r]的值,那么如果已经通过CDQ(l,mid)求出了dp[l],dp[l+1],…,dp[mid],下面考虑dp[l],dp[l+1],…,dp[mid]对dp[mid+1],dp[mid+2],…,dp[r]的贡献。
令g[i]表示dp[l],…,dp[mid]对dp[i]的贡献,那么有这里写图片描述,令x[i]=dp[i+l] (i=0,…,mid-l),y[i]=a[i+1] (i=0,…,r-l-1),则有 
这里写图片描述 
所以对x序列和y序列做一遍FFT即可得到z序列,进而得到g序列 
总时间复杂度O(n*logn*logn) 。

AC代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int N = 2e5+10;
const double pi = acos(-1.0);
const int mod=313; 
char s1[N],s2[N];
int len,res[N];

struct Complex
{
    double r,i;
    Complex(double r=0,double i=0):r(r),i(i) {};
    Complex operator+(const Complex &rhs)
    {
        return Complex(r + rhs.r,i + rhs.i);
    }
    Complex operator-(const Complex &rhs)
    {
        return Complex(r - rhs.r,i - rhs.i);
    }
    Complex operator*(const Complex &rhs)
    {
        return Complex(r*rhs.r - i*rhs.i,i*rhs.r + r*rhs.i);
    }
} va[N],vb[N];

//雷德算法--倒位序  
void rader(Complex F[],int len) //len = 2^M,reverse F[i] with  F[j] j为i二进制反转
{
    int j = len >> 1;
    for(int i = 1;i < len - 1;++i)
    {
        if(i < j) swap(F[i],F[j]);  // reverse
        int k = len>>1;
        while(j>=k)
        {
            j -= k;
            k >>= 1;
        }
        if(j < k) j += k;
    }
}
//FFT实现
void FFT(Complex F[],int len,int t)
{
    rader(F,len);
    for(int h=2;h<=len;h<<=1) //分治后计算长度为h的DFT 
    {
        Complex wn(cos(-t*2*pi/h),sin(-t*2*pi/h)); //单位复根e^(2*PI/m)用欧拉公式展开
        for(int j=0;j<len;j+=h)
        {
            Complex E(1,0); //旋转因子
            for(int k=j;k<j+h/2;++k)
            {
                Complex u = F[k];
                Complex v = E*F[k+h/2];
                F[k] = u+v; //蝴蝶合并操作
                F[k+h/2] = u-v;
                E=E*wn; //更新旋转因子
            }
        }
    }
    if(t==-1)   //IDFT
        for(int i=0;i<len;++i)
            F[i].r/=len;
}
//数组开小会RE 
ll a[4*N]; 
int b[4*N];  
int dp[N]; 
Complex x[4*N],y[4*N];
int n;

//求卷积 
void Conv(Complex a[],Complex b[],int len) 
{
    FFT(a,len,1);
    FFT(b,len,1);
    for(int i=0;i<len;i++) a[i] = a[i]*b[i];
    FFT(a,len,-1);
}
void gao(int l,int r)  
{  
	if(l==r)
	{
		dp[l]+=a[l];
		dp[l]%=mod;
		return ;
	}
	int mid=(l+r)>>1;
	gao(l,mid);
	
	int len=1;
	while(len<=r-l+1) len<<=1;
	
	for(int i=0;i<len;i++)
	{
		if(i+l<=mid) x[i]=Complex(dp[i+l],0);
        else x[i]=Complex(0,0);
        
        if(l+i+1<=r) y[i]=Complex(a[i+1],0);
        else y[i]=Complex(0,0);
	}
    Conv(x,y,len);
    for(int i=0;i<len;i++)
    b[i]=(ll)(x[i].r+0.5)%mod;
    
    for(int i=mid+1;i<=r;i++)
    {
    	dp[i]+=b[i-l-1];
    	dp[i]%=mod;
	}
	gao(mid+1,r);
}  
void solve()  
{  
    while(scanf("%d",&n),n)
    {
    	memset(dp,0,sizeof(dp));
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d",&a[i]);
    		a[i]%=mod;
		}
		gao(1,n);
		printf("%d\n",dp[n]);
	}
}  
int main()
{ 
    solve();
    return 0;
}