hdu 1575 Tr A(矩阵快速幂乘法优化算法)
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%。
数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n( <= n <= )和k( <= k < ^)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[,],表示方阵A的内容。
对应每组数据,输出Tr(A^k)%。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 10
#define MOD 9973
int n,k;
struct Matrix
{
int mp[N][N];
}matrix;
Matrix Mul(Matrix a,Matrix b)
{
Matrix res;
int i,j,k;
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
res.mp[i][j] = ;
for(k=;k<n;k++)
res.mp[i][j] = (res.mp[i][j]+(a.mp[i][k]*b.mp[k][j]))%MOD;
}
}
return res;
} Matrix fastm(Matrix a,int b)
{
Matrix res;
memset(res.mp,,sizeof(res.mp));
for(int i=;i<n;i++)
res.mp[i][i] = ;
while(b)
{
if(b&)
res = Mul(res,a);
a = Mul(a,a);
b >>= ;
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&matrix.mp[i][j]);
}
}
Matrix tmp=fastm(matrix,k);
int ans=;
for(int i=;i<n;i++)
{
ans=(ans+tmp.mp[i][i])%MOD;
}
printf("%d\n",ans);
}
return ;
}
hdu 1575 Tr A(矩阵快速幂乘法优化算法)的更多相关文章
- HDU.1575 Tr A ( 矩阵快速幂)
HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...
- hdu 2604 Queuing(矩阵快速幂乘法)
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- HDU 1575 Tr A(矩阵高速幂)
题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...
- hdu 4602 Partition(矩阵快速幂乘法)
Problem Description Define f(n) , we have =+++ =++ =++ =++ =+ =+ =+ = totally ways. Actually, we wil ...
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- HDU 6470 Count 【矩阵快速幂】(广东工业大学第十四届程序设计竞赛 )
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6470 Count Time Limit: 6000/3000 MS (Java/Others) ...
随机推荐
- Binary Search Tree Iterator 解答
Question Implement an iterator over a binary search tree (BST). Your iterator will be initialized wi ...
- 求高精度幂(java)
求高精度幂 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 对数值很大.精度很高的数进行高精度计算是一类十分常见的问题.比如,对国债进行计算就是属于这类问题. 现在要 ...
- HDU 1559 最大子矩阵 (DP)
题目地址:pid=1559">HDU 1559 构造二维前缀和矩阵.即矩阵上的点a[i][j]表示左上方的点为(0,0),右下方的点为(i,j)的矩阵的和.然后枚举每一个矩阵的左上方的 ...
- BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
称号:Harry And Dig Machine 哈哈 最终涨边粉色了,不easy呀.顺便写一道题解吧 题意:给一个m*n的矩阵,然后当中最多由10个有值,求总左上角把全部的值都拿上回到左上角的最小 ...
- VS插件集
Unit Test Generator 很好用的测试插件 注:在VS2015中,改名为Test generator Nunit extension了. ReSharperPlatformVs11 ...
- C#中decimal的用法
decimal拥有比float更高的精度,最高能处理到小数点后面的28位.适合用在财务类等对数字精确度要求比较高的场合. using System; using System.Collections. ...
- 实现android4.4新特性:沉浸式状态栏
先放效果图: 所谓沉浸式状态栏,就是android4.4以后新加入的ui效果,能使最顶部的状态栏自动适宜app顶部的颜色,使两者看起来更像融为一体.下面放上实现代码: requestWindowFea ...
- 设计模式--委托模式C++实现
原文章地址:http://www.cnblogs.com/zplutor/archive/2011/09/17/2179756.html [委托模式 C++实现] 我对.Net的委托模型印象很深刻,使 ...
- win32程序调试OutputDebugString 类似printf格式化输出
有没有win32编程因为打印变量调试程序而头疼呢.方法二的函数完全类似printf.非常完美.方法一:不带参数输出如printf("hello world"); OutputDeb ...
- ListView的小知识
1.设置项目分割功能 android:divider="@android:color/blue" android:dividerHeight="10dp" &l ...