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) ...
随机推荐
- Validate Binary Search Tree 解答
Question Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is d ...
- CentOS7安全设置 yum-cron系统自动更新,firewalld防火墙简单使用
PermitRootLogin nosystemctl restart sshd.service; yum -y install firewalld; systemctl start firewall ...
- linux多线程socket编程一些心得
http://hi.baidu.com/netpet/blog/item/2cc79216d9012b54f2de32b9.html 前段时间将新的web模型办到linux上来,用epoll代替了IO ...
- c#将输入的人民币数字金额转换成小写
//// <summary> /// 人民币大小写金额转换 /// </summary> class RMBCapitalization { private const str ...
- VLC-Android和VLC几个关键宏定义的分析
在用SourceInsight分析VLC-Android源码过程中,有几个宏定义在源代码中一直没有找到出处,比如 HAVE_DYNAMIC_PLUGINS和__PLUGIN__,以及MODULE_NA ...
- jQuery中两种阻止事件冒泡的区别
方式一:event.stopPropagation(); 方式二:return false; 但是这两种方式是有区别的.return false 不仅阻止了事件往上冒泡,而且阻止了事件本身.event ...
- ASP.NET通用权限验证组件实现
沙发(SF)通用权限验证组件 开篇 本篇介绍通用权限验证的实现代码思路,总共分为导入参数.解析XML.根据XML配置进行处理.返回结果. 代码架构图 1. 类介绍 1.SFWebPermissio ...
- <runtime> 的 <assemblyBinding> 元素
一.<assemblyBinding> 元素 包含有关程序集版本重定向和程序集位置的信息. <assemblyBinding xmlns="urn:schemas-micr ...
- 冒泡排序 选择排序 插入排序希尔排序 java
双向冒泡 package com.huang; public class _014_bubb_sort { int[] b={1,2}; static int a[]={12,4,35,65,43,6 ...
- Swift语法基础入门一(适合有C, OC开发人员)
Swift开发体验 /*: 创建对象 * OC: alloc initWithXXX 方法 * Swift: (xxx:) */ /*: 调用方法 * OC: [UIColor redColor]; ...