hdoj1757 A Simple Math Problem(矩阵快速幂)
构造矩阵。
1,当k<=9时,直接输出;
2,当k >9时,先求原矩阵的(k-9)次幂res矩阵,在求幂的过程中对m取余。最后res矩阵再与矩阵F相乘得到矩阵ans,相乘的过程中对m取余。ans矩阵的第一个元素就是答案。
PS.orz,这道题一气呵成。只不过我好像和大家构造矩阵的方向有点点差别。这不重要!能做出题就妥!
#include<iostream>
#include<cstring>
#define maxn 12
using namespace std;
int f[]={,,,,,,,,,};
struct mat{
int a[maxn][maxn];
};
mat mat_mul(mat x,mat y,int Mod){
mat ans;
memset(ans.a,,sizeof(ans.a));
for (int i=;i<;i++)
for (int j=;j<;j++)
for (int k=;k<;k++){
ans.a[i][j]+=x.a[i][k]*y.a[k][j];
ans.a[i][j]%=Mod;
}
return ans;
}
void mat_pow(mat &res,int k,int Mod){
mat c=res;
k--;
while (k){
if (k&) res=mat_mul(res,c,Mod);
k>>=;
c=mat_mul(c,c,Mod);
}
}
int main(){
mat res;
int k,m;
while (cin >> k >> m){
memset(res.a,,sizeof(res.a));
for (int i=;i<;i++){
cin >> res.a[i][];
res.a[i][i+]=;
}
if (k<=){
cout << f[k]%m << endl;
continue;
}
else mat_pow(res,k-,m);
int ans=;
for (int i=;i<;i++){
ans+=res.a[i][]*f[-i]%m;
}
cout << ans%m << endl;
}
return ;
}
hdoj1757 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的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...
随机推荐
- CentOS7下BIND配置主从服务器和缓存服务器
系统环境:CentOS Linux release 7.4.1708 (Core) 3.10.0-693.el7.x86_64 软件版本:bind-chroot-9.9.4-51.el7_4.1.x ...
- python yaml
一.安装PyYAML http://pyyaml.org/ 二.入门参考 http://www.cnblogs.com/c9com/archive/2013/01/05/2845539.html ht ...
- POJ3687.Labeling Balls 拓扑排序
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...
- array_column()提取二维数组中某个值
<?php $multipleCommodity = array( =>array(), =>array() ); $arr1=array_column($multipleCommo ...
- div浮停在页面最上或最下
div{ position:fixed; bottom:0px; // top:0px; z-index:999; } bottom:0px 浮停在最下面,top:0px 浮停在最上面:z-index ...
- java如何从一段html代码中获取图片的src路径
java如何从一段html代码中获取图片的src路径 package com.cellstrain.icell.Test; import java.util.ArrayList;import java ...
- 2018.09.16 spoj104Highways (矩阵树定理)
传送门 第一次写矩阵树定理. 就是度数矩阵减去邻接矩阵之后得到的基尔霍夫矩阵的余子式的行列式值. 这个可以用高斯消元O(n3)" role="presentation" ...
- 23. Man and His Natural Habitat 人类及其自然栖息地
. Man and His Natural Habitat 人类及其自然栖息地 ① Ecology is that branch of science which concerns itself wi ...
- webuploader php上传视频
webuploader 上传大视频文件 在网上找了一个,自己重新组合了下,两个主要的文件,再加上官方下载的文件.其中有几个重要的点. 1.上传存放视频目录为了测试 直接777 2.fileupload ...
- 通过wsdl生成client 的几种方式
wsimport 位置 %JAVA_HOME%/bin/wsimport.exe 帮助 wsimport -help Usage: wsimport [options] <WSDL_URI> ...