HDU1575-Tr A(矩阵高速幂)
题意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
思路:简单的矩阵高速幂
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; //typedef long long ll;
typedef __int64 ll; const int MOD = 9973;
const int N = 15; ll k;
int n; struct mat{
int s[N][N];
mat() {
memset(s, 0, sizeof(s));
}
mat operator * (const mat& c) {
mat ans;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD;
return ans;
}
}; mat state; mat pow_mod(ll k) {
if (k == 1)
return state;
mat a = pow_mod(k / 2);
mat ans = a * a;
if (k % 2)
ans = ans * state;
return ans;
} int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%I64d", &n, &k);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &state.s[i][j]);
mat c = pow_mod(k);
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i == j)
ans += c.s[i][j];
printf("%d\n", ans % MOD);
}
return 0;
}
HDU1575-Tr A(矩阵高速幂)的更多相关文章
- HDU 1575 Tr A(矩阵高速幂)
题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...
- 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> ...
- hdu 1575 Tr A(矩阵高速电源输入)
Tr A Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
- UVA10518 - How Many Calls?(矩阵高速幂)
UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...
- HDU2842-Chinese Rings(递推+矩阵高速幂)
pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...
- HDU2276 - Kiki & Little Kiki 2(矩阵高速幂)
pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...
- uva 10655 - Contemplation! Algebra(矩阵高速幂)
题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...
随机推荐
- hdoj-1289-A Bug's Life【种类并查集】
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- bug 7715339 登录失败触发 ‘row cache lock’ 等待
Bug 7715339 - Logon failures causes "row cache lock" waits - Allow disable of logon delay ...
- UVA 11346 Probability (几何概型, 积分)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321">https://uva ...
- js12--块作用域函数作用域
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Pairs Forming LCM
题目: B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB Description Find the result of ...
- BZOJ2738: 矩阵乘法(整体二分)
Description 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. Input 第一行两个数N,Q,表示矩阵大小和询问组数: 接下来N行N列一共N*N个数,表示这个矩阵: ...
- 自定义input[type="checkbox"]样式
input[type=checkbox] { visibility: hidden; position: relative;} input[type=checkbox]:after { content ...
- 王小川分享AI
王小川的分享:Link
- 开源课程管理系统(CMS):Moodle
开源课程管理系统(CMS):Moodle 一.总结 1.php开发的cms,可借鉴参考用 二.Moodle(百度) Moodle(Modular Object-Oriented Dynamic Lea ...
- 14.inline与namespace使用
#include <iostream> using namespace std; namespace all { //inline作用为默认调用 inline namespace V201 ...