【多校训练】hdu 5730 cdq+fft
2017-06-22 21:27
405 查看
Shell Necklace
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1089 Accepted Submission(s): 467
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 nnon-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
![](http://acm.split.hdu.edu.cn/data/images/C704-1008-1.jpg)
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.
这题很容易就推出公式
f[1]=0;
f[i]=∑(f[i - j] * a[j]), j∈[1, i];
但是如果按照公式暴力做的话会超时。看到这种形式的式子应该想到fft(...其实我也没想到,之前没怎么看过fft,趁机会学习了一下。
但是做n次fft是会超时的,所以还需要用到cdq二分的方法来优化,cdq二分之前没见过,学习了一下,感觉挺巧妙的。。。
总之通过cdq+fft,这题也就出来了。其实没有什么思维上的难点,主要的对两个算法的了解(套模板,但是如果不熟悉的话还是很难做出的。
#include <iostream> #include<bits/stdc++.h> #define ll long long using namespace std; const int MOD=313; const int N=410000; int n; const double PI = acos(-1.0); //复数结构体 struct Complex { double r,i; Complex(double _r = 0.0,double _i = 0.0) { r = _r; i = _i; } Complex operator +(const Complex &b) { return Complex(r+b.r,i+b.i); } Complex operator -(const Complex &b) { return Complex(r-b.r,i-b.i); } Complex operator *(const Complex &b) { return Complex(r*b.r-i*b.i,r*b.i+i*b.r); } }; /* * 进行FFT和IFFT前的反转变换。 * 位置i和 (i二进制反转后位置)互换 * len必须去2的幂 */ void change(Complex y[],int len) { int i,j,k; for(i = 1, j = len/2;i < len-1; i++) { if(i < j)swap(y[i],y[j]); //交换互为小标反转的元素,i<j保证交换一次 //i做正常的+1,j左反转类型的+1,始终保持i和j是反转的 k = len/2; while( j >= k) { j -= k; k /= 2; } if(j < k) j += k; } } /* * 做FFT * len必须为2^k形式, * on==1时是DFT,on==-1时是IDFT */ void fft(Complex y[],int len,int on) { change(y,len); for(int h = 2; h <= len; h <<= 1) { Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h)); for(int j = 0;j < len;j+=h) { Complex w(1,0); for(int k = j;k < j+h/2;k++) { Complex u = y[k]; Complex t = w*y[k+h/2]; y[k] = u+t; y[k+h/2] = u-t; w = w*wn; } } } if(on == -1) for(int i = 0;i < len;i++) y[i].r /= len; } Complex x1 ,x2 ; int f ,a ; void cdq(int l, int r) { if(l==r) { f[l]=(f[l]+a[l])%MOD; return ; } int mid=l+r >> 1; cdq(l,mid); int len1 = r - l + 1; int len2 = mid - l + 1; int len=1; while(len<(len1+len2)) len<<=1; for(int i=0;i<len2;i++) x1[i]=Complex(f[i+l],0); for(int i=len2;i<len;i++) x1[i]=Complex(0,0); for(int i=0;i<len1;i++) x2[i]=Complex(a[i],0); for(int i=len1;i<len;i++) x2[i]=Complex(0,0); fft(x1,len,1); fft(x2,len,1); for(int i=0;i<len;i++) x1[i]=x1[i]*x2[i]; fft(x1,len,-1); for(int i=mid+1;i<=r;i++) f[i]=(f[i]+(int)(x1[i-l].r+0.5))%MOD; cdq(mid+1,r); } int main() { while(~scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) {scanf("%d",&a[i]);a[i]%=MOD;} memset(f,0,sizeof(f)); cdq(1,n); printf("%d\n",f ); } }
相关文章推荐
- HDU 5730 2016多校Contest 1 G题【CDQ分治和FFT模板】
- hdu 5730 (CDQ+FFT)模板题
- HDU 5730 (CDQ分治 FFT)
- HDU - 5730 CDQ分治 + FFT
- Hdu 5730 Shell Necklace(cdq+fft)
- HDU 5730: Shell Necklace 分治FFT
- (2017多校训练第二场)HDU - 6055 & POJ - 2002 Regular polygon 哈希
- (HDU 5792)World is Exploding <树状数组+去重> 多校训练5
- hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场
- hdu 5730(分治FFT)
- 【多校训练】hdu 6129 Just do it
- hdu 4323 Magic Number 2012多校训练第三场1004题 BK树
- 2017 多校训练第九场 HDU 6168 Numbers 贪心枚举
- 【多校训练】hdu 6180 Schedule 贪心+set
- 2017 多校训练第六场 HDU 6097 Mindis
- 2017 多校训练第二场 HDU 6045 Is Derek lying?
- hdu 4941 Magical Forest(STL之map应用)2014多校训练第7场
- hdu 4649 Professor Tian 多校联合训练的题
- 2017 多校训练第三场 HDU 6058 Kanade's sum
- HDU多校联合训练 Problem1001 Abandoned country