Shell Necklace

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

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[i]  = ∑ f[ n - i ] * a[i]

  分治FFT经典题,不作赘述

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 3e5+, M = 1e3+,inf = 2e9; const LL mod = 313LL; struct Complex {
double r , i ;
Complex () {}
Complex ( double r , double i ) : r ( r ) , i ( i ) {}
Complex operator + ( const Complex& t ) const {
return Complex ( r + t.r , i + t.i ) ;
}
Complex operator - ( const Complex& t ) const {
return Complex ( r - t.r , i - t.i ) ;
}
Complex operator * ( const Complex& t ) const {
return Complex ( r * t.r - i * t.i , r * t.i + i * t.r ) ;
}
} ; void FFT ( Complex y[] , int n , int rev ) {
for ( int i = , j , t , k ; i < n ; ++ i ) {
for ( j = , t = i , k = n >> ; k ; k >>= , t >>= ) j = j << | t & ;
if ( i < j ) swap ( y[i] , y[j] ) ;
}
for ( int s = , ds = ; s <= n ; ds = s , s <<= ) {
Complex wn = Complex ( cos ( rev * * pi / s ) , sin ( rev * * pi / s ) ) , w ( , ) , t ;
for ( int k = ; k < ds ; ++ k , w = w * wn ) {
for ( int i = k ; i < n ; i += s ) {
y[i + ds] = y[i] - ( t = w * y[i + ds] ) ;
y[i] = y[i] + t ;
}
}
}
if ( rev == - ) for ( int i = ; i < n ; ++ i ) y[i].r /= n ;
}
Complex y[N],s[N]; LL dp[N],a[N];
int n;
void cdq(int ll,int rr) {
if(ll == rr) {
dp[ll] += a[ll];
dp[ll] %= mod;
return ;
}
cdq(ll,mid);
int len = ;
while(len <= (rr-ll+)) len<<=;
for(int i = ; i < len ; ++i) y[i] = Complex(,),s[i] = y[i];
for(int i = ; i < len; ++i) y[i] = Complex(a[i+],);
for(int i = ll; i <= mid; ++i) s[i - ll] = Complex(dp[i],);
FFT(s,len,);FFT(y,len,);
for(int i = ; i < len; ++i) s[i] = y[i] * s[i];
FFT(s,len,-);
for(int i = mid; i < rr; ++i)
dp[i+] += LL(s[i-ll].r+0.5),dp[i] %= mod;
cdq(mid+,rr);
}
int main() {
while(scanf("%d",&n)!=EOF) {
if(!n) return ;
memset(a,,sizeof(a));
for(int i = ; i <= n; ++i)
scanf("%lld",&a[i]),dp[i] = ,a[i] = a[i] % mod;
cdq(,n);
printf("%lld\n",dp[n]);
}
return ;
}

HDU 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 5730 分治FFT

    Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell neckl ...

  3. hdu5730 Shell Necklace 【分治fft】

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

  4. 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 ...

  5. 【BZOJ3456】轩辕朗的城市规划 无向连通图计数 CDQ分治 FFT 多项式求逆 多项式ln

    题解 分治FFT 设\(f_i\)为\(i\)个点组成的无向图个数,\(g_i\)为\(i\)个点组成的无向连通图个数 经过简单的推导(枚举\(1\)所在的连通块大小),有: \[ f_i=2^{\f ...

  6. [BZOJ 3456]城市规划(cdq分治+FFT)

    [BZOJ 3456]城市规划(cdq分治+FFT) 题面 求有标号n个点无向连通图数目. 分析 设\(f(i)\)表示\(i\)个点组成的无向连通图数量,\(g(i)\)表示\(i\)个点的图的数量 ...

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

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

  8. HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...

  9. Shell Necklace (dp递推改cdq分治 + fft)

    首先读出题意,然后发现这是一道DP,我们可以获得递推式为 然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法 ...

随机推荐

  1. 也来“玩”Metro UI之磁贴(二)

    继昨天的“也来“玩”Metro UI之磁贴(一)”之后,还不过瘾,今天继续“玩”吧——今天把单选的功能加进来,还有磁贴的内容,还加了发光效果(CSS3,IE9+浏览器),当然,还是纯CSS,真的要感谢 ...

  2. 【Luogu】P3800点收集(DP)

    题目链接 原题解 代码 #include<iostream> #include<cstdio> #include<cstring> #include<ccty ...

  3. BZOJ 3196 二逼平衡树 ——树套树

    [题目分析] 全靠运气,卡空间. xjb试几次就过了. [代码] #include <cmath> #include <cstdio> #include <cstring ...

  4. mock数据。根据表中一天的数据模拟其他日期的数据

    package test; import java.sql.*; import java.text.SimpleDateFormat; import java.util.*; import java. ...

  5. uva 10140 素数筛选(两次)

    #include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...

  6. MySQL中blob 与 text

    BLOB是一个二进制大对象,可以容纳可变数量的数据.有4种BLOB类型:TINYBLOB.BLOB.MEDIUMBLOB和LONGBLOB.它们只是可容纳值的最大长度不同. 有4种TEXT类型:TIN ...

  7. LeetCode OJ--Reverse Linked List II

    http://oj.leetcode.com/problems/reverse-linked-list-ii/ 链表的操作 #include <iostream> using namesp ...

  8. ORA-01033: ORACLE initialization or shutdown in progress问题

    这是Oracle12c中笔者遇到的一个错误提示:ORA-01033: ORACLE initialization or shutdown in progress 错误的中文意思是:Oracle初始化未 ...

  9. 使用Crypto对数据进行加密解密

    注释都在代码里: 先撸客户端: from Crypto.Cipher import AES import base64,requests class Message(object): def __in ...

  10. TOJ 4105

    题意:有10万个点,10万个询问,没有更新,求L1<=L<=L2,R1<=R<=R2,有多少个, 其实转换一下:就是求一个矩形 (L1,R1) ----(L2,R2) 中有多少 ...