If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .

Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
 
Sample Output
45
104
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int k,n;
struct Matrix
{
int mat[][];
}p;
int aa[];
Matrix mul(Matrix a,Matrix b)
{
Matrix c;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
c.mat[i][j]=;
for(int k=;k<;k++)
c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%n;
}
}
return c;
}
Matrix mod_pow(Matrix x,int n)
{
Matrix res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<;i++)
res.mat[i][i]=;
while(n)
{
if(n&)
res=mul(res,x);
x=mul(x,x);
n>>=;
}
return res;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d%d",&k,&n)!=EOF)
{
if(k<)
{
printf("%d\n",k);
continue;
}
memset(p.mat,,sizeof(p.mat));
for(int i=;i<;i++)
cin>>p.mat[][i];
for(int i=;i<;i++)
p.mat[i][i-]=;
Matrix ans= mod_pow(p,k-);
/*for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
cout<<ans.mat[i][j]<<" ";
cout<<endl;
}*/
int sum=;
for(int i=;i<;i++)
sum+=ans.mat[][i]*(-i);
printf("%d\n",sum%n);
}
}

最新文章

  1. String,StringBuffer
  2. 最近在学习bootstrap的时候用bootstrap的视频教程2.0的引用bootstrap3.0突然发现很多不同,总结了一下
  3. codeforces Round #263(div2) D. Appleman and Tree 树形dp
  4. Android实现贪吃蛇游戏
  5. uva 701
  6. OpenStack Networking
  7. [Android4.4.3] Nubia Z5S Mokee4.4.3 RC2.0 by syhost
  8. 常用Map实现类对比
  9. 微信小程序开发-tabbar组件
  10. 使用DHCP动态管理主机地址
  11. pytest 2.测试用例setup和teardown
  12. BZOJ3531[Sdoi2014]旅行——树链剖分+线段树
  13. doi
  14. 阿里云服务器 CentOS 安装Mysql 5.6
  15. Vuex的深入学习
  16. vue的watcher 关于数组和对象
  17. hibernate 验证异常 javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint
  18. android中实现简单的聊天功能
  19. 字符集(编码)转换_Linux
  20. 随手练——HUD 2609 How many

热门文章

  1. 剑指offer之O(1)算法删除指针所指向的节点
  2. poj 3335 Rotating Scoreboard
  3. Keil C51 中的函数指针和再入函数
  4. Linux read语法及浅析
  5. hadoop深入研究:(十八)——Avro schema兼容
  6. [Tips]ASP.NET MVC 发布到服务器后Model中属性相关的Attribute失效
  7. 上海游侠电动汽车团队招募。iOS,Android,产品经理以及 SEVER 端工程师 - V2EX
  8. springBoot学习
  9. UIWebView加载html 图片大小自适应的方法汇总
  10. [ES6] Module export