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} ...
随机推荐
- sourceTree回退撤销commit
https://blog.csdn.net/gang544043963/article/details/71511958
- Oracle启动关闭
启动: [oracle@oracleSigle ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Wed Nov ...
- container_of 例子说明
很早前之前看的linux内核,一直想把container_of记录一下,趁今天想起就记录一下: 内核中的描述 /** * container_of - cast a member of a struc ...
- selenium启动Firefox失败
今天搭建java+selenium环境,搭建几次都失败,总结一下原因 1. selenium启动Firefox,不需要额外的driver 2. Friefox如果没有安装到默认路径C盘,代码中需要修改 ...
- [py]一致性hash原理
1,可变,不可变 python中值得是引用地址是否变化. 2.可hash 生命周期里不可变得值都可hash 3.python中内置数据结构特点 有序不可变 有序可变 无序可变 无序不可变 5.一致性h ...
- 遇到NotificationService: Suppressing notification from package com.example.dell.servicebestpractice by u错误
一般来说是手机设置没有允许通知
- js对象属性名驼峰式转下划线
一.题目示例: 思路: 1.匹配属性名字符串中的大写字母和数字 2.通过匹配后的lastIndex属性获取匹配到的大写字母和数字的位置 3.判断大写字母的位置是否为首位置以及lastIndex是否为0 ...
- haier周的计算原则
现使用oracle的sql表示出haier周, 经过对其生成结果的分析,发现海尔周是以周日到周六分别作为一周的始末, 用到的oracle sql中会涉及到calendar week的定义,还涉及到了I ...
- Hdu2039 三角形
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2039 三角形 Time Limit: 2000/1000 MS (Java/Others) Me ...
- perl 用网易发邮件报错 554 DT:SPM 163 smtp14
查看相关链接,貌似被当成垃圾邮件了,发不出去 554 DT:SPM 163 smtp14,EsCowACHUegmKpdc3giRMQ--.29617S2 1553410599,please see ...