HDU - 1575——矩阵快速幂问题
题目:
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
其实,矩阵快速幂问题,和整数快速幂问题是差不多,模板基本相同。
可以自己去对比以下,我博客有关与整数快速幂的随笔。
本题的代码实现如下:
import java.util.Scanner; class Matrix
{
int m[][] = new int[11][11];
}
public class Main
{
static Scanner cin = new Scanner(System.in);
static final int RE = 9973;
public static void main(String []agrs)
{
int T = cin.nextInt();
for(int i = 0; i < T; i++)
{
int n = cin.nextInt();
int k = cin.nextInt();
QuickPower(n,k);
}
}
static void QuickPower(int n,int k)
{
Matrix k1,ans;
k1 = new Matrix();
ans = new Matrix();
for(int i = 0; i < n; i++)//双重循环是得出单位矩阵
{
for(int j = 0; j < n; j++)
{
k1.m[i][j] = cin.nextInt();
if(i == j)
{
ans.m[i][j] = 1;
}
else
{
ans.m[i][j] = 0;
}
}
}
while(k != 0)
{
if((k & 1) != 0)
{
ans = Mul(ans,k1,n);
}
k1 = Mul(k1,k1,n);
k = k>>1;
}
int sum = 0;
for(int i = 0; i < n; i++)
{
sum = (sum + ans.m[i][i])%RE;
}
System.out.println(sum);
}
static Matrix Mul(Matrix a,Matrix b,int n)
{
Matrix result = new Matrix();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
result.m[i][j] = 0;
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
for(int k = 0; k < n; k++)
{
result.m[i][j] = (result.m[i][j] + a.m[i][k]*b.m[k][j])%RE;
}
}
}
return result;
}
}
HDU - 1575——矩阵快速幂问题的更多相关文章
- hdu 1575(矩阵快速幂)
Tr A Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1575 矩阵快速幂裸题
题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...
- Tr A HDU 1575 (矩阵快速幂)
#include<iostream> #include<vector> #include<string> #include<cmath> #includ ...
- hdu 1575 矩阵快速幂模板
#include "iostream" #include "vector" #include "cstring" using namespa ...
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- 随手练——HDU 5015 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...
- HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识
求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...
随机推荐
- sql server实例内存使用统计
转载于: http://blog.csdn.net/shutao917/article/details/51444424 SQL SERVER内存按存放数据的类型,大概可以分为三类: 1.buffer ...
- 小型网站使用高德地图开发定位模块需要的php代码
项目要求: 1.使用定位,获取gps信息 2.获取当前所在城市 3.从该城市中取数据,按照距离我的当前位置远近排序 方案 1.使用js获取当前位置信息,然后使用cookie或者session存储 fu ...
- ORACLE结构体系篇之表空间详解.md
表空间详解一.系统表空间SYSTEM 表空间是Oracle 数据库最重要的一个表空间,存放了一些DDL 语言产生的信息以及PL/SQL 包.视图.函数.过程等,称之为数据字典,因此该表空间也具有其特殊 ...
- Linux之SSH免密登录
实验方法: 开启两台虚拟机A和B,IP地址分别为192.168.222.12.192.168.222.10 在虚拟机A下做如下操作,生成公钥和密钥: [root@localhost ~]# ssh-k ...
- .so相关总结
1.windows 中查看进程依赖那个dll,使用depends,linux使用ldd命令. 2.查看dll中有哪些导出函数windows使用dumpbin,linux使用objdump查看so中有哪 ...
- [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Parco_Love_gcd
传送门 出题人说正解为RMQ,鄙人实在太蒟蒻了,不会呀只能暴力…… #include <bits/stdc++.h> using namespace std; #define ll lon ...
- SLAM领域牛人、牛实验室、牛研究成果梳理
点击公众号"计算机视觉life"关注,置顶星标更快接收消息! 本文阅读时间约5分钟 对于小白来说,初入一个领域时最应该了解的当然是这个领域的研究现状啦.只有知道这个领域大家现在正在 ...
- 《linux就该这么学》第四节课笔记,三章和四章开始!
第三章 (根据课本和在线培训视频排版总结,借鉴请改动) 右键可打开终端练习 3.1:输入输出重定向 输入重定向:符号 "<" ,是一种 ...
- arithmetic-02
Java collection API 中实现的表ADT: collection<E>接口实现继承iterable<E>接口,实现iterable接口的类可以使用增强for循环 ...