Lele now is thinking about a simple function f(x). 

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 . 

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

Input

The problem contains mutiple test cases.Please process to the end of file. 

In each case, there will be two lines. 

In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )

In the second line , there are ten integers represent a0 ~ a9.

Output

For each case, output f(k) % m in one line.

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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; const int N=10; int n,mod;
int temp[N][N];
int res[N][N],a[N][N];
void mul(int a[][N],int b[][N]) {
memset(temp,0,sizeof(temp));
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
for(int k=0; k<N; k++)
temp[i][j]=(temp[i][j]+a[i][k]*b[k][j]%mod)%mod;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
a[i][j]=temp[i][j];
return ;
}
void QuickPow(int nn) {
memset(res,0,sizeof(res));
for(int i=0; i<N; i++)
res[i][i]=1;
while(nn) {
if(nn&1)
mul(res,a);
mul(a,a);
nn>>=1;
}
return ;
}
int main() {
while(~scanf("%d %d",&n,&mod)) {
memset(a,0,sizeof(a));
for(int i=0; i<N; i++)
scanf("%d",&a[0][i]);
for(int i=1; i<N; i++)
a[i][i-1]=1;
if(n<10) printf("%d\n",n%mod);
else {
QuickPow(n-9);
int ans=0;
for(int i=0; i<N; i++)
ans+=res[0][i]*(9-i)%mod;
printf("%d\n",ans%mod);
}
}
return 0;
}

A Simple Math Problem (矩阵快速幂)的更多相关文章

  1. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  3. A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列

    Lele now is thinking about a simple function f(x).  If x < 10 f(x) = x.  If x >= 10 f(x) = a0 ...

  4. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  5. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  7. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

  8. A Simple Math Problem 矩阵打水题

    A Simple Math Problem Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x & ...

  9. HDU - 3521 An easy Problem(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...

  10. hdu 6182A Math Problem(快速幂)

    You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n.  Inp ...

随机推荐

  1. 大数据 - Zookeeper

    Zookeeper 1.  Zookeeper概念简介: Zookeeper是一个分布式协调服务:就是为用户的分布式应用程序提供协调服务 A.zookeeper是为别的分布式程序服务的 B.Zooke ...

  2. python学习笔记:第八天(模块)

    Python3 模块 脚本上是用 python 解释器来编程,如果从 Python 解释器退出再进入,那么定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办法,把这些定义存放在文件中 ...

  3. EmbarassedBird网站需求规格说明书

    网站概述 一个特别的在线问答游戏 用户环境 小屏手机, 中等屏幕平板电脑, 大屏显示器 使用chrome浏览器将有全部功能, 其他浏览器完备的基本功能 编程语言&开发环境 HTML/CSS/J ...

  4. listen 71

    Creepy People Leave You Cold Jack Nicholson, playing the crazed caretaker in The Shining, makes me r ...

  5. HihoCoder1644 : 完美命名的烦恼([Offer收割]编程练习赛37)(有向图的一笔画问题||欧拉路)

    描述 程序员常常需要给变量命名.给函数命名.给项目命名.给团队命名…… 好的名字可以大大提高程序员的主观能动性,所以很多程序员在起名时都会陷入纠结和烦恼. 小Hi希望给新的项目起个完美的名字.首先小H ...

  6. BZOJ2877:[NOI2012]魔幻棋盘

    浅谈树状数组与主席树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...

  7. SQL 优化总结(二) 索引

     索引 1.索引的建立 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的:合理的索引设计要建立在对各种查询的分析和预测上. 一般来说: (1) 有大量重复值.且经常有范围查询(between, ...

  8. tomcat 开机自启方法

    1.把下面的代码保存为tomcat文件,并让它成为可执行文件 chmod 755 tomcat. #!/bin/bash # # /etc/rc.d/init.d/tomcat # init scri ...

  9. linux下使用c判断文件夹是否为空的小程序

    自己写了一个 判断文件夹是否为空的小代码 //文件夹操作相关的函数的帮助$: man 3 readdir #include <stdio.h> #include <sys/types ...

  10. 30.构建单机多容器环境-故障&31.构建单机多容器环境

    主要的命令是docker run .主要是用它来构建容器 关机打开序列化 31.构建单机多容器环境 构建自己单机的多容器 加入我们做一个应用程序 -d是在后台运行,不会阻塞你的命令行 之前有一个空的a ...