UVA11149 矩阵快速幂
首先我们来想一下计算A+A^2+A^3...+A^k。
如果A=2,k=6。那你怎么算
2+22+23+24+25+26 = ?= (2+22+23)*(1+23)
如果A=2,k=7。那你怎么算
2+22+23+24+25+26+27 = ?= (2+22+23)*(1+23)+27
so....同理:
当k是偶数,A+A^2+A^3...+A^k=(E+A^(k/2))*(A+A^2...+A^(k/2))。
当k是奇数,A+A^2+A^3...+A^k=(E+A^(k/2))*(A+A^2...+A^(k/2))+A^k。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define MAXN 50
int n,K;
struct node
{
int mat[MAXN][MAXN];
};
node calcu(node x, node y)
{
node ret;
memset(ret.mat,,sizeof(ret.mat));
for (int i = ; i <= n ; i++)
for (int j = ; j <= n ; j++)
{
for (int k = ; k <= n ; k++)
ret.mat[i][j] = (ret.mat[i][j] + x.mat[i][k] * y.mat[k][j]) % ;
}
return ret;
}
node add(node x,node y)
{
node ret;
memset(ret.mat,,sizeof(ret.mat));
for (int i = ; i <= n ; i++)
for (int j = ; j <= n ; j++)
{
ret.mat[i][j] = x.mat[i][j] + y.mat[i][j];
ret.mat[i][j] %= ;
}
return ret;
}
node pow_mat(node x,int cnt)
{
node ret;
memset(ret.mat,,sizeof(ret.mat));
for (int i = ; i < MAXN ; i++) ret.mat[i][i] = ;
while (cnt)
{
if (cnt & ) ret = calcu(ret,x);
x = calcu(x,x);
cnt >>= ;
}
return ret;
}
node dfs(node cur, int k)
{
if (k == ) return cur;
node res = dfs(cur,k / );
node ans;
ans = add(res,calcu(res,pow_mat(cur,k / )));
if (k & ) ans = add(ans,pow_mat(cur,k));
return ans;
}
int main()
{
while (scanf("%d%d",&n,&K) != EOF)
{
if (n == ) break;
node ans;
for (int i = ; i <= n ; i++)
for (int j = ; j <= n ; j++) {scanf("%d",&ans.mat[i][j]); ans.mat[i][j] %= ;}
node ret = dfs(ans,K);
for (int i = ; i <= n ; i++)
{
printf("%d",ret.mat[i][]);
for (int j = ; j <= n ; j++)
printf(" %d",ret.mat[i][j]);
putchar('\n');
}
putchar('\n');
}
return ;
}
UVA11149 矩阵快速幂的更多相关文章
- Power of Matrix(uva11149+矩阵快速幂)
Power of Matrix Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- uva11149矩阵快速幂
求A+A^1+...+A^n 转换一下变成|A E|,的n+1次方就是|A^(n+1) A^n+...+A+E| |0 E| | 0 ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 51nod 1126 矩阵快速幂 水
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
- hdu2604(递推,矩阵快速幂)
题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...
随机推荐
- zoj 1204 Additive equations
ACCEPT acm作业 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204 因为老师是在集合那里要我们做这道题.所以我很是天 ...
- SELECTION-SCREEN 文本丢失
最近有点无聊....随便找点东西填了... 自从系统上线,经常出现程序的的文本丢失,然后选择界面就变成英文的了....一直在出现,就是解决不了,不知道到底是哪里的问题 严重怀疑是服务器上文件丢失... ...
- Javascript面向对象编程:构造函数的继承
今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个"动物"对象的构造函数. function Animal(){ this.species = & ...
- FileUpload上传与下载
后台代码: public string connstr = "server=128.1.3.113;database=test;uid=sa;pwd=pass"; protecte ...
- 转:Nginx+Apache环境的安装与配置
转:http://www.server110.com/nginx/201404/8817.html 我们依然尽可能采用yum来安装我们需要的软件,由系统官方维护的软件,其安全性和稳定性都值得信赖,并且 ...
- cmd扩展路径
在命令行窗口中,输入for /? 即可得到如下参数解释==== 对一组文件中的每一个文件执行某个特定命令. FOR %variable IN (set) DO command [command-par ...
- WebAPI用法
ASP.NET Web API(一):使用初探,GET和POST数据[Parry] HttpClient + ASP.NET Web API, WCF之外的另一个选择[dudu] 通过这两篇文章让我了 ...
- 个人收集的一些网页上一键云DDOS攻击的网站、IP地址测试,服务器压力测试
http://bbs.itzmx.com/thread-9018-1-1.html #1 - Network Stresser - http://networkstresser.com#2 - Lif ...
- jQuery.Autocomplete实现自动完成功能(详解)
1.jquery.autocomplete参考地址 http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ http://do ...
- 在 Ubuntu 14.04/15.04 上配置 Node JS v4.0.0
大家好,Node.JS 4.0 发布了,这个流行的服务器端 JS 平台合并了 Node.js 和 io.js 的代码,4.0 版就是这两个项目结合的产物——现在合并为一个代码库.这次最主要的变化是 N ...