Retrospective Sequence

Time Limit: Unknown ms
Memory Limit: 65536KB

This problem will be judged on CodeForcesGym. Original ID: 100735B
64-bit integer IO format: %I64d      Java class name: (Any)

 

Retrospective sequence is a recursive sequence that is defined through itself. For example Fibonacci specifies the rate at which a population of rabbits reproduces and it can be generalized to a retrospective sequence. In this problem you will have to find the n-th Retrospective Sequence modulo MOD = 1000000009. The first (1 ≤ N ≤ 20) elements of the sequence are specified. The remaining elements of the sequence depend on some of the previous N elements. Formally, the sequence can be written as Fm = Fm - k1 + Fm - k2 + ... + Fm - ki + ... + Fm - kC - 1 + Fm - kC. Here, C is the number of previous elements the m-th element depends on, 1 ≤ ki ≤ N.

 

Input

The first line of each test case contains 3 numbers, the number (1 ≤ N ≤ 20) of elements of the retrospective sequence that are specified, the index (1 ≤ M ≤ 1018) of the sequence element that has to be found modulo MOD, the number (1 ≤ C ≤ N) of previous elements the i-th element of the sequence depends on.

The second line of each test case contains N integers specifying 0 ≤ Fi ≤ 10, (1 ≤ i ≤ N).

The third line of each test case contains C ≥ 1 integers specifying k1, k2, ..., kC - 1, kC (1 ≤ ki ≤ N).

 

Output

Output single integer R, where R is FM modulo MOD.

 

Sample Input

Input
2 2 2
1 1
1 2
Output
1
Input
2 7 2
1 1
1 2
Output
13
Input
3 100000000000 3
0 1 2
1 2 3
Output
48407255

Source

 
解题:矩阵快速幂
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = ;
LL n,N,M,C;
struct Matrix{
LL m[][];
void init(){
memset(m,,sizeof m);
}
void setOne(){
init();
for(int i = ; i < ; ++i) m[i][i] = ;
}
Matrix(){
init();
}
Matrix operator*(const Matrix &rhs) const{
Matrix ret;
for(int k = ; k <= n; ++k)
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
ret.m[i][j] = (ret.m[i][j] + m[i][k]*rhs.m[k][j]%mod)%mod;
return ret;
}
void print(){
for(int i = ; i <= n; ++i){
for(int j = ; j <= n; ++j)
cout<<m[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
};
Matrix a,b;
void quickPow(LL index){
//Matrix ret;
//ret.setOne();
while(index){
if(index&) a = a*b;
index >>= ;
b = b*b;
}
//a = a*ret;
}
int main(){
while(~scanf("%I64d%I64d%I64d",&N,&M,&C)){
a.init();
b.init();
n = N;
for(int i = ; i <= N; ++i){
scanf("%I64d",&a.m[][i]);
b.m[i+][i]++;
}
for(int i = ,tmp; i <= C; ++i){
scanf("%d",&tmp);
b.m[N + - tmp][n]++;
}
if(M <= N){
printf("%I64d\n",a.m[][M]%mod);
continue;
}
quickPow(M - N);
printf("%I64d\n",a.m[][n]%mod);
}
return ;
}
/*
2 3 2
1 1
1 2 3 5 3
0 1 2
1 2 3
*/

最新文章

  1. Android SDK 与API版本对应关系
  2. iOS瀑布流实现(Swift)
  3. Maven之 聚合与继承 详解
  4. asd
  5. Linux grep命令和正则表达式
  6. eclipse中建python项目并运行
  7. 快速上手如何使用FluentData
  8. javascript原生dom操作方法
  9. 30-Razor语法基础
  10. leetcode 233 Number of Digit One
  11. noip2006T1 能量项链
  12. POJ3690+位运算
  13. C# 动态创建出来的窗体间的通讯 delegate3
  14. 数据分析:Weka,Matlab,R,SPSS,SAS等分析软件的入门
  15. ELK监控系统nginx / mysql慢日志
  16. 201521123098 《Java程序设计》第1周学习总结
  17. [原创]同一个Tomcat,配置多个context、多个Host
  18. ssm上传图片
  19. Highways---poj1751最小生成树
  20. CAD批量合并文件

热门文章

  1. (多项式)因式分解定理(Factor theorem)与多项式剩余定理(Polynomial remainder theorem)(多项式长除法)
  2. P3959 宝藏 状压dp
  3. P4166 [SCOI2007]最大土地面积
  4. day02_12/12/2016_bean的实例化之静态工厂方式
  5. 【LeetCode】105 &amp; 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
  6. MyBatis分页组件--PageHelper
  7. cplusplus系列&gt;utility&gt;pair
  8. mysql和java的时间对应关系
  9. 注解是建立在class文件基础上的东西,同C语言的宏有异曲同工的效果
  10. gitlab 第1次提交代码到1个新仓库