1 Star 0 Fork 0

JJustRight/ACM-ICPC-Algorithms

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
matrix_power.cpp 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
OmarKimo 提交于 2018-10-06 18:48 +08:00 . Add Matrix Power code
#include <iostream>
using namespace std;
const int MaxSize = 2; //It depends on the problem (2 for computing Fibonacci series)
const int mod = 1e9+7; //It depends on the problem as you may don't need it
struct Matrix{
long long mat[MaxSize][MaxSize];
};
Matrix matMul(Matrix a , Matrix b)
{
Matrix ans; int k;
for(int i = 0 ; i < MaxSize ; i++)
for(int j = 0 ; j < MaxSize ; j++){
for(ans.mat[i][j] = k = 0 ; k < MaxSize ; k++){
long long sum = (a.mat[i][k] * b.mat[k][j]) % mod;
ans.mat[i][j] = (ans.mat[i][j] + sum) % mod;
}
}
return ans;
}
Matrix Identity(){
Matrix res;
for(int i = 0 ; i < MaxSize ; i++)
for(int j = 0 ; j < MaxSize ; j++)
res.mat[i][j] = (i == j);
return res;
}
Matrix Power(Matrix base, long long pw)
{
Matrix ans = Identity();
while(pw){
if(pw & 1) ans = matMul(ans , base);
base = matMul(base, base);
pw >>= 1;
}
return ans;
}
int main(){
// Matrix Power is a technique not a specific problem
// So I wrote just the template of its code.
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/jjustright/ACM-ICPC-Algorithms.git
git@gitee.com:jjustright/ACM-ICPC-Algorithms.git
jjustright
ACM-ICPC-Algorithms
ACM-ICPC-Algorithms
master

搜索帮助