HDU - 1575

题目:

A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。 

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——矩阵快速幂问题的更多相关文章

  1. hdu 1575(矩阵快速幂)

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

  2. HDU 1575 矩阵快速幂裸题

    题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...

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

    #include<iostream> #include<vector> #include<string> #include<cmath> #includ ...

  4. hdu 1575 矩阵快速幂模板

    #include "iostream" #include "vector" #include "cstring" using namespa ...

  5. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  6. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  7. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  8. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

  9. 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} ...

随机推荐

  1. HomeBrew及HomeBrew Cask的简介和使用

    前言: 最近在Mac终端上安装SVN时用到了HomeBrew工具,虽然常用,但平时没有过多了解,特翻阅了资料总结一下,以便遗忘. 目录结构: 1. 什么是HomeBrew 2. HomeBrew的主要 ...

  2. mysql in 排序 也可以按in里面的顺序来排序

    SQL: select * from table where id IN (3,9,6);这样的情况取出来后,其实,id还是按3,6,9,排序的,但如果我们真要按IN里面的顺序排序怎么办?SQL能不能 ...

  3. 万能poi导入功能模板

    同时支持2007版本和2003版本,空行过滤,纯数字类型数据格式处理,日期格式处理等 package com.yss.db.util; import com.yss.base.common.excep ...

  4. java EE(1)

    1.安装eclipse javaEE版本,配置好Tomcat服务器(略) 2.新建项目: Dynamic web project 3.创建服务器,并选择项目发布目录 4.添加项目: 右键服务器,选择A ...

  5. jQuery:实现图片按需加载的方法,当要显示内容的高度超过了页面的高度,按需加载,根据滚动条的位置来判断页面显示的内容

    实现图片按需加载的方法,当要显示内容的高度超过了页面的高度,按需加载,根据滚动条的位置来判断页面显示的内容 这个类似于京东或淘宝页面,根绝页面的滚动,显示下面的内容 如下图所示,一开始并不是所有的图片 ...

  6. js 执行机制

    浏览器中每个一个窗口都是一个单独的进程.这就需要分析浏览器与Javascript解释引擎之间的关系.先给出结论,浏览器本身是多线程的,Javascript解释引擎是单线程的. 先说说浏览器有哪些线程, ...

  7. Software Testing 3

    Questions: • 7. Use the following method printPrimes() for questions a–d. 基于Junit及Eclemma(jacoco)实现一 ...

  8. GDScript 格式化字符串

    GDScript offers a feature called format strings, which allows reusing text templates to succinctly c ...

  9. scrapy流程图

    refer:https://blog.yongli1992.com/2015/02/08/python-scrapy-module/ 这里是一张Scrapy架构图的展示.Scrapy Engine负责 ...

  10. C#对象序列化成XML,以及自定义标签名

    C#对象序列化操作: public class XMLHelper { /// <summary> /// 对象序列化成 XML String /// </summary> p ...