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.

InputThere are multiple test cases(no more than 2020 cases and no more than 1 in extreme case), ended by 0.

For each test cases, the first line contains an integer nn, meaning the number of shells in this shell necklace, where 1≤n≤1051≤n≤105. Following line is a sequence with nnnon-negative integer a1,a2,…,ana1,a2,…,an, and ai≤107ai≤107 meaning the number of schemes to decorate ii continuous shells together with a declaration of love. 
OutputFor each test case, print one line containing the total number of schemes module 313313(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.

见:https://blog.csdn.net/Maxwei_wzj/article/details/79850756

题意:一串项链是n个珠子组成,如果i个珠子连续,可以被认为是模式i,贡献是ai一串项链是n个珠子组成,如果i个珠子连续,可以被认为是    模式i,贡献是ai 
思路:对于一串珠子,列出DP方程,dp[i]表示长度为i的项链,所有情况的贡献和 dp[i]=∑ dp[j]*A[i-j],复杂度O(N^2)。
观察这个式子很像卷积的形式,于是可以用cdq分治+FFT来优化一波观察这个式子很像卷积的形式,于是可以用cdq分治+FFT来优化一波 。

(正确性不难证明,因为分治到Mid+1之前,dp[1]-dp[Mid]的值都已经求出来了。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=;
const double pi=acos(-1.0);
struct cp
{
double r,i;
cp(){}
cp(double rr,double ii):r(rr),i(ii){}
cp operator +(const cp&x)const{return cp(r+x.r,i+x.i);}
cp operator -(const cp&x)const{return cp(r-x.r,i-x.i);}
cp operator *(const cp&x)const{return cp(r*x.r-i*x.i,i*x.r+r*x.i);}
};
ll dp[maxn],A[maxn];
cp a[maxn<<],b[maxn<<],W,w,p; int R[maxn<<],n;
inline void fft(cp*c,int t)
{
int i,j,k;
for(i=;i<n;i++) R[i]<i?swap(c[R[i]],c[i]),:;
for(i=;i<n;i<<=)
for(j=,W={cos(pi/i),sin(pi/i)*t};j<n;j+=i<<)
for(k=,w={,};k<i;k++,w=w*W)
p=c[j+k+i]*w,c[j+k+i]=c[j+k]-p,c[j+k]=c[j+k]+p;
}
void solve(int l,int r)
{
if(l==r) return ;
int mid=(l+r)>> ;
solve(l,mid);
for(n=;n<((r-l+));n<<=);
rep(i,,n-) R[i]=R[i>>]>>|(i&?n>>:); rep(i,,mid-l) a[i]=cp(dp[l+i],.);
rep(i,mid-l+,n) a[i]=cp(,); rep(i,,r-l-) b[i]=cp(A[i+],.);
rep(i,r-l,n) b[i]=cp(,); fft(a,); fft(b,);
rep(i,,n-) a[i]=a[i]*b[i]; fft(a,-);
rep(i,mid+,r) dp[i]+=a[i-l-].r/n+0.5,dp[i]%=Mod; solve(mid+,r);
}
int main()
{
int N;
while(~scanf("%d",&N)&&N>){
rep(i,,N) dp[i]=; dp[]=;
rep(i,,N) scanf("%d",&A[i]),A[i]%=Mod;
solve(,N);
printf("%d\n",dp[N]);
}
return ;
}

HDU - 5730 :Shell Necklace(CDQ分治+FFT)的更多相关文章

  1. HDU 5730 Shell Necklace cdq分治+FFT

    题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...

  2. HDU Shell Necklace CDQ分治+FFT

    Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...

  3. hdu 5730 Shell Necklace [分治fft | 多项式求逆]

    hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...

  4. HDU 5730 Shell Necklace(CDQ分治+FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3 ...

  5. hdu 5730 Shell Necklace fft+cdq分治

    题目链接 dp[n] = sigma(a[i]*dp[n-i]), 给出a1.....an, 求dp[n]. n为1e5. 这个式子的形式显然是一个卷积, 所以可以用fft来优化一下, 但是这样也是会 ...

  6. HDU.5730.Shell Necklace(分治FFT)

    题目链接 \(Description\) 有\(n\)个长度分别为\(1,2,\ldots,n\)的珠子串,每个有\(a_i\)种,每种个数不限.求有多少种方法组成长度为\(n\)的串.答案对\(31 ...

  7. hdu 5730 Shell Necklace——多项式求逆+拆系数FFT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 可以用分治FFT.但自己只写了多项式求逆. 和COGS2259几乎很像.设A(x),指数是长度,系数 ...

  8. hdu5730 Shell Necklace 【分治fft】

    题目 简述: 有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案 题解 设\(f[i]\)表示长度为\(i\)时的方案数 不难得dp方程: \[f[i] = \su ...

  9. #8 //HDU 5730 Shell Necklace(CDQ分治+FFT)

    Description 给出长度分别为1~n的珠子,长度为i的珠子有a[i]种,每种珠子有无限个,问用这些珠子串成长度为n的链有多少种方案 题解: dp[i]表示组合成包含i个贝壳的项链的总方案数 转 ...

  10. 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] ...

随机推荐

  1. mysql只能连接localhost解决

    grant all privileges on *.* to 'root'@'%' identified by 'root';flush privileges;

  2. echarts3.8.4实现城市空气质量(结合百度地图bmap.js,小航哥)

    (小航哥自己实现的)为了事先地图效果,需要以下准备: 用百度地图作为地图,需要 1.bmap.min.js(下载地址https://github.com/ecomfe/echarts ,GitHub上 ...

  3. 41和为S的连续正数序列

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

  4. 17 南宁区域赛 F - The Chosen One 【规律】

    题目链接 https://nanti.jisuanke.com/t/19972 题意 给出一个n 然后将 n 个数 标号为 1 -> n 按顺序排列 每次抽掉 奇数位的数 然后求最后剩下那个数字 ...

  5. des加密——补齐

    下面这个网址(英文)介绍的比较全面. http://www.di-mgt.com.au/cryptopad.html

  6. $.queue() 与 $.dequeue() -- 队列

    JQuery 运用队列为动画模块服务,但好像它应该有更多用处,我觉得的,那试试就知道咯. 简单的来讲,它就是形成队列和出列, 也就因此可以进行很有规律的回调和延时了呀(暂停感觉有难度),当然这就是后面 ...

  7. 4.2《深入理解计算机系统》笔记(五)并发、多进程和多线程【Final】

    该书中第11章是写web服务器的搭建,无奈对web还比较陌生.还没有搞明白. 这些所谓的并发,其实都是操作系统做的事情,比如,多进程是操作系统fork函数实现的.I/O多路复用需要内核挂起进程.多线程 ...

  8. (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解

    说明:Statement stmt = con.createStatemen=(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY ...

  9. 查看SELinux状态

    查看SELinux状态:1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 2.getenforce ##也可以用这个命令检查 关闭S ...

  10. 【bzoj1036】树的统计[ZJOI2008]树链剖分+线段树

    题目传送门:1036: [ZJOI2008]树的统计Count 这道题是我第一次打树剖的板子,虽然代码有点长,但是“打起来很爽”,而且整道题只花了不到1.5h+,还是一遍过样例!一次提交AC!(难道前 ...