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. Windows10环境搭建Elasticsearch+Kibana+Marvel

    环境: Windows10企业版X64 Elasticsearch-2.4.1 Kibana-4.6.1 Marvel-2.0+ 步骤: 安装Elasticsearch:官网下载Elasticsear ...

  2. EF唯一索引

    this.Property(p => p.Name) .IsRequired() .HasMaxLength()) .HasColumnAnnotation("Index", ...

  3. 4Struts2标签库----青软S2SH(笔记)

    这里弄错了,这一堆属性是<datetimepicker>标签的,不是<doubleselect>标签的 输出错误信息这个不错,挺方便的. 这个树形标签,也用了好多网络,分析如下 ...

  4. [Head First设计模式]身边的设计模式——适配器模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  5. tail -f 和 -F 的用法

    tail -f 和 -F 的用法  Tai 2010-08-16 16:03:18 -f 是--follow[=HOW]的缩写, 可以一直读文件末尾的字符并打印出来."[=HOW]" ...

  6. tyvj1013 找啊找啊找GF

    描述 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见.""诶,别再见啊..."七夕...七夕...七夕这个日子,对于sqybi这种单身的 ...

  7. 试图加载格式不正确的程序。 (异常来自HRESULT:0x8007000B)

    异常来自HRESULT:0x8007000B   缘由:在64位操作系统下IIS发布32位的项目,报“项目依赖的dll无法读取,试图加载格式不正确的程序”错误. 原因:程序集之间的通讯要么全是64位环 ...

  8. [linux]Socket编程的头文件

    socket编程中需要用到的头文件 sys/types.h:数据类型定义 sys/socket.h:提供socket函数及数据结构 netinet/in.h:定义数据结构sockaddr_in arp ...

  9. Fold Change和t分布

    基因表达谱数据 基因表达谱可以用一个矩阵来表示,每一行代表一个基因,每一列代表一个样本(如图1).所有基因的表达谱数据在“gene_exp.txt”文件中存储,第一列为基因的entrez geneid ...

  10. 在OSX和Windows版本Docker上运行GUI程序

    看到很多人在Docker问题区讨论:如何在OS X和Windows的Docker上运行GUI程序, 随手记录几个参考资料: https://github.com/docker/docker/issue ...