hdu Shell Necklace 5730 分治FFT
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的更多相关文章
- HDU Shell Necklace CDQ分治+FFT
Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...
- hdu5730 Shell Necklace 【分治fft】
题目 简述: 有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案 题解 设\(f[i]\)表示长度为\(i\)时的方案数 不难得dp方程: \[f[i] = \su ...
- HDU 5730 Shell Necklace cdq分治+FFT
题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...
- 【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 ...
- hdu 5730 Shell Necklace [分治fft | 多项式求逆]
hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...
- 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 ...
- hdu 5730 Shell Necklace——多项式求逆+拆系数FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 可以用分治FFT.但自己只写了多项式求逆. 和COGS2259几乎很像.设A(x),指数是长度,系数 ...
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- 【HDU5730】Shell Necklace(多项式运算,分治FFT)
[HDU5730]Shell Necklace(多项式运算,分治FFT) 题面 Vjudge 翻译: 有一个长度为\(n\)的序列 已知给连续的长度为\(i\)的序列装饰的方案数为\(a[i]\) 求 ...
随机推荐
- cloudera manager服务迁移(scm数据库在postgresql上,其他amon,rman,oozie,metastore等在mysql上)
公司线上大数据集群,之前用的是公有云主机,现在换成了自己idc机房机器,需要服务迁移,已下为测试: 1.备份原postgresql数据库: pg_dump -U scm scm > scm.sq ...
- SLAM中的常识与经验
双目矫正 双目通常事先是通过畸变矫正标定的,而RGB-D和单目则并不一定完成了矫正. 因此,对于RGB-D和单目获取的图像,在提取特征点之后,需要矫正,而双目则可以省略这一过程. 词袋模型反向索引 D ...
- Java中的IO流体系
Java为我们提供了多种多样的IO流,我们可以根据不同的功能及性能要求挑选合适的IO流,如图10-7所示,为Java中IO流类的体系. 注:这里只列出常用的类,详情可以参考JDK API文档.粗体标注 ...
- 思杰VDI提示“The VDI is not available”
前言:困扰已久的问题终于解决. 问题:客户反馈无法连接VDI. 解决过程:1.登录后台查看VDI状态为关机状态尝试重新启动提示如下图: 2.判断此VDI的启动盘出现问题(注:本人环境无数据盘) 3.查 ...
- 给apk签名
一.签名 把apk和签名文件放在jdk bin目录下,然后在jkd bin目录下执行以下代码: jarsigner -verbose -keystore xxx.keystore -signedjar ...
- jmeter操作JDBC
1. 依次添加计划.线程组.JDBC Connection Configuration.JDBC Request.HTTP请求.Debug Sampler.察看结果树 在计划中导入mysql的jdbc ...
- [B2B、B2C、C2C] 区别介绍
最近在学习建站系统的时候,偶尔我们的老大会说几个自己所不太了解的名词“简称”,所以呢?我就总结了一下,如果有不全面的地方,还请博友们多多指点! B2B B2B(也有写成BTB)是指企业对企业之间的营销 ...
- 第18讲——string类
关键字:string类 字符串 C-风格字符串 C库字符串函数 字符串:存储在内存的连续字节中的一系列字符. C++处理字符串的方式有两种: 来自C语言,常被称为C-风格字符串: 基于strin ...
- BufferedInputStream/BufferedOutputStream
BufferedInputStream: public synchronized int read() throws IOException int res=bis.read(); System.ou ...
- PAT L2-019 悄悄关注
https://pintia.cn/problem-sets/994805046380707840/problems/994805059731177472 新浪微博上有个“悄悄关注”,一个用户悄悄关注 ...