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
*/

CodeForcesGym 100735B Retrospective Sequence的更多相关文章

  1. CodeForcesGym 100641B A Cure for the Common Code

    A Cure for the Common Code Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on  ...

  2. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  3. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  4. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  5. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  7. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  8. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  9. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

随机推荐

  1. go语言笔记——切片底层本质是共享数组内存!!!绝对不要用指针指向 slice切片本身已经是一个引用类型就是指针

    切片 切片(slice)是对数组一个连续片段的引用(该数组我们称之为相关数组,通常是匿名的),所以切片是一个引用类型(因此更类似于 C/C++ 中的数组类型,或者 Python 中的 list 类型) ...

  2. DStream 转换操作------有状态转换操作

    import org.apache.spark.SparkConf import org.apache.spark.streaming.{Seconds, StreamingContext} obje ...

  3. selenium3 + python - action_chains源码分析

    ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互.这对于做更复杂的动作非常有用,比如悬停和 ...

  4. 推荐给Web前端开发人员的一些书籍(从基础到架构阶段)

    有很多人问我说作为一个前端开发人员都需要看一些什么书籍,尤其是刚入门的新手,今天我整理了一下推荐给大家,大佬绕过. HTML+CSS+JavaScript 网页设计 从入门到精通 作为一个前端新手,强 ...

  5. 如何解决error LNK2001(转载)

    转自:http://www.cnblogs.com/myzhijie/articles/1658545.html 解决外部符号错误:_main,_WinMain@16,__beginthreadex ...

  6. NHibernate系列学习(一)-看看用NH怎么做增速改查

    1.本次所有代码是在一下环境下运行的 学习系统:win8 64bit 开发环境:VS2013,MSSQL2012 NHibernate版本:NHibernate-4.0.3.GA [文章结尾有本次笔记 ...

  7. hibernate.cfg.xml配置

    hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...

  8. 浅谈Java中的hashcode方法以及equals方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...

  9. Node.js的运行

    nodejs的运行 首先要在你的电脑上下载node.js并安装,大家可以去官网下载 1.第一种方法:去官网下载git,安装好后,在桌面上单击鼠标右键,会有一个Git Bash Here,然后在新建一个 ...

  10. Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事

    Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...