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]\) 求 ...
随机推荐
- 笨方法学python之import sys与from sys import argv的区别
这是在网上看到的一个大神的解答: sys is a module that contains “system functionality”. sys.argv is a list containing ...
- JAVA 泛型方法<T>
public static void main(String[] args) throws Exception { String[] arr = new String[]{"1", ...
- Ubuntu中搭建Hadoop集群(简记)
stp1:在Vmware虚拟机上创建Ubantu.2环境 步骤:文件—>新建虚拟机—>典型(下一步)—>下一步——>位置(不建议放c盘,文件地址一定要全英文)—>下一步— ...
- DSP28335的XINTF操作SRAM实验
1. 本次使用三兄弟的XDS28335开发板,研究一下XINTF操作SRAM的代码.哈弗结构,奇怪,DSP28335是哈弗结构,那么数据和程序空间应该独立的,为啥书上说采用统一的寻址方式?估计只是读写 ...
- Android各版本代号、版本号、API/NDK级别、发布时间
代号 版本号 API/NDK级别 发布时间 牛轧糖 Nougat 7.1.2 API level 25 2017-2 7.1.1 2016-10 7.0 API level 24 2016-05 棉花 ...
- Scala学习笔记(四):从文件里读取文本行
第一个版本: import scala.io.Source if(args.length>0){ for(line<-Source.fromFile(args(0)).getLines) ...
- 【APUE】Chapter1 UNIX System Overview
这章内容就是“provides a whirlwind tour of the UNIX System from a programmer's perspective”. 其实在看这章内容的时候,已经 ...
- ES5新增数组方法(3):some
检查数组元素中是否有元素符合指定. // 数组中的元素部分满足指定条件返回true let arr = [1, 3, 5, 7, 9]; console.log(arr.some((value, in ...
- jmeter的基本使用过程
jmeter的基本使用过程 接下来几周,我将通过视频的方式,录制下来jmeter的基本用法,方便大家参考学习 可能导图会随时调整
- 当我们在安装tensorflow时,我们在安装什么?- Intro to TF, Virtualenv, Docker, CUDA, cuDNN, NCCL, Bazel
(Mainly quoted from its official website) Summary: 1. TensorFlow™ is an open source software library ...