今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午。废话不说,入正题。该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了,我直接用个结构体来表示矩阵,确实能省去不少功夫(这里一定要注意用单位矩阵来初次相乘,但不要把它放进构造函数中,我就是在这里卡了好久)。下面附上代码:

 #include<cstdio>
#include<cstring>
const int Mod= ; struct matrix{
int a[][], n;
matrix(int _n){
n =_n;
memset(a,,sizeof(a));
}
void identity(){
for(int i=; i<=n; ++i) //单位矩阵的初始化,切记!
a[i][i]= ; //如果没有这个的话就不能直接相乘了
}
matrix operator *(const matrix m2){
matrix mul(this->n);
for(int i=; i<=n; ++i)
for(int j=; j<=n; ++j)
for(int k=; k<=n; ++k)
mul.a[i][j]= (mul.a[i][j]+ this->a[i][k]*m2.a[k][j]%Mod)% Mod;
return mul;
}
}; matrix quick_mod(matrix &m, int p)
{
matrix ans(m.n);
ans.identity(); // ans一定要为单位矩阵的!
while(p){
if(p&) ans= ans*m;
m= m*m;
p>>=;
}
return ans;
} int main()
{
int t,n,k,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
matrix m(n);
for(i=; i<=n; ++i)
for(j=; j<=n; ++j)
scanf("%d",&m.a[i][j]);
matrix ans(n);
ans= quick_mod(m,k);
int sum= ;
for(i=; i<=n; ++i)
sum= (sum+ ans.a[i][i])%Mod;
printf("%d\n",sum);
}
return ;
}

hdu 1575 Tr A(矩阵快速幂)的更多相关文章

  1. HDU.1575 Tr A ( 矩阵快速幂)

    HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...

  2. HDU 1575 Tr A(矩阵高速幂)

    题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...

  3. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  4. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  5. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  6. 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 ...

  7. 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...

  8. HDU 6470 Count 【矩阵快速幂】(广东工业大学第十四届程序设计竞赛 )

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6470 Count Time Limit: 6000/3000 MS (Java/Others)    ...

  9. HDU 6395 Sequence 【矩阵快速幂 && 暴力】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)   ...

  10. HDU 5667 Sequence【矩阵快速幂+费马小定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...

随机推荐

  1. ACM题目————已知前序和中序求后序

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ], z ...

  2. java 用socket制作一个简易多人聊天室

    代码: 服务器端Server import java.io.*; import java.net.*; import java.util.ArrayList; public class Server{ ...

  3. WebView的返回功能

    WebView 实现返回到最后一个 在退出 import android.app.Activity; import android.os.Bundle; import android.view.Key ...

  4. view class source code with JAD plugin in Eclipse

    The default class viewer doesn't decompile the class file so you cannot open and check the source co ...

  5. ural 1109,NYOJ 239,匈牙利算法邻接表

    NYOJ 239:http://acm.nyist.net/JudgeOnline/problem.php?pid=239 ural 1109 :http://acm.timus.ru/problem ...

  6. easyUI的window包含一个iframe,在iframe中如何关闭window?

    easyUI的window包含一个iframe,在iframe中如何关闭window? parent.$('#win').window('close');

  7. linux 网络通信

    网络命令 历史上最早的即时通信 1 write linzhiling (内容) ctrl+D结束 这样一封信就写出去了,注意:只有用户在线才可以发送 2 wall (write all) 给所有的用户 ...

  8. 2016年11月17日 星期四 --出埃及记 Exodus 20:8

    2016年11月17日 星期四 --出埃及记 Exodus 20:8 "Remember the Sabbath day by keeping it holy.当记念安息日,守为圣日.

  9. 2016年11月13日 星期日 --出埃及记 Exodus 20:4

    2016年11月13日 星期日 --出埃及记 Exodus 20:4 "You shall not make for yourself an idol in the form of anyt ...

  10. WebForm分页浏览

    1.封装类 //封装类 using System; using System.Collections.Generic; using System.Web; /// <summary> // ...