hdu1757 A Simple Math Problem
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.
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.
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
104
/*
裸题
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = ;
ll sz,mod,f[maxn],a[maxn],ans[maxn];
struct mtx{
ll v[maxn][maxn];
void clear(){
memset(v,,sizeof(v));
}
mtx mul(mtx A,mtx B){
mtx C;
C.clear();
for(int i = ;i <= sz;i++){
for(int j = ;j <= sz;j++){
for(int k = ;k <= sz;k++){
C.v[i][j] = (C.v[i][j] + A.v[i][k]*B.v[k][j]) % mod;
}
}
}
return C;
}
mtx pow(mtx A,int n){
mtx R;
R.clear();
for(int i = ;i <= sz;i++) R.v[i][i] = ;
while(n){
if(n&) R = R.mul(R,A);
n >>= ;
A = A.mul(A,A);
}
return R;
}
void get_tr(mtx A){
for(int i = ;i <= sz;i++){
for(int j = ;j <= sz;j++){
ans[j] = (ans[j] + f[i]*A.v[i][j]) % mod;
}
}
}
};
int main(){
ll d,n,m;
while(scanf("%I64d%I64d",&n,&m) == ) {
d = ;
mtx A;
for(int i = ; i <= d; i++) { cin >> a[i]; a[i] %= m; }
for(int i = d; i >= ; i--) { f[i] = d-i; f[i] %= m; }
if(n < ){
cout<<n<<endl;
continue;
}
n++;
A.clear();
memset(ans,,sizeof(ans));
for(int i = ; i <= d; i++) A.v[i][] = a[i];
for(int i = ; i <= d; i++) A.v[i-][i] = ; sz = d;
mod = m;
A = A.pow(A,n-d);
A.get_tr(A);
cout << ans[] << endl;
}
return ;
}
hdu1757 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 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- FZYZ-2071 A Simple Math Problem IX
P2071 -- A Simple Math Problem IX 时间限制:1000MS 内存限制:262144KB 状态:Accepted 标签: 数学问题-博弈论 ...
- A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU 1757 A Simple Math Problem (矩阵乘法)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 5974 A Simple Math Problem
A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
随机推荐
- php des 加密解密实例
des加密是对称加密中在互联网应用的比较多的一种加密方式,php 通过mcrypt扩展库来支持des加密,要在Php中使用des加密,需要先安装mcrypt扩展库 $iv_size = mcrypt_ ...
- 分布式ID生成器
最近会写一篇分布式的ID生成器的文章,先占位.借鉴Mongodb的ObjectId的生成: 4byte时间戳 + 3byte机器标识 + 2byte PID + 3byte自增id 简单代码: imp ...
- 基本概率分布Basic Concept of Probability Distributions 7: Uniform Distribution
PDF version PDF & CDF The probability density function of the uniform distribution is $$f(x; \al ...
- JSTL的if-else表式
JSTL用法,这里不细讲了,主要是if-else的写法: 代码片段: <c:choose> <c:when test="${user.role eq 1 }"&g ...
- 同样有缓冲区,为什么bufferedReader输入流不需要清空缓冲区?而bufferedWriter需要清空缓冲区呢?
当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取, 如果缓冲区数据不足,才会再从文件中读取.清不清空Buf ...
- Android 使用 DownloadManager 管理系统下载任务的方法,android管理系统
从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作.Download Manager ...
- 浅谈DDOS攻击
preface 做过网站运维的同事来说,绝对遇到过DDOS的攻击吧,这样的攻击实属令人头疼,那么今年就说DDOS的攻击与防护吧. 原理 DDOS(Distributed Denial Of Servi ...
- 从Paxos到ZooKeeper-一、分布式架构
本系列为本人读<从Paxos到ZooKeeper>一书的一些读书笔记,仅供学习使用,谢谢. 一.从集中式到分布式 1.1 分布式的定义: 分布式系统是一个硬件或软件组件分布在不同的网络计算 ...
- IIS------IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决办法
链接: http://www.cnblogs.com/mrma/p/3529859.html
- 《Struts2.x权威指南》学习笔记2
在学习了第二章后,我想要将struts分类,修改一下struts.xml的默认读取路径如下图. 在IntelliJ中,resources是struts的默认路径 修改路径,需要在web.xml中添加s ...