Tr A(矩阵快速幂)
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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
const int maxn =1e5+5;
const int mod =9973;
typedef long long ll;
using namespace std;
struct mat
{
int a[15][15];
};
int n;
mat Mul(mat a,mat b)
{
mat ans;
memset(ans.a,0,sizeof(ans.a));
for(int t=0;t<n;t++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
ans.a[t][j]=(ans.a[t][j]+a.a[t][k]*b.a[k][j])%mod;
}
}
}
return ans;
}
mat ans;
ll quickPow(ll x)
{
mat res;
memset(res.a,0,sizeof(res));
for(int t=0;t<n;t++)
{
res.a[t][t]=1;
}
while(x)
{
if(x&1)
{
res=Mul(ans,res);
}
ans=Mul(ans,ans);
x>>=1;
}
ll ss=0;
for(int t=0;t<n;t++)
{
ss=(ss+res.a[t][t])%mod;
}
return ss;
}
int main()
{
int T;
cin>>T;
int k;
while(T--)
{
cin>>n>>k;
for(int t=0;t<n;t++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&ans.a[t][j]);
}
}
ll s=quickPow(k);
cout<<s<<endl;
}
return 0;
}
Tr A(矩阵快速幂)的更多相关文章
- HDU.1575 Tr A ( 矩阵快速幂)
HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...
- hdu1575 Tr A 矩阵快速幂模板题
hdu1575 TrA 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 都不需要构造矩阵,矩阵是题目给的,直接套模板,把对角线上的数相加就好 ...
- HDU1575:Tr A(矩阵快速幂模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1575 #include <iostream> #include <string.h> ...
- Tr A(矩阵快速幂)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- (矩阵快速幂)HDU1575 Tr A
Tr A Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Time Limit: 1000/1000 MS (Java/Others) Me ...
- 矩阵快速幂 hud 1575 Tr A 模板 *
Tr A Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 1575 Tr A(矩阵快速幂)
今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午.废话不说,入正题.该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了 ...
- hdu 1575 Tr A(矩阵快速幂,简单)
题目 和 LightOj 1096 - nth Term 类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...
随机推荐
- Java泛型:泛型的定义(类、接口、对象)、使用、继承
地址 http://blog.csdn.net/lirx_tech/article/details/51570138 1. 设计泛型的初衷: 1) 主要是为了解决Java容器无法记忆元素类型的问题 ...
- code2039 骑马修栏杆
欧拉通路: find_circuit(结点i){ 当结点i有邻居时 选择任意一个邻居j: 删除边(i,j)或者做访问标记: find_circuit(结点j): 输出或存储节点i: } 本题注意点的编 ...
- STREAMING #5 题解 3.高位网络
高维网络 [题目描述] 现在有一个 d 维的坐标网格,其中第 i 维坐标的范围是[0,a_i].在这个范围内建立一个有向图:我们把范围内的每个整点(每一维坐标均为整数的点)当做图上的顶点.设点 A(0 ...
- laravel form表单提交
控制器 中间层 中间层
- Texstudio
Texstudio快捷键:Ctrl+T,选中之后按这个可以快速注释
- Java 前台后台数据传递、中文乱码解决方法
1.向前台传递数据;2.向后台传递数据;3.ajax post 提交数据到服务端时中文乱码解决方法;4.数组类型参数传递; 1.向前台传递数据:1.1 字符串数据传递: 这种方式只是单一的向前台传递 ...
- css实现水平伸缩菜单
ul li a{transition:width 500ms ease;} a:hover{width:*;} 高度向上延伸用height:*;margin-top:-*px;//负值实现向上
- Call to undefined function Think\C()
Fatal error: Call to undefined function Think\C() in /alidata/www/default/2017/newyear/simplewind/Co ...
- speedbutton用法
- 【windows】dos命令查看某个文件夹下所有文件目录列表
dos命令 dir展示一个目录中的文件夹和文件列表 /a代表显示隐藏目录