【HDU5730】Shell Necklace——CDQ+FFT
来源:互联网 发布:伊朗空军实力知乎 编辑:程序博客网 时间:2024/05/21 08:50
Problem 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
Hint
For the first test case in Sample Input, the Figure 1 provides all schemes about it. The total number of schemes is 1 + 3 + 3 + 7 = 14.
Author
HIT
题意:给你长度为i的项链的组合的情况是a[i],求组成长度为n的项链的方案数。
思路:比较容易想到的就是
假设区间
这样的话,我们就可以通过CDQ分治的方式降低时间复杂度
#include <bits/stdc++.h>#define CLR(a,b) memset(a,b,sizeof(a))using namespace std;const double Pi = acos(-1.0);const int maxn = 2e5+100;const int Mod = 313;struct Complex{ double real, imag; Complex(){} Complex(double _r,double _i):real(_r),imag(_i){} Complex operator + (const Complex &a) const { return Complex(real+a.real,imag + a.imag); } Complex operator - (const Complex &a) const { return Complex(real-a.real,imag - a.imag); } Complex operator * (const Complex &a) const { return Complex(real*a.real-imag*a.imag,real*a.imag+imag*a.real); } void Setvalue(double r,double i) { real = r; imag = i; } void Output() { printf("%f %f\n",real,imag); }}a[maxn*4],b[maxn*4];int dp[maxn],num[maxn];int n,len;void Rader(Complex y[]) { for(int i = 1,j = len>>1;i<len-1;i++) { if(i<j) swap(y[i],y[j]); int k = len>>1; while(j>=k) { j-=k; k>>=1; } if(j<k) j+=k; }}void FFT(Complex y[],int op) { Rader(y); for(int h = 2;h<=len;h<<=1) { Complex wn(cos(2*Pi*op/h),sin(2*Pi*op/h)); for(int i = 0;i<len;i+=h) { Complex w(1,0); for(int j = i;j<i+h/2;j++) { Complex u = y[j]; Complex v = w*y[j+h/2]; y[j] = u+v; y[j+h/2] = u-v; w =w*wn; } } } if(op == -1) { for(int i = 0;i<len;i++) y[i].real /= len; }}void CDQ(int L,int R) {//CDQ分治 if(L == R){ return ; } int mid = (L+R)>>1; CDQ(L,mid); len = 1; while(len<=(R-L+1)) len<<=1; for(int i =0;i<len;i++) a[i].Setvalue(0,0); for(int i =0;i<len;i++) b[i].Setvalue(0,0); for(int i = L;i <= mid; i++) a[i-L].real+=dp[i]; for(int i = 0;i <R-L+1; i++) b[i].real += num[i]; FFT(a,1);FFT(b,1); for(int i = 0;i<len;i++) a[i] = a[i]*b[i]; FFT(a,-1); for(int i = mid+1;i<=R;i++) { dp[i]+=(int)(a[i-L].real+0.5); dp[i]%=Mod; } CDQ(mid+1,R);}int main() { while(~scanf("%d",&n) && n) { CLR(dp,0);CLR(num,0); for(int i = 1;i<=n;i++) { scanf("%d",&num[i]); num[i]%=Mod; } dp[0] = 1; CDQ(0,n); printf("%d\n",dp[n]); } return 0;}
- 【HDU5730】Shell Necklace——CDQ+FFT
- [DP] [1D1D优化] [FFT] [CDQ分治] [HDU5730] Shell Necklace
- hdu5730 Shell Necklace
- hdu5730 Shell Necklace
- hdu5730 Shell Necklace
- Hdu 5730 Shell Necklace(cdq+fft)
- hdu 5730 Shell Necklace cdq分治+FFT
- 【HDU5730 2016 Multi-University Training Contest 1H】【FFT + cdq 分治】 Shell Necklace f[i]=∑f[i-j] x a[j]
- [分治fft] hdu5730 2016多校第一场1008 Shell Necklace
- HDU 5730 Shell Necklace(CDQ分治+FFT)
- [HDU 5730] Shell Necklace (FFT+CDQ分治)
- HDU 5730 多校1 Shell Necklace (CDQ分治+FFT)
- HDU 5730 Shell Necklace(dp+cdq分治+FFT优化)
- HDU 5730 Shell Necklace(FFT+分治)
- HDU 5730——Shell Necklace
- hdu5730(FFT+多项式求逆)
- [2016ACM多校] HDU5730 FFT 重叠相加法
- hdu 5730 Shell Necklace(2016多校第一场)FFT+分治
- vbox安装ukylin 16.10
- 按钮实现鼠标悬停背景色渐变的动画特效
- cocos-js html5网页版修改默认背景颜色
- 表单提交前的验证
- 【OpenCV学习笔记 016】图像分割-种子区域生长
- 【HDU5730】Shell Necklace——CDQ+FFT
- 【行研】工控安全之工控厂商防护思路
- spring @component的作用
- C++中cout输出字符型指针地址值的方法
- CSDN插入图片
- URL中的?和&
- 天使
- 解决 Must have one-pixel frame that is either transparent or white
- 关于登录的自己总结