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 ...
随机推荐
- ABAP知识点笔记
1,获取光标所在行 READ TABLE TD_ALV_DATA INTO TH_ALV_DATA INDEX RS_SELFIELD-TABINDEX. 2,获取alv可编辑单元格内容 DATA: ...
- c#将前端传来的Json解析成对象
描述:因工作中需要将C#中的Json字符串转换为对象,对此记录下. 解决办法: 1.前端传过来的Json字符串,OrderAppModuleJson即前端传递到后端的Json字符串 string st ...
- Flask最强攻略 - 跟DragonFire学Flask - 第五篇 做一个用户登录之后查看学员信息的小例子
需求: 1. 用户名: oldboy 密码: oldboy123 2. 用户登录成功之后跳转到列表页面 3. 失败有消息提示,重新登录 4.点击学生名称之后,可以看到学生的详细信息 后端: from ...
- 虚拟树研究-CheckBox初步判断只能在第一列
//虚拟树研究-CheckBox初步判断只能在第一列 procedure TWindowsXPForm.XPTreeInitNode(Sender: TBaseVirtualTree; ParentN ...
- 2017/05/03 java 基础 随笔
1.硬盘500G 厂商是按照1000计算的 500g=500*1000*1000/1024/1024=465g 2.jdk1.7可以表示二进制了 0b001(b大小写无所谓) 3.进制转换 4.原码, ...
- Statistics in Python
Statistics in Python Materials for the “Statistics in Python” euroscipy 2015 tutorial. Requirements ...
- lxde 的安装和卸载以及注意事项,lubuntu
安装: $ sudo apt install lxde $ sudo apt install lxde-common 安装完毕后,可能没法关机及logout,可以使用如下安装: $ sudo apt ...
- Ex 6_16 旧货销售问题_第七次作业
即可 子问题定义:定义数组B(S,j),其中 B(S,j)表示在子集S中结束位置为j的子问题的最大收益值,其中j的前一个地点有两种情况,第一种情况是某个拍卖会 另一种情况是从家里出发. 递归关系: 初 ...
- 解读使用Daisy-chain(菊花链)方式筛选一定范围内素数的代码
go version go1.11 windows/amd64 本文为解读 参考链接1 中的 菊花链 一节 的示例程序,此程序和 参考链接2 中代码有些类似:前者有范围,后者是无限循环.清楚了 参考链 ...
- 解析神奇的 Object.defineProperty
这个方法了不起啊..vue.js是通过它实现双向绑定的..而且Object.observe也被草案发起人撤回了..所以defineProperty更有必要了解一下了. 几行代码看他怎么用 var a= ...