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

hdu Shell Necklace 5730 分治FFT的更多相关文章

  1. HDU Shell Necklace CDQ分治+FFT

    Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...

  2. hdu5730 Shell Necklace 【分治fft】

    题目 简述: 有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案 题解 设\(f[i]\)表示长度为\(i\)时的方案数 不难得dp方程: \[f[i] = \su ...

  3. HDU 5730 Shell Necklace cdq分治+FFT

    题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...

  4. 【HDU 5730】Shell Necklace

    http://acm.hdu.edu.cn/showproblem.php?pid=5730 分治FFT模板. DP:\(f(i)=\sum\limits_{j=0}^{i-1}f(j)\times ...

  5. hdu 5730 Shell Necklace [分治fft | 多项式求逆]

    hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...

  6. HDU - 5730 :Shell Necklace(CDQ分治+FFT)

    Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell necklace with n b ...

  7. hdu 5730 Shell Necklace——多项式求逆+拆系数FFT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 可以用分治FFT.但自己只写了多项式求逆. 和COGS2259几乎很像.设A(x),指数是长度,系数 ...

  8. HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...

  9. 【HDU5730】Shell Necklace(多项式运算,分治FFT)

    [HDU5730]Shell Necklace(多项式运算,分治FFT) 题面 Vjudge 翻译: 有一个长度为\(n\)的序列 已知给连续的长度为\(i\)的序列装饰的方案数为\(a[i]\) 求 ...

随机推荐

  1. Python3爬虫(七) 解析库的使用之pyquery

    Infi-chu: http://www.cnblogs.com/Infi-chu/ pyquery专门针对CSS和jQuery的操作处理 1.初始化字符串初始化 from pyquery impor ...

  2. Spyder在windows下常用快捷键

    块注释/反块注释:Ctrl+4/5 行注释/反行注释:Ctrl+1 代码提示:Tab 复制一行:Ctrl+Alt+↓/↑ 删除一行:Ctrl+D 运行:F5 全屏:F11 撤销:Ctrl+Z 反撤销: ...

  3. ubuntu配置机器学习环境(三) opencv 安装

    这里使用脚本安装 一些教程里使用cmake 安装,很容易出错的 使用github上的安装脚本,自动化安装 参考链接 Ubuntu $ cd Ubuntu/2.4 $ chmod +x * # 如果要安 ...

  4. ALVのイベントを取得する方法

    概要 表示されたALVをダブルクリックした時に別画面へ遷移する方法を説明しよう.下記サンプルのように標準トランザクションへ遷移したり.別のALVを表示したりする事が可能である. サンプルコード ABA ...

  5. WPF 构建无外观(Lookless)控件

    原文:WPF 构建无外观(Lookless)控件 构建一个用户可以使用Template属性设置外观的WPF控件需要以下几步 1.继承自System.Windows.Controls.Control 2 ...

  6. Java的HashMap和HashTable

    Java的HashMap和HashTable 1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashm ...

  7. 「暑期训练」「Brute Force」 Optimal Point on a Line (Educational Codeforces Round 16, B)

    题意 You are given n points on a line with their coordinates $x_i$. Find the point x so the sum of dis ...

  8. Linux-Shell脚本编程-学习-8-函数

    在这章往后的学习中,我讲尽可能详细的讲书中讲到的都记录到这里,以便以后方便查看. 什么是函数,函数就是一段代码,这段代码可以在我们需要的位置调用,那么这段代码就叫做函数. 在Shell中,定义一个函数 ...

  9. 游戏测试中遇到的奇葩bug(不断整理中...)

    1:跨服组织战中,不同服务器相同组织ID的敌对玩家不能造成伤害. 2:节日活动24点开启,角色不下线自然过渡到活动开启,界面显示异常 3:前端请求数据之后,不管是否接收到后端返回的数据,只要玩家点击仙 ...

  10. ThreadPool线程池的几种姿势比较

    from multiprocessing.pool import ThreadPool #from multiprocessing.dummy import Pool as ThreadPool #这 ...