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 ...
随机推荐
- app 摇一摇功能
1.重写canBecomeFirstResponder ,并返回YES -(BOOL)canBecomeFirstResponder{ return YES; } 2.重写UIResponder的三个 ...
- debian 中新建或调整 swap 空间
调整 swap 空间之前,需要了解下面几个基本操作: 1. swap 空间是根据 /etc/fstab 中的记录挂载的 2. 可以使用 swapoff 临时关闭 swap 空间,同时可以使用 swap ...
- poj3484 Showstopper 二分
题目地址 二分用的很是巧妙!关键是抽象出问题本质. #include <cstdio> #include <string> #include <cstring> ; ...
- WPF元素可视化效果
在WPF中设置元素的可视化效果主要用到BlurEffect类和DropShadowEffect类.(目前只学到这两个,哈哈) 1.BlurEffect类 命名空间: System.Windows.Me ...
- Linux进程关闭和后台运行解析
1.问题背景 Java是跨平台的,大部分程序也都是在Linux服务器上运行的.但是很多朋友其实对服务器了解并不多,对相关知识也是一知半解.很多概念可能知道,但是并不十分清楚,仅仅是基本运用.可能很多新 ...
- 一种M2M业务的架构及实现M2M业务的方法
http://www.cnblogs.com/coryxie/p/3849764.html 技术领域 [0001] 本发明涉及通信技术领域,尤其涉及一种M2M业务的架构及实现M2M业务的方法. 背景技 ...
- wpf arcglobe +c# 三维缩放到图层
/// <summary> /// 地图缩放到图层 /// </summary> /// <param name="s ...
- 三大WEB服务器对比分析(apache ,lighttpd,nginx)
一.软件介绍(apache lighttpd nginx) 1. lighttpd Lighttpd是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点.lighttpd是众多 ...
- win7下exe提示无法正常启动(0xc0000906)
本人遇见是 avast问题,卸了
- Zookeeper集群的安装和使用
Apache Zookeeper 由 Apache Hadoop 的 Zookeeper 子项目发展而来,现已经成为 Apache 的顶级项目,它是一个开放源码的分布式应用程序协调服务,是Google ...