hdu-1575(Tr A)
来源:互联网 发布:wiley数据库使用方法 编辑:程序博客网 时间:2024/05/22 14:11
一道矩阵快速幂的练习题。
题目大意:求矩阵A的k次幂,再余上9973。附链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575。
大体思路:纯练习矩阵快速幂的代码 。
以下是ac代码:
#include<iostream>#include<cstring>using namespace std;const int mod=9973;const int maxn=15;struct Matrix{ long long mat[maxn][maxn];};Matrix unit; //单位矩阵int n;Matrix mul(Matrix a,Matrix b){ Matrix c; for(int i=0;i<n;i++) for(int j=0;j<n;j++){ c.mat[i][j]=0; for(int k=0;k<n;k++){ c.mat[i][j]+=a.mat[i][k]*b.mat[k][j]; c.mat[i][j]%=mod; } } return c;}Matrix mat_pow(Matrix a,long long b){ Matrix temp=unit; while(b){ if(b%2) temp=mul(temp,a); a=mul(a,a); b/=2; } return temp;}int main(){ memset(unit.mat,0,sizeof(unit.mat)); for(int i=0;i<maxn;i++) unit.mat[i][i]=1; int t; cin>>t; while(t--){ long long k; cin>>n>>k; Matrix a; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a.mat[i][j]; Matrix b=mat_pow(a,k); long long result=0; for(int i=0;i<n;i++) result=(result+b.mat[i][i])%mod; cout<<result<<endl; } return 0;}
1 0
- Tr A hdu 1575
- hdu 1575 Tr A
- HDU 1575 Tr A
- HDU 1575 Tr A
- hdu 1575 Tr A
- hdu 1575 Tr A
- hdu 1575 Tr A
- HDU 1575 Tr A
- HDU - 1575 Tr A
- hdu 1575 Tr A
- HDU 1575 Tr A
- HDU 1575 Tr A
- HDU 1575 Tr A
- HDU 1575 Tr A
- HDU 1575 Tr A
- hdu 1575 Tr A
- hdu 1575 Tr A
- HDU 1575 Tr A
- Android中文件的压缩和解压缩
- 【数学】均匀分布生成其他分布的方法
- Python爬虫利器一之Requests库的用法
- 可重入函数设计
- 371. Sum of Two Integers
- hdu-1575(Tr A)
- Python字典的常用方法总结
- C++基础6:虚函数、纯虚函数
- jQuery Mobile 快速入门
- Java多线程-实例解析
- BufferedReader缓冲字符输入流
- C++作业1
- 关于CSS3中多个DIV 盒子垂直叠加及空间堆放z-index计算问题
- CODEFORCES, 34B Sale