Shell Necklace

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 534    Accepted Submission(s): 227

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.

 
Author
HIT
 
题意:
一串由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的更多相关文章

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

  2. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

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

  4. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

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

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

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

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

  9. 2016 Al-Baath University Training Camp Contest-1 B

    Description A group of junior programmers are attending an advanced programming camp, where they lea ...

随机推荐

  1. Mac OSX定位命令路径的方法

    可以使用which命令来定位一个命令. http://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/

  2. WebPack常用功能介绍

    概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...

  3. ThinkPHP真正疑难问题笔记

    如何选择线程安全版本还是非线程安全版本: http://www.cnblogs.com/Alight/p/3389113.html(看webserver处理请求时, 使用的是多线程的方式还是 多进程的 ...

  4. jenkins自动化构建iOS应用配置过程中遇到的问题

    最近配置jenkins来自动构建iOS应用,期间遇上不少问题.在这里分享给大家,也给自己留个底,方便下次解决问题. 首先说明下基本情况,我们因为部署jenkins的机器不是Mac,所以不能安装Xcod ...

  5. 如何用angularjs给从后台传来数据添加链接

    <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF- ...

  6. 将字符串转换成JSON对象

    import net.sf.json.*; JSONObject jsStr = JSONObject.fromObject(params); //将字符串{"id":1} int ...

  7. orm 语法 数据库连接、建表、增删改查、回滚、单键关联 、多键关联、三表关联

    1.数据库连接, #!usr/bin/env/python # -*- coding:utf-8 -*- # from wangteng import sqlalchemy from sqlalche ...

  8. win10预览版9926升级10049操作步骤

    文章转自:豆豆系统收藏备用 win10预览版系统安装的用户非常多,现在最新版本已经到了10049,但是之前很多装了9926版本或者10041版本的同学在通过系统自动更新的时候,且发现,微软官方提供的速 ...

  9. 解决weblogic.net.http.SOAPHttpsURLConnection incompatible with javax.net.ssl.HttpsURLConnection

    1. 按照网上的办法,可以修改代码解决问题,但是由于我们使用的是别人的jar包,不能修改代码,: URL url = new URL(null, "https://www.baidu.&qu ...

  10. c/c++连接mysql数据库

    环境:win7 x64.vs2008.mysql 对于已经安装mysql的,查看mysql安装目录,如果安装目录下没有include和lib目录, 说明没有完全安装,需要下载mysql-connect ...