HDU1575-Tr 【矩阵快速幂】(模板题)
<题目链接>
题目大意:
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 <cstring>
#include <iostream>
#include <algorithm> using namespace std;
const int mod = ; struct Matrix
{
int arr[][];
}init,tmp; int n; Matrix Mul(Matrix a, Matrix b) //矩阵相乘
{
Matrix temp;
for(int i=;i<n;i++)
for (int j = ; j < n; j++)
{
temp.arr[i][j] = ;
for (int k = ; k < n; k++)
{
temp.arr[i][j] = (temp.arr[i][j] + a.arr[i][k] * b.arr[k][j] % mod) % mod;
}
}
return temp;
} Matrix Pow(Matrix ans, Matrix a, int x) //快速幂
{
while (x)
{
if (x & )
{
ans = Mul(ans, a);
}
x >>= ;
a = Mul(a, a);
}
return ans;
} int main()
{
int t; scanf("%d", &t);
while (t--)
{
int k;
scanf("%d%d", &n, &k);
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
{
scanf("%d", &init.arr[i][j]);
tmp.arr[i][j] = init.arr[i][j];
}
Matrix ans=Pow(init, tmp, k - ); int res = ;
for (int i = ; i < n; i++)
{
res = (res + ans.arr[i][i]) % mod;
}
printf("%d\n", res);
}
return ;
}
2018-08-08
HDU1575-Tr 【矩阵快速幂】(模板题)的更多相关文章
- luoguP3390(矩阵快速幂模板题)
链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...
- hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)
Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...
- hdu 2604 矩阵快速幂模板题
/* 矩阵快速幂: 第n个人如果是m,有f(n-1)种合法结果 第n个人如果是f,对于第n-1和n-2个人有四种ff,fm,mf,mm其中合法的只有fm和mm 对于ffm第n-3个人只能是m那么有f( ...
- Final Destination II -- 矩阵快速幂模板题
求f[n]=f[n-1]+f[n-2]+f[n-3] 我们知道 f[n] f[n-1] f[n-2] f[n-1] f[n-2] f[n-3] 1 1 ...
- POJ3070 斐波那契数列递推 矩阵快速幂模板题
题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...
- CodeForces 450B (矩阵快速幂模板题+负数取模)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...
- 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> ...
- 51 Nod 1242 斐波那契数列的第N项(矩阵快速幂模板题)
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) ...
- POJ3070:Fibonacci(矩阵快速幂模板题)
http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...
随机推荐
- POJ3177 Redundant Paths【双连通分量】
题意: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以 ...
- PXC中的GTIDs
基本环境:PXC 5.7.19 Row+Gtid,3节点 一.Galera GTID vs MySQL GTID 1.1.Galera GTID vs MySQL GTID Both kinds of ...
- 生成eps图形
(1) matlab可直接将生成图片保存为eps格式. print -fhandle -rresolution -dfileformat filename 例子:set(gcf,'paperposit ...
- RabbitMQ运行机制
AMQP中消息的路由过程和Java开发者熟悉的JMS存在一些差别,AMQP中增加了Exchange和Binding的角色,生产者把消息发布到Exchange上,Binding决定发布到Exchange ...
- MHL技术剖析,比HDMI更强【转】
转自:http://blog.chinaunix.net/uid-22030783-id-3294750.html MHL这个只是经常听说,没有见过的东西,现在已经非常火热了,我们才刚刚开始做,人家三 ...
- telnet不能用!!!提示:-bash: telnet: command not found
1.[root@localhost ~]# telnet 2. 查询了是否安装Telnet包,结果如下: telnet-server-0.17-47.el6.i686 [xinetd (pid ...
- Bootstrap3.0学习第五轮(表格)
详情请查看 http://aehyok.com/Blog/Detail/11.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:h ...
- Ex 5_26 变量约束是否能同时满足(并查集)_第九次作业
利用并查集进行处理,定义一个维护数组components,components[i]表示变量序号为i的变量所处的集合,首先处理相等的变量,把它们放入同一个集合中,最后再处理不相等变量,若两个不相等的变 ...
- PYTHON-模块-time&datetime-练习 +目录规范
# 作业# 1.请写出规范目录# 并解释各文件夹的作用bin 可执行文件conf 配置文件core 主要业务逻辑db 数据文件lib 库(公共代码 第三方模块)log 日志文件 # 2.改造atm + ...
- PYTHON-函数的定义与调用,返回值,和参数-练习
# day10函数的定义调用和参数作业# 1.写函数,用户传入修改的文件名.与要修改的内容,执行函数,完成批量修改操作# def modify_file(filename,old,new):# imp ...