2016 Multi-University Training Contest 1 H.Shell Necklace
Shell Necklace
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 534 Accepted Submission(s): 227
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.
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.
1 3 7
4
2 2 2 2
0
54

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.
题意:
一串由n颗珍珠组成的项链,连续的i个珍珠有ci种染色方式。
问n颗珍珠有多少种染色方式?
题解:
显然可知dp方程
f[i] = sigma(f[i - j] * a[j])
由于n很大,可以考虑分治fft解决。 说说这个分治fft:
如果我们需要计算出l~r的f的值。
假设我们已经计算出(l,mid)的f的值
观察方程
f[i] = sigma(f[j] * a[i - j]) 1 <= j < i
= sigma(f[j] * a[i - j]) + sigma(f[k] * a[i - k]) 1<= j <= mid, mid < k < i
所以可以分开计算f[l~mid]对f[mid+1~r]的影响。
具体过程可以看代码。
使用时记得调整一下下标。
class Complex {
public :
double real, image;
Complex(double real = ., double image = .):real(real), image(image) {}
Complex(const Complex &t):real(t.real), image(t.image) {}
Complex operator +(const Complex &t) const {
return Complex(real + t.real, image + t.image);
}
Complex operator -(const Complex &t) const {
return Complex(real - t.real, image - t.image);
}
Complex operator *(const Complex &t) const {
return Complex(real * t.real - image * t.image,
real * t.image + t.real * image);
}
};
const int N = , MOD = ;
const double PI = acos(-.);
class FFT {
/**
* 1. Need define PI
* 2. Need define class Complex
* 3. tmp is need for fft, so define a N suffice it
* 4. dig[30] -> (1 << 30) must bigger than N
* */
private :
static Complex tmp[N];
static int revNum[N], dig[];
public :
static void init(int n) {
int len = ;
for(int t = n - ; t; t >>= ) ++len;
for(int i = ; i < n; i++) {
revNum[i] = ;
for(int j = ; j < len; j++) dig[j] = ;
for(int idx = , t = i; t; t >>= ) dig[idx++] = t & ;
for(int j = ; j < len; j++)
revNum[i] = (revNum[i] << ) | dig[j];
}
}
static int rev(int x) {
return revNum[x];
}
static void fft(Complex a[], int n, int flag) {
/**
* flag = 1 -> DFT
* flag = -1 -> IDFT
* */
for(int i = ; i < n; ++i) tmp[i] = a[rev(i)];
for(int i = ; i < n; ++i) a[i] = tmp[i];
for(int i = ; i <= n; i <<= ) {
Complex wn(cos( * PI / i), flag * sin( * PI / i));
for(int k = , half = i / ; k < n; k += i) {
Complex w(., .);
for(int j = k; j < k + half; ++j) {
Complex x = a[j], y = w * a[j + half];
a[j] = x + y, a[j + half] = x - y;
w = w * wn;
}
}
}
if(flag == -) {
for(int i = ; i < n; ++i) a[i].real /= n;
}
}
static void dft(Complex a[], int n) {
fft(a, n, );
}
static void idft(Complex a[], int n) {
fft(a, n, -);
}
};
Complex FFT::tmp[N];
int FFT::revNum[N], FFT::dig[];
int n, arr[N];
int f[N];
Complex A[N], B[N];
inline void divideAndConquer(int lef, int rig) {
if(lef >= rig) return;
int mid = (lef + rig) >> ;
divideAndConquer(lef, mid);
int m;
for(m = ; m < (rig - lef + ) * ; m <<= );
for(int i = ; i < m; ++i) A[i] = B[i] = Complex();
for(int i = lef; i <= mid; ++i) A[i - lef] = Complex(f[i]);
int len = min(n, m - );
for(int i = ; i < len; ++i) B[i + ] = Complex(arr[i]);
FFT::init(m);
FFT::dft(A, m), FFT::dft(B, m);
for(int i = ; i < m; ++i) A[i] = A[i] * B[i];
FFT::idft(A, m);
for(int i = mid + ; i <= rig; ++i)
f[i] = (f[i] + ((ll)(A[i - lef].real + 0.5))) % MOD;
divideAndConquer(mid + , rig);
}
inline void solve() {
for(int i = ; i < n; ++i) arr[i] %= MOD;
for(int i = ; i <= n; ++i) f[i] = ;
f[] = ;
divideAndConquer(, n);
printf("%d\n", f[n]);
}
int main() {
while(scanf("%d", &n) == && n) {
for(int i = ; i < n; ++i) scanf("%d", &arr[i]);
solve();
}
return ;
}
2016 Multi-University Training Contest 1 H.Shell Necklace的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1 H
Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 2016 Al-Baath University Training Camp Contest-1 E
Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 2016 Al-Baath University Training Camp Contest-1 J
Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- 2016 Al-Baath University Training Camp Contest-1 G
Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...
- 2016 Al-Baath University Training Camp Contest-1 F
Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...
- 2016 Al-Baath University Training Camp Contest-1 B
Description A group of junior programmers are attending an advanced programming camp, where they lea ...
随机推荐
- centos ab命令安装
yum install apr-util -ymkdir abcd abyum -y install yum-utils -yyumdownloader httpd yum localinstall ...
- js 闭包 理解
1.什么是闭包 定义:是指有权访问另一个函数作用域中的变量的函数 创建闭包:在一个函数内部创建另一个函数 基本特点 在返回的匿名函数中 可以调用外部函数的变量 如下例中所示 内部函数(匿名函数) 可以 ...
- 查看修改Linux时区和时间
查看/修改Linux时区和时间 一.时区 1. 查看当前时区 date -R 2. 修改设置时区 方法(1) tzselect 方法(2) 仅限于RedHat Linux 和 CentOS timec ...
- [Redis]发布/订阅
摘要 有这样的一个场景,管理员需要发布一条消息,所有的客户端都要受到通知.然后想到了发布订阅模式.使用redis的发布与订阅实现起来更简单一些,说做就做,这里弄个简单的demo,先模拟下. 核心代码 ...
- 【bzoj4720】[NOIP2016]换教室
题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内容相同的课程同时在不同的 ...
- linux source
清华TUNA镜像源https://mirrors.tuna.tsinghua.edu.cn/ 中科大USTC镜像源 https://mirrors.ustc.edu.cn/ ali http://mi ...
- 【Python基础学习二】定义变量、判断、循环、函数基本语法
先来一个愉快的Hello World吧,就是这么简单,不需要写标点符号,但是需要严格按照缩进关系,Python变量的作用域是靠tab来控制的. print("Hello World" ...
- 孙鑫C++教程留下来的作业--如何让工具栏在原来隐藏的位置出现
--加油,不仅仅是口号! BEGIN---------------------------------- 将工具栏进行停靠.当隐藏后再次点击出现的时候它出现在工具栏顶部了,并没有停靠在原来的位置,如何 ...
- python file
>>> help(open) Help on built-in function open in module __builtin__: open(...) open(name[, ...
- destoon去掉会员注册email验证
修改文件: /module/member/member.class.php 删除61行: //if(!is_email($email)) return $this->_($L['member_e ...