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: ...
随机推荐
- 安装 request模块
python3 requests 安装包下载安装[windows] 听语音 | 浏览:54 | 更新:2016-07-25 17:09 windows下直接使用:easy_install reques ...
- Ansible :一个配置管理和IT自动化工具
编译文章:LCTT https://linux.cn/article-4215-1.html 译者: felixonmars 文章地址:https://linux.cn/article-4215-1 ...
- 安装VC6提示找不到ACME时的解决办法
将安装程序COPY到电脑上1.打开setupwiz.ini,把"acme=acmboot.exe"改为"=acmsetup.exe";2.STF=setup/v ...
- javascript 数组操作 转
javascript之数组操作 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一 ...
- 百度Tera数据库介绍——类似cassandra,levelDB
转自:https://my.oschina.net/u/2982571/blog/775452 设计背景 百度的链接处理系统每天处理万亿级的超链数据,在过去,这是一系列Mapreduce的批量过程,对 ...
- (置顶)js实现超过页面一屏后,点击图标滚动到页面顶部top
<script type="text/javascript">$(document).ready(function() { var ScrolltoTop = $ ...
- oracle Redhat64 安装错误3
问题描述 /usr/bin/ld: cannot find -lxxx 其中xxx即表示函式库文件名称,其命名规则是:lib+库名(即xxx)+.so. 可能原因: 1 安装了,但相对应的lib版本 ...
- 微信浏览器——User Agent
在iPhone 返回 Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gec ...
- Redis persistence demystified - part 2
重写AOF 当AOF文件太大时,Redis将在临时文件重新写入新的内容.重写不会读取旧的AOF文件,而是直接访问内存中数据,以便让新产生的AOF文件最小,重写过程不需要读取磁盘. 重写完成后,Redi ...
- (转载)Java基础知识总结
写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java语言代码把思路体现出来. 学习新技 ...