luogu3390 矩阵快速幂
矩阵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 矩阵快速幂的更多相关文章
- 矩阵快速幂 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: ...
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
随机推荐
- TextOut与DrawText的区别
BOOL TextOut( HDC hdc, // 句柄 int nXStart, // 字符串的开始位置 x坐标 int nYStart, // 字符串的开始位置 y坐标 LPCTSTR lpStr ...
- linux下用scp命令在两个服务器之间传输文件,利用php_scp函数进行文件传输
在linux下利用scp进行文件传输, 从服务器下载文件 scp username@servername:/path/filename /path/filename 上传本地文件到服务器 scp /p ...
- PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unkno…
今天安装安装一个叫得推校园O2O系统的使劲都说连接不上服务器. 下面是安装说明,我直接进行3步骤,导入数据库配置文件,结果就显示题目报错的内容 安装说明: 直接输入程序目录即可 http://loca ...
- windons共享的一些问题
有时候访问共享一直说无法打开共享,但是别人确实是开了共享. 其中可能如下: 1.首先确定网络没有问题,win+R输入cmd,ping对方IP地址,保证是网络是通的,如果不通,关闭共享电脑的防火墙. 2 ...
- Neo4j图数据库从入门到精通
目录 第一章:介绍 Neo4j是什么 Neo4j的特点 Neo4j的优点 第二章:安装 1.环境 2.下载 3.开启远程访问 4.启动 第三章:CQL 1.CQL简介 2.Neo4j CQL命令/条款 ...
- c/c++排坑(4) -- c/c++中返回局部变量
返回c语言中的局部变量 先看一段代码猜猜,打印值: #include <iostream> using namespace std; char * func(); int main() { ...
- 不抛异常的swap函数
namespace AStuff{ template<typename T> class A { public: void swap(A *other) { using std::swap ...
- isinstance、issubclass、反射
一.isinstance.issubclass # isinstance(obj, cls) 检查obj是否是cls的对象 class A(object):pass a = A() print(isi ...
- LeetCode 712. Minimum ASCII Delete Sum for Two Strings
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...
- hdu2013 蟠桃记【C++】
蟠桃记 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...