HDU 1575 Tr A 矩阵快速幂
来源:互联网 发布:和讯个股数据 编辑:程序博客网 时间:2024/05/11 17:36
Tr A
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
Input
数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。
Output
对应每组数据,输出Tr(A^k)%9973。
Sample Input
2
2 2
1 0
0 1
3 99999999
1 2 3
4 5 6
7 8 9
Sample Output
2
2686
#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<stack>#include<set>#include<bitset>using namespace std;const int INF=0x3f3f3f3f;const int MOD=9973;const int SIZE=16;typedef long long ll;struct Matrix{ int n; ll Mat[SIZE][SIZE]; Matrix(int a):n(a) { for (int i=0; i<n; i++) for (int j=0; j<n; j++) Mat[i][j]=0; } Matrix operator * (Matrix& x) { Matrix result(n); for (int k=0; k<n; k++) for (int i=0; i<n; i++) for (int j=0; j<n; j++) result.Mat[i][j]=(result.Mat[i][j]+Mat[i][k]%MOD*x.Mat[k][j]%MOD)%MOD; return result; } Matrix operator ^ (ll x) { Matrix temp(n),a(*this); for (int i=0; i<n; i++) temp.Mat[i][i]=1; while (x) { if (x&1) temp=a*temp; a=a*a; x>>=1; } return temp; }};int main(){ cin.sync_with_stdio(false); int T,N,K; cin>>T; while (T--) { cin>>N>>K; Matrix F(N); for (int i=0; i<N; i++) for (int j=0; j<N; j++) cin>>F.Mat[i][j]; F=F^K; ll Ans=0; for (int i=0;i<N;i++) Ans=(Ans+F.Mat[i][i]%MOD)%MOD; cout<<Ans<<endl; } return 0;}
0 0
- hdu 1575Tr 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 ( 矩阵快速幂)
- hdu 1575-Tr A(矩阵快速幂)
- POJ 2187 Beauty Contest
- Django入门教程
- 模态弹出扩展modalPopupExtender
- POJ 3070 Fibonacci 矩阵快速幂
- Epoll代码实例
- HDU 1575 Tr A 矩阵快速幂
- android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态
- POJ 1061 青蛙的约会 拓展欧几里德
- java smtp.sina.com发送邮件解决及编码修正!
- 问题解决——VM虚拟机克隆后网卡启动不起来device eth0 does not seem to be present, delaying initialization
- Role of Internal Search
- nyoj 题目685 查找字符串
- python学习(1)———安装python
- 【数位DP】HDU 2089 不要62