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 1≤n≤105. Following line is a sequence with n non-negative integer a1,a2,…,an, and ai≤107 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

题意:就是给定长度为n,然后长度为1-n的珠子有a[i]种,问拼成长度n的方案数,每种珠子有无限个。

题解:f[i]设为拼成i的方案数,发现转移时f[i]=∑f[j]*a[i-j]+a[i] (1<=j<i)发现是个卷积的形式。

   如何去优化。

   去分治解决,设 l,mid的已经处理好了,那么,考虑l,mid对mid+1,r的贡献,

   对于l,mid的贡献部分做卷积运算,得出其贡献,加到mid,r中。

   这个过程就是CDQ的过程。

 #include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath> #define N 200007
#define pi acos(-1)
#define mod 313
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,num,L;
int a[N],rev[N],f[N];
struct comp
{
double r,v;
comp(double _r=,double _v=){r=_r;v=_v;}
friend inline comp operator+(comp x,comp y){return comp(x.r+y.r,x.v+y.v);}
friend inline comp operator-(comp x,comp y){return comp(x.r-y.r,x.v-y.v);}
friend inline comp operator*(comp x,comp y){return comp(x.r*y.r-x.v*y.v,x.r*y.v+x.v*y.r);}
}x[N],y[N]; void FFT(comp *a,int f)
{
for (int i=;i<num;i++)
if (i<rev[i]) swap(a[i],a[rev[i]]);
for (int i=;i<num;i<<=)
{
comp wn=comp(cos(pi/i),f*sin(pi/i));
for (int j=;j<num;j+=(i<<))
{
comp w=comp(,);
for (int k=;k<i;k++,w=w*wn)
{
comp x=a[j+k],y=w*a[j+k+i];
a[j+k]=x+y,a[j+k+i]=x-y;
}
}
}
if (f==-) for (int i=;i<num;i++) a[i].r/=num;
}
void CDQ(int l,int r)
{
if (l==r) {f[l]=(f[l]+a[l])%mod;return;}
int mid=(l+r)>>;
CDQ(l,mid);
L=;for (num=;num<=(r-l+);num<<=,L++);if (L) L--;
for (int i=;i<num;i++) rev[i]=(rev[i>>]>>)|((i&)<<L);
for (int i=;i<num;i++) x[i]=y[i]=comp(,);
for (int i=l;i<=mid;i++) x[i-l]=comp(f[i],);
for (int i=;i<=r-l;i++) y[i-]=comp(a[i],);
FFT(x,);FFT(y,);
for (int i=;i<num;i++) x[i]=x[i]*y[i];
FFT(x,-);
for (int i=mid+;i<=r;i++)
(f[i]+=x[i-l-].r+0.5)%=mod;
CDQ(mid+,r);
}
int main()
{
while(~scanf("%d",&n)&&n)
{
for (int i=;i<=n;i++) a[i]=read()%mod,f[i]=;
CDQ(,n);
printf("%d\n",f[n]);
}
}

最新文章

  1. 奇怪的Hibernate——当?遇上%
  2. 学习cocos 空程序
  3. shell编程基础(2)---&amp;&amp;与||
  4. S2SH项目框架搭建(完全注解)
  5. 【转】android4.1.2 CTS测试总结
  6. 使用IDENTITY列属性和Sequence对象
  7. Qt 学习之路 2(75):线程总结
  8. ubuntu16.04安装不上有道词典的解决办法
  9. js 你所不知道的一面
  10. 黄聪:Mysql主从配置,实现读写分离
  11. unity 调整摄像机视角完整脚本
  12. Netsharp总体介绍
  13. 总结Linux 下Redis 操作常用命令(转)
  14. 前端菜鸟起飞之学会ps切图
  15. 3143 二叉树的序遍历codevs
  16. Windows虚拟内存不足问题的处理
  17. Fragment切换问题
  18. TeamWork#1,Week 2,Learn In Team
  19. 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
  20. EIT: where is it now and what lies ahead?

热门文章

  1. 003---设计首页index页面
  2. Tensorflow之MNIST的最佳实践思路总结
  3. HBase 是什么
  4. C++11中decltype的使用
  5. spring boot 连接mysql 错误The server time zone value &#39;&#214;&#208;&#185;&#250;&#177;&#234;&#215;&#188;&#202;&#177;&#188;&#228;&#39; is unrecognized or represents more than one
  6. 「日常训练」Phone Numbers (CFR466D2C)
  7. 在Android上Kotlin的单元测试(KAD22)
  8. 基于Ubuntu搭建Linux路由器
  9. 为什么logstash进程的CPU使用率100%?
  10. 初识Django —Python API接口编程入门