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 * 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 (矩阵快速幂)的更多相关文章
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- 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 ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- 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 ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- 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 > ...
- 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 & ...
- HDU - 3521 An easy Problem(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...
- hdu 6182A Math Problem(快速幂)
You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n. Inp ...
随机推荐
- 使用libcurl,根据url下载对应html页面
1. [图片] Capture.JPG 2. [代码]GetPageByURL //static member variable definestring GetPageByURL::m_curPa ...
- the art of seo(chapter eight)
How Social Media and User Data Play a Role in Search Results and Rankings ***Correlation Between Soc ...
- linux命令学习笔记(26):用SecureCRT来上传和下载文件
用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...
- suse enterprise Linux 11上配置 oracle11g和tomcat开机自启动
一.oracle 11g r2自启动 1.修改/etc/sysconfig/oracle文件: ORACLE_BASE=/oracle //此处改为你安装的oracle目录 START_ORACLE ...
- docker镜像管理基础
[root@node01 ~]# docker pull quay.io/coreos/flannel:v0.10.0-amd64 v0.10.0-amd64: Pulling from coreos ...
- 「BZOJ2721」「LuoguP1445」 [Violet]樱花(数论
题目背景 我很愤怒 题目描述 求方程 $\frac{1}{x}+\frac{1}{y}=\frac{1}{N!}$ 的正整数解的组数,其中$N≤10^6$. 解的组数,应模$1e9+7$. 输入输出格 ...
- python爬虫知识点详解
python爬虫知识点总结(一)库的安装 python爬虫知识点总结(二)爬虫的基本原理 python爬虫知识点总结(三)urllib库详解 python爬虫知识点总结(四)Requests库的基本使 ...
- bzoj 3230 相似子串 —— 后缀数组+二分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3230 先算出每个后缀贡献子串的区间: 然后前缀LCP直接查询,后缀LCP二分长度,查询即可: ...
- Ubuntu 16.04 LTS 一键安装VNC
Ubuntu 16.04 LTS 安装VNC,在百度和谷歌找了很多教程,不是太老,就是说的驴唇不对马嘴,所以忍不住写一些以正视听. Ubuntu 16.04 LTS是最近出的LTS版本系统,估计未来也 ...
- angular学习的一些Mark
http://www.cnblogs.com/xianrongbin/p/4104596.html http://angular-ui.github.io/