题目链接: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 —— 矩阵倍增、矩阵快速幂的更多相关文章

  1. Luogu 3390 【模板】矩阵快速幂 (矩阵乘法,快速幂)

    Luogu 3390 [模板]矩阵快速幂 (矩阵乘法,快速幂) Description 给定n*n的矩阵A,求A^k Input 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵 ...

  2. Luogu T7152 细胞(递推,矩阵乘法,快速幂)

    Luogu T7152 细胞(递推,矩阵乘法,快速幂) Description 小 X 在上完生物课后对细胞的分裂产生了浓厚的兴趣.于是他决定做实验并 观察细胞分裂的规律. 他选取了一种特别的细胞,每 ...

  3. UVa 11149 Power of Matrix(倍增法、矩阵快速幂)

    题目链接: 传送门 Power of Matrix Time Limit: 3000MS      Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...

  4. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

  5. HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂

    题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...

  6. Power of Matrix 等比数列求和 矩阵版!

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  7. HDU1575Tr A(矩阵相乘与快速幂)

    Tr A hdu1575 就是一个快速幂的应用: 只要知道怎么求矩阵相乘!!(比赛就知道会超时,就是没想到快速幂!!!) #include<iostream> #include<st ...

  8. bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化

    3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 613  Solved: 256[Submit][Status] ...

  9. 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)* ...

随机推荐

  1. zookeeper源码 — 四、session建立

    目录 session建立的主要过程 客户端发起连接 服务端创建session session建立的主要过程 用一张图来说明session建立过程中client和server的交互 主要流程 服务端启动 ...

  2. ML | Naive Bayes

    what's xxx In machine learning, naive Bayes classifiers are a family of simple probabilistic classif ...

  3. Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Minimization dp

    原题链接:http://codeforces.com/contest/572/problem/D 题意 给你个数组A和n,k,问你排列A后,下面的最小值是多少. 题解 先排个序,要填充像1,1+k,1 ...

  4. list all of the Oracle 12c hidden undocumented parameters

    http://www.dba-oracle.com/t_12c_hidden_undocumented_parameters.htm select ksppinm, ksppdesc from x$k ...

  5. spring管理事务

    2.1 事务管理器 Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibernate或者JTA等持久化机制所提供的相关平台框架的事务来实现. Spring事务管理器 ...

  6. Android 蓝牙 笔记

    安卓:短信复制机制 软件识别蓝牙 软件可以读取蓝牙的信息 给蓝牙弄上一个类似于短信的东西 并且存一个短信 然后发到客户端 客户端 可以分类  分成表格 形式 这样做的目的是为了 让你的设备可以写蓝牙给 ...

  7. STP 根桥、根port、指定port是怎样选举的

    学习CCNA过程中,对交换机的根桥.跟port以及指定port选举有些迷糊.也度娘了一番,总认为一部分人解释的不够全面精细.通过细致研究终于有了自己的理解,分享给大家,假设纰漏,欢迎指正. STP收敛 ...

  8. ZOJ 3626 Treasure Hunt I(树形dp)

    Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since ...

  9. substr使用注意

    substr使用时要判断起点和长度是否超过了串本身的长度,否则会抛异常

  10. 为基于 x86 的 Android* 游戏选择合适的引擎

    摘要 游戏开发者知道 Android 中蕴藏着巨大的机遇. 在 Google Play 商店的前 100 款应用中,约一半是游戏应用(在利润最高的前 100 款应用中.它们所占的比例超过 90%). ...