矩阵基本算法

#include<cstdio>
using namespace std;
int n;
struct matrix
{
int m[15][15]; }ans,base; matrix multi( matrix a,matrix b )//矩阵乘法
{
matrix temp;
for(int i=0;i<n;i++)//n阶矩阵
{
for(int j=0;j<n;j++)
{
temp.m[i][j]=0;
for(int k=0;k<n;k++)
temp.m[i][j]=(temp.m[i][j]+a.m[i][k]*b.m[k][j])%9973;
}
}
return temp;
} matrix fast(matrix a, int k)//矩阵a的k次幂
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(i==j)ans.m[i][j]=1;
else ans.m[i][j]=0;
} while(k)
{
if(k&1)
{
ans=multi(ans,a);
}
a=multi(a,a);
k>>=1;
}
return ans;
} int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
int k;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&base.m[i][j]);
matrix temp=fast(base, k);
int sum=0;
for(int i=0;i<n;i++)
sum=(sum+temp.m[i][i])%9973;
printf("%d\n",sum); } return 0;
}

  

Tr A HDU1575的更多相关文章

  1. HDU1575Tr A(矩阵相乘与快速幂)

    Tr A hdu1575 就是一个快速幂的应用: 只要知道怎么求矩阵相乘!!(比赛就知道会超时,就是没想到快速幂!!!) #include<iostream> #include<st ...

  2. (矩阵快速幂)HDU1575 Tr A

    Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. hdu1575 Tr A 矩阵快速幂模板题

    hdu1575   TrA 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 都不需要构造矩阵,矩阵是题目给的,直接套模板,把对角线上的数相加就好 ...

  4. hdu1575 Tr A 矩阵初识

    A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= ...

  5. HDU1575:Tr A(矩阵快速幂模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1575   #include <iostream> #include <string.h> ...

  6. HDU1575 Tr A

    解题思路:矩阵快速幂模板题,见代码: #include<cstdio> #include<cstring> #include<algorithm> using na ...

  7. table tr foreach td 换行

    @{ ;} <table style=" class="sy_table"> <tr> @foreach (DataRow dr in (View ...

  8. [转]jquery遍历table的tr获取td的值

    html代码: 1 <tbody id="history_income_list"> 2 <tr> 3 <td align="center& ...

  9. table中某一个tr边框样式设置

    <html> <head> <style type="text/css"> table{ width:500px; } table tr td{ ...

随机推荐

  1. 出售爱奇艺 tx好莱坞 优酷 乐视 芒果 搜狐 等一个月会员激活码

    出售爱奇艺 tx好莱坞 优酷 乐视 芒果  搜狐 等一个月会员激活码  进群价格有优惠  QQ群:569333649

  2. CSS——margin

    CSS margin 属性 定义和用法 margin 简写属性在一个声明中设置所有外边距属性.该属性可以有 1 到 4 个值. 说明 这个简写属性设置一个元素所有外边距的宽度,或者设置各边上外边距的宽 ...

  3. Postfix 邮件服务 - postfix服务

    postfix 邮件服务 也是基于sendmail (需要关闭或卸载sendmail)postfix 是一个电子邮件服务器,它为了改良sendmail邮件服务器而产生的,并且它的配置文件比sendma ...

  4. 数据库运维平台~inception回滚功能

    一 简介:inception的另一个激动人心的功能,很强大.二 功能简介: inception会针对已经执行sql语句进行1 记录 2 生成回滚语句三 备份:   1 启用远程备份机制(强烈建议一台单 ...

  5. mysql 查询优化~sql优化通用

    一 简介:今天我们来探讨下SQL语句的优化基础 二 基础规则: 一 通用: 1 避免索引字段使用函数     2 避免发生隐式转换     3 order by字段需要走索引,否则会发生filesor ...

  6. python - class propert应用

    propert应用 #方式一: # class Student(object): # def __init__(self,name,sc): # self.name = name # self.sc ...

  7. python - 上下文管理协议(with + __enter__ + __exit__)

    上下文管理协议: with + __enter__ + __exit__ #上下问管理协议: #with + __enter__ + __exit__ class Test(): def __init ...

  8. session和cookies

    Cookie 与session的产生过程                                 我们都知道HTTP协议本身是无状态的,客户只需要简单的向服务器来发送请求下载某些文件,客户端向 ...

  9. SSD win7优化步骤

    随着固态硬盘价格不断下降,目前固态硬盘也得到了广泛了应用,一些新笔记本以及组装电脑也开始普遍采用固态硬盘平台,超级本就更不用说了,采用固态硬盘已经成标配化,虽然固态硬盘速度很快,但不懂的优化,依然无法 ...

  10. BFGS算法(转载)

    转载链接:http://blog.csdn.net/itplus/article/details/21897443 这里,式(2.38)暂时不知如何证出来,有哪位知道麻烦给个思路.