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: ...
随机推荐
- MyBatis框架Maven资源
<!-- MyBatis框架 --> <dependency> <groupId>org.mybatis</groupId> <artifac ...
- HDUOJ---1236 排名(浙大考研题)
排名 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- Permutations [LeetCode]
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- IDEA配置maven
步骤:Setting....或Ctrl+Alt+S
- 20145236 《Java程序设计》实验三实验报告
实验三实验报告 和张亚军同学一组: 实验三实验报告
- BZOJ3993 [SDOI2015]星际战争
二分答案...然后最大流验证是否可行... 没了,好水啊QAQ /************************************************************** Prob ...
- BZOJ3942 [Usaco2015 Feb]Censoring
维护一个栈...如果栈顶出现了要被删除的字符串就全删掉就好了,判断的话...kmp就行了 /****************************************************** ...
- 在完成一个异步任务后取消剩余任务(C#)
完整实例 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...
- S1:运算符
中括号和小括号的用法: 一般而言,对象中的属性的值是通过点(.)运算符来取值,但是考虑到这样一种情况,我们在遍历一个对象的时候,对其中的属性的键(key)是一无所知的,我们怎么通过点(.)来访问呢?这 ...
- Oracle练习题20~33
20.查询score中选学多门课程的同学中分数为非最高分成绩的记录. 21. 查询成绩高于学号为“109”.课程号为“3-105”的成绩的所有记录. 22.查询和学号为108的同学同年出生的所有学生的 ...