【模板】矩阵乘法
时间:2019-09-18
本文章向大家介绍【模板】矩阵乘法,主要包括【模板】矩阵乘法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
矩阵乘法基础模板:
在矩阵快速幂中,正对角线为\(1\),其他为\(0\)的矩阵为单位矩阵。
利用单位矩阵进行快速幂。
code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
LL n, k, flag;
struct Matrix {
LL a[105][105];
Matrix() { memset(a, 0, sizeof(a)); }
inline LL *operator [] (int x) { return a[x]; }
Matrix operator * (Matrix b) {
Matrix res;
for(int i = 1; i <= n; i ++) {
for(int k= 1; k <= n; k ++) {
for(int j = 1; j <= n; j ++) {
(res[i][j] += a[i][k] * b[k][j]) %= mod;
}
}
}
return res;
}
} a, base;
Matrix Matrix_ksm(Matrix x, LL y) {
Matrix res = base;
for( ; y ; x = x * x, y >>= 1) {
if(y & 1) res = res * x;
}
return res;
}
int main() {
cin >> n >> k;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
cin >> a[i][j];
}
}
for(int i = 1; i <= n; i ++) base[i][i] = 1;
Matrix ans = Matrix_ksm(a, k);
for(int i = 1; i <= n;i ++) {
for(int j = 1; j <= n; j ++) {
cout << ans[i][j] << " ";
}
cout << endl;
}
return 0;
}
构造矩阵得
\[
\left[\begin{matrix}a_{n+1}\\a_{n}\\a_{n-1} \end{matrix}\right] = \left[\begin{matrix}1&0&1\\1&0&0\\0&1&0 \end{matrix}\right] * \left[\begin{matrix}a_{n}\\a_{n-1}\\a_{n-2} \end{matrix}\right]
\]
套上矩阵快速幂即可
code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
LL t, n;
struct Matrix {
LL a[4][4];
Matrix() { memset(a, 0, sizeof(a)); }
inline LL *operator [] (int x) { return a[x]; }
Matrix operator * (Matrix b) {
Matrix res;
for(int i = 1; i <= 3; i ++) {
for(int k= 1; k <= 3; k ++) {
for(int j = 1; j <= 3; j ++) {
(res[i][j] += a[i][k] * b[k][j]) %= mod;
}
}
}
return res;
}
} ans, base;
void Matrix_ksm(LL y) {
for( ; y ; base = base * base, y >>= 1) {
if(y & 1) ans = ans * base;
}
}
void INIT() {
for(int i = 1; i <= 3; i ++) {
for(int j = 1; j <= 3; j ++) {
base[i][j] = 0;
ans[i][j] = 0;
}
}
base[1][1] = base[1][3] = base[2][1] = base[3][2] = 1;
ans[1][1] = ans[2][1] = ans[3][1] = 1;
}
int main() {
cin >> t;
while(t --> 0) {
cin >> n;
if(n <= 3) { puts("1"); continue; }
INIT();
Matrix_ksm(n-1);
cout << ans[1][1] << endl;
}
return 0;
}
构造矩阵得
\[
\left[\begin{matrix}f_{n}\\f_{n-1}\end{matrix}\right] = \left[\begin{matrix}1&1\\1&0 \end{matrix}\right] * \left[\begin{matrix}f_{n-1}\\f_{n-2}\end{matrix}\right]
\]
同样套上矩阵快速幂即可
code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
LL n;
struct Matrix {
LL a[3][3];
Matrix() { memset(a, 0, sizeof(a)); }
inline LL *operator[] (int x) { return a[x]; }
Matrix operator * (Matrix b) {
Matrix res;
for(int i = 1; i <= 2; i ++) {
for(int k = 1; k <= 2; k ++) {
for(int j = 1; j <= 2; j ++) {
(res[i][j] += a[i][k] * b[k][j]) %= mod;
}
}
}
return res;
}
} ans, base;
void Matrix_ksm(LL y) {
for( ; y ; base = base * base, y >>= 1)
if(y & 1) ans = ans * base;
}
void INIT() {
base[1][1] = base[1][2] = base[2][1] = 1;
ans[1][1] = ans[1][2] = 1;
}
int main() {
cin >> n;
if(n <= 2) return puts("1"), 0;
INIT();
Matrix_ksm(n-2);
printf("%lld\n", ans[1][1]);
return 0;
}
一般构造矩阵的方法就是\(rand()\), 使劲\(rand()\).
原文地址:https://www.cnblogs.com/Paranoid-LS/p/11545440.html
- zookeeper curator使用caches实现各种监听
- python apschedule安装使用与源码分析
- 数据迁移过程中hive sql调优
- 词序:神经网络能按正确的顺序排列单词吗?
- 使用spark对hive表中的多列数据判重
- 如何从微信公众平台上下载关注用户(备份微信关注用户)
- 使用hive客户端java api读写hive集群上的信息
- 大数据算法设计模式(1) - topN spark实现
- redis性能调优笔记(can not get Resource from jedis pool和jedis connect time out)
- thrift例子:python客户端/java服务端
- springboot与thrift集成实现服务端和客户端
- 调用{dede:likewords}为dedecms添加相关搜索词
- 重新调整Keras中长短期记忆网络的输入数据
- Linux内存(手动释放cache)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- python五十六课——正则表达式(常用函数之findall)
- python五十七课——正则表达式(元字符)
- python五十七课——正则表达式(边界字符)
- python五十七课——正则表达式(多个字符)
- python五十八课——正则表达式(分组)
- python五十八课——正则表达式(替换)
- 配置Tomcat使用https协议(单向认证)
- python五十八课——正则表达式(切割)
- python五十九课——正则表达式的拓展内容
- 配置SVN的hooks功能自动更新代码
- python六十课——高阶函数之map
- python六十一课——高阶函数之reduce
- python六十二课——高阶函数之filter
- Nginx优化配置详解
- python六十三课——高阶函数之sorted