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

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 than20 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 module313(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
2016 Multi-University Training Contest 1
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
#include
#include
#include
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>1;
	gao(l,mid);
	
	int len=1;
	while(len<=r-l+1) len<<=1;
	
	for(int i=0;i


 

你可能感兴趣的:(ACM_FFT,/,NTT,ACM_分治,HDUOJ)