HDU 5730 Shell Necklace cdq分治+FFT
题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式
分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1)
然后对于这种递推式,也就是dp[i]等于前j个dp数组和a数组的卷积,然后可看所有的
一看n是1e5,所以暴力超时,然后采用cdq分治加速,这种卷积递推通常采用cdq分治加速
cdq的话很简单了,就是先递归左边,算左对右的贡献,递归右边就行,一半一半更新
#include <cstdio>
#include <vector>
#include <stack>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+;
const int mod = ;
typedef long long LL;
const double pi = acos(-1.0);
int a[N],dp[N],n;
struct complex{
double r,i;
complex(double R=,double I=){
r=R;i=I;
}
complex operator+(const complex &a)const{
return complex(r+a.r,i+a.i);
}
complex operator-(const complex &a)const{
return complex(r-a.r,i-a.i);
}
complex operator*(const complex &a)const{
return complex(r*a.r-i*a.i,r*a.i+i*a.r);
}
}x[N*],y[N*];
void change(complex x[],int len){
int i,j,k;
for(i=,j=len/;i<len-;++i){
if(i<j)swap(x[i],x[j]);
k=len/;
while(j>=k){j-=k;k>>=;}
if(j<k)j+=k;
}
}
void fft(complex x[],int len,int on){
change(x,len);
for(int i=;i<=len;i<<=){
complex wn(cos(-on**pi/i),sin(-on**pi/i));
for(int j=;j<len;j+=i){
complex w(,);
for(int k=j;k<j+i/;++k){
complex u = x[k];
complex t = w*x[k+i/];
x[k]=u+t;
x[k+i/]=u-t;
w=w*wn;
}
}
}
if(on==-)for(int i=;i<len;++i)x[i].r/=len;
}
void up(int &x){
x%=mod;
}
void cdq(int l,int r){
if(l==r){dp[l]+=a[l];up(dp[l]);return;}
int mid=l+r>>;
cdq(l,mid);
int len=;
while(len<=(r-l+))len<<=;
for(int i=;i<len;++i)x[i]=y[i]=complex(,);
for(int i=l;i<=mid;++i)x[i-l]=complex(dp[i],);
for(int i=;i<=r-l+;++i)y[i-]=complex(a[i],);
fft(x,len,);fft(y,len,);
for(int i=;i<len;++i)x[i]=x[i]*y[i];
fft(x,len,-);
for(int i=mid+;i<=r;++i)
dp[i]+=(int)(x[i-l-].r+0.5),up(dp[i]);
cdq(mid+,r);
}
int main(){
while(~scanf("%d",&n),n){
for(int i=;i<=n;++i){
scanf("%d",&a[i]);up(a[i]);dp[i]=;
}
cdq(,n);
printf("%d\n",dp[n]);
}
return ;
}
HDU 5730 Shell Necklace cdq分治+FFT的更多相关文章
- HDU Shell Necklace CDQ分治+FFT
Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...
- 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)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3 ...
- hdu 5730 Shell Necklace fft+cdq分治
题目链接 dp[n] = sigma(a[i]*dp[n-i]), 给出a1.....an, 求dp[n]. n为1e5. 这个式子的形式显然是一个卷积, 所以可以用fft来优化一下, 但是这样也是会 ...
- HDU.5730.Shell Necklace(分治FFT)
题目链接 \(Description\) 有\(n\)个长度分别为\(1,2,\ldots,n\)的珠子串,每个有\(a_i\)种,每种个数不限.求有多少种方法组成长度为\(n\)的串.答案对\(31 ...
- hdu 5730 Shell Necklace——多项式求逆+拆系数FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 可以用分治FFT.但自己只写了多项式求逆. 和COGS2259几乎很像.设A(x),指数是长度,系数 ...
- hdu5730 Shell Necklace 【分治fft】
题目 简述: 有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案 题解 设\(f[i]\)表示长度为\(i\)时的方案数 不难得dp方程: \[f[i] = \su ...
- #8 //HDU 5730 Shell Necklace(CDQ分治+FFT)
Description 给出长度分别为1~n的珠子,长度为i的珠子有a[i]种,每种珠子有无限个,问用这些珠子串成长度为n的链有多少种方案 题解: dp[i]表示组合成包含i个贝壳的项链的总方案数 转 ...
- hdu 5730 Shell Necklace —— 分治FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 DP式:\( f[i] = \sum\limits_{j=1}^{i} f[i-j] * a[j] ...
随机推荐
- Strust2最基本使用
制作一个登陆表单,然后在另一个页面显示提交的内容,简单的一个小工程. 页面: //login.jsp <form action="login.action" method=& ...
- React事件处理函数的bind复用和name复用
一.bind复用 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&qu ...
- 243. Shortest Word Distance
题目: Given a list of words and two words word1 and word2, return the shortest distance between these ...
- 37. Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- ios ableviewcell的动态加载数据,模仿喜马拉雅动态数据加载
iphone(UITableViewCell)动态加载图片http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Intr ...
- SGU 441 Set Division(矩阵快速幂)
题目链接:http://acm.sgu.ru/status.php 题意:将n个有区别的球放到m个无区别的盒子里,盒子不能为空.不同的方案数. 思路:设f[i][j]表示将前i个球放到j个盒子里,那么 ...
- HDU 4549 M斐波那契数列(矩阵幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549 题意:F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]. 思路:手算一下可以发现 ...
- [HDOJ2795]Billboard(线段树,单点更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:w*h的公告板要贴公告,公告是w*1的,每个公告有先后顺序,要使每个公告贴的位置尽可能地高 ...
- sublime中文乱码
今天在用sublime的时候,又出现乱码的情况了.弹层如下: 检测了一下,当前文件,sublime编辑器左下角显示如下: 显示的是 ASCII 编码的文件,而其他没有没问题的页面显示的的 GBK(或G ...
- gulp browser-sync自动刷新插件
很久没弄gulp了,都快忘了,今天又来温习下browser-sync 自动刷新插件,在安装的时候出现以下提示: $ npm install browser-sync --save-dev> ws ...