1920℃

# 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

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)
{
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;
}


## IT问道推荐

“Dr信用牛牛”让你远离信用污点 国内首家信用健康管理平台免费为你提供信用修复方案