UVA11149 Power of Matrix —— 矩阵倍增、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-11149
题意:
给出矩阵A,求出A^1 + A^2 …… + A^k 。
题解:
1.可知:A^1 + A^2 …… + A^k = (1+A^k/2)*(A^1 + A^2 …… + A^k/2)+ (k%2?A^k:0)。
2.根据上述式子,可二分对其求解。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
//const int MOD = 1e9+7;
const int MAXN = 1e6+; const int MOD = ;
int Size;
struct MA
{
LL mat[][];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA mul(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += 1LL*x.mat[i][k]*y.mat[k][j]%MOD, ret.mat[i][j] %= MOD;;
return ret;
} MA add(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
ret.mat[i][j] = x.mat[i][j]+y.mat[i][j], ret.mat[i][j] %= MOD;
return ret;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
} MA solve(MA x, int k)
{
if(k==) return x;
MA s;
s.init();
s = mul(add(s, qpow(x, k/)), solve(x, k/));
if(k%) s = add(s, qpow(x, k));
return s;
} int main()
{
int n, k;
while(scanf("%d%d", &n,&k)&&n)
{
MA s;
Size = n;
memset(s.mat, , sizeof(s.mat));
for(int i = ; i<n; i++)
for(int j = ; j<n; j++)
{
scanf("%lld", &s.mat[i][j]);
s.mat[i][j] %= MOD;
} s = solve(s, k);
for(int i = ; i<n; i++)
{
for(int j = ; j<n; j++)
printf("%lld%s", s.mat[i][j], j==n-?"\n":" ");
}
printf("\n");
}
}
UVA11149 Power of Matrix —— 矩阵倍增、矩阵快速幂的更多相关文章
- Luogu 3390 【模板】矩阵快速幂 (矩阵乘法,快速幂)
Luogu 3390 [模板]矩阵快速幂 (矩阵乘法,快速幂) Description 给定n*n的矩阵A,求A^k Input 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵 ...
- Luogu T7152 细胞(递推,矩阵乘法,快速幂)
Luogu T7152 细胞(递推,矩阵乘法,快速幂) Description 小 X 在上完生物课后对细胞的分裂产生了浓厚的兴趣.于是他决定做实验并 观察细胞分裂的规律. 他选取了一种特别的细胞,每 ...
- UVa 11149 Power of Matrix(倍增法、矩阵快速幂)
题目链接: 传送门 Power of Matrix Time Limit: 3000MS Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
- Power of Matrix 等比数列求和 矩阵版!
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- HDU1575Tr A(矩阵相乘与快速幂)
Tr A hdu1575 就是一个快速幂的应用: 只要知道怎么求矩阵相乘!!(比赛就知道会超时,就是没想到快速幂!!!) #include<iostream> #include<st ...
- bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 613 Solved: 256[Submit][Status] ...
- bzoj 3240 矩阵乘法+十进制快速幂
首先,构造出从f[][i]->f[][i+1]的转移矩阵a,和从f[i][m]->f[i+1][1]的转移矩阵b, 那么从f[1][1]转移到f[n][m]就是init*(a^(m-1)* ...
随机推荐
- [翻译] NumSharp的数组切片功能 [:]
原文地址:https://medium.com/scisharp/slicing-in-numsharp-e56c46826630 翻译初稿(英文水平有限,请多包涵): 由于Numsharp新推出了数 ...
- javascript --- 继承小结
回顾之前学到的知识,大体上可以分为两类: 1. 基于构造器工作的模式. 2. 基于对象的工作模式. 3. 是否使用原型 4. 是否执行属性拷贝. 5. 两者都有(执行原型属性拷贝) 下面我们把之前的知 ...
- XCode 4.3 Unable to load persistent store UserDictionary.sqlite 以及 ios simulator failed to install the application
I have been working on an iOS app for some time, all of a sudden I am getting the following crash ev ...
- 【jQuery】方法和选择器的双重使用详解
1.jQuery选择直接子节点+除了某个元素 1>方法 $(".begon").children(".row:not(.moreDetail)") 2&g ...
- NGUI版虚拟摇杆
以下是我用nui实现的一个虚拟摇杆. 1,示图 2.代码例如以下,都有比較具体的凝视.就不说明了. using UnityEngine; using System.Collections; using ...
- 随想录(fatfs的学习)
[ 声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 上学的时候就对文件系统非常有兴趣.可是苦于没有合适的fs代码能够学习.市面上的fs代码,要么太 ...
- css控制打印时只显示指定区域
CreateTime--2017年9月26日08:16:04 Author:Marydon css控制打印时只显示指定区域 思路: 1.使用打印命令@media print: 2.控制执行打印命令 ...
- 解决"Subquery returns more than 1 row"sql查询错误
http://blog.csdn.net/c517984604/article/details/7052186 [Err] 1242 - Subquery returns more than 1 ro ...
- B树的生成
B树的生成 flyfish 2015-7-19 从空树開始构建一棵B树 逐个插入keyword 规则: 除根结点之外的全部非终端结点至少有⌈m/2⌉棵子树,所以keyword的个数必须 n为keywo ...
- css实现轮播效果图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...