hdoj1575【矩阵快速幂】
2024-09-02 05:26:39
题意:非常清晰!
直接搞吧。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const double pi = acos(-1.0);
const int mod =9973;
const int N = 20;
struct asd{
LL a[N][N];
};
int n;
asd mul(asd a,asd b)
{
asd ans;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
ans.a[i][j]=0;
for(int k=1;k<=n;k++)
{
ans.a[i][j]=(ans.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
}
}
}
return ans;
}
asd quickmul(asd z,int g)
{
asd ans;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
ans.a[i][j]=1;
else
ans.a[i][j]=0;
}
}
while(g)
{
if(g%2)
{
ans=mul(ans,z);
}
g/=2;
z=mul(z,z);
}
return ans;
}
int main()
{
int t;
int i,j,k;
asd s;
scanf("%d",&t);
while(t--)
{
asd A;
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%lld",&A.a[i][j]);
}
}
s=quickmul(A,k);
LL ans=0;
for(i=1;i<=n;i++)
{
ans+=s.a[i][i];
}
printf("%lld\n",ans%mod);
}
return 0;
}
最新文章
- Yii2.0 对的一些简单的操作
- dataGridViewX和数据库的链接之dataGridViewX1.DataSource = ds.Tables[0];
- jmx : ClientCommunicatorAdmin Checker-run
- 从jQuery中学习来的另一种继承方式(技巧)
- 如何在ASP.Net中实现RSA加密
- 【iHMI43 4.3寸液晶模块】demo例程(版本1.02)发布
- 虚拟机centos配置ip
- 在Centos7上安装漏洞扫描软件Nessus
- Android手机开发者模式设置
- [jQuery编程挑战]007 切换数据表格的行列
- [Jquery] 操作html 不常用元素方法大全
- 依赖注入之Autofac使用总结
- iOS 微信消息拦截插件系列教程-附录(服务端成果展示)
- 一些常见的shell命令和git命令
- istio环境搭建for macbook
- 2019.4.1 JMeter中文乱码解决方案
- java调用dll/so文件
- FileReader &; Blob &; File
- bzoj千题计划200:bzoj3106: [cqoi2013]棋盘游戏
- boost boost::asio::read socket.read_some 区别