矩阵A乘矩阵B是A的第i行向量乘以B的第j列向量的值放在结果矩阵的i行j列。因为矩阵乘法满足结合律,所以它可以与一般的快速幂算法同理使用。注意矩阵在乘的时候取模。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define ll long long const int MAX_N = 110;
const ll P = 1e9 + 7; struct Matrix
{
ll A[MAX_N][MAX_N];
int N; Matrix(int n)
{
N = n;
memset(A, 0, sizeof(A));
} void operator = (const Matrix& a)
{
memcpy(A, a.A, sizeof(a.A));
N = a.N;
} void operator *= (const Matrix& a)
{
Matrix ans(N);
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
for (int k = 1; k <= N; k++)
ans.A[i][j] += (A[i][k] % P) * (a.A[k][j] % P) % P;
*this = ans;
} void operator %= (const ll x)
{
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
A[i][j] %= x;
} void SetAnsUnit()
{
memset(A, 0, sizeof(A));
for (int i = 1; i <= N; i++)
A[i][i] = 1;
} bool Empty()
{
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
if (A[i][j])
return false;
return true;
} void Print()
{
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
printf("%lld ", A[i][j]);
printf("\n");
}
}
}; Matrix Power(Matrix a, ll n)
{
Matrix ans(a.N);
ans.SetAnsUnit();
while (n)
{
if (n & 1)
{
ans *= a;
ans %= P;
}
a *= a;
a %= P;
n >>= 1;
}
return ans;
} int main()
{
int n;
long long k;
scanf("%d%lld", &n, &k);
static Matrix a(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &a.A[i][j]);
Power(a, k).Print();
return 0;
}

  

luogu3390 矩阵快速幂的更多相关文章

  1. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  2. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  3. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  4. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  5. 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 ...

  6. 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 输 ...

  7. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  8. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  9. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

随机推荐

  1. java学习笔记_网络

    客户端 import java.io.*; import java.net.*; public class DailyAdviceClient { public void go() { try { S ...

  2. Laravel (5.5.33) 加载过程---make方法(四)

    /** * Resolve the given type from the container. * * @param string $abstract * @return mixed */ publ ...

  3. python框架之Flask基础篇(三)-------- 模版的操作

    1.flask特有的变量和函数: 变量:g.session.request.config 函数:url_for().get_flashed_messages()这个函数注意了啊,记住这是个函数,别忘了 ...

  4. html5——颜色

    CSS2 1.opacity,可以设置透明度,但是父盒子设置了透明度会影响子盒子 CC3 1.transparent属性,但是不可改变透明值 2.rgba():r--red g--green b--b ...

  5. JS——回调函数

    1.回调函数作为参数的形式进行使用 2.回调函数一定程度上达到了解耦效果(模块化.功能化) <script> console.log(op(1, 2, add)); console.log ...

  6. 三年半Java后端面试鹅厂,三面竟被虐的体无完肤

    经过半年的沉淀,加上对MySQL,redis和分布式这块的补齐,终于开始重拾面试信心,再次出征. 鹅厂 面试职位: go后端开发工程师,接受从Java转语言 都知道鹅厂是cpp的主战场,而以cpp为背 ...

  7. java数据类型和码表、转义字符

      类型名称 字节空间 范围 整数型 byte 1 -27到27-1   或者   -128到127   short 2 -215到215-1   int 4 -231到231-1   long 8 ...

  8. openstack——cinder服务篇

    一.cinder 介绍:     理解 Block Storage 操作系统获得存储空间的方式一般有两种: 通过某种协议(SAS,SCSI,SAN,iSCSI 等)挂接裸硬盘,然后分区.格式化.创建文 ...

  9. Getmemory问题

    题目一: [cpp] view plaincopy void GetMemory( char *p ) { p = ( ); } void Test( void ) { char *str = NUL ...

  10. uva-156(Ananagrams UVA - 156)

    map容器的模板题,判断是否能交换字母顺序变成另外一个单词,只需要先把单词都变成小写字母.然后再按字母字典序排序,放入map中进行计数,然后把计数为一的再放入另一个容器,再排序输出即可 我的代码(刘汝 ...