HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575

Tr A

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7572    Accepted Submission(s): 5539

Problem Description

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

题意概括:如题干

解题思路:矩阵快速幂(本质是二分优化快速进行幂运算)矩阵快速幂模板题

注意:单位矩阵初始化

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cmath>
 5 #define LL long long
 6 using namespace std;
 7 const int MAXN = 11;
 8 const int Mod = 9973;
 9 int N;
10 struct mat
11 {
12     int m[MAXN][MAXN];
13 }base;
14
15 mat muti(mat a, mat b)
16 {
17     mat res;
18     memset(res.m, 0, sizeof(res.m));
19     for(int i = 1; i <= N; i++)
20     for(int j = 1; j <= N; j++){
21             if(a.m[i][j]){
22                 for(int k = 1; k <= N; k++){
23                     res.m[i][k] = (res.m[i][k] + a.m[i][j]*b.m[j][k])%Mod;
24                 }
25             }
26         }
27
28     return res;
29 }
30
31 mat qpow(mat a, int n)
32 {
33     mat res;
34     memset(res.m, 0, sizeof(res.m));
35     for(int i = 1; i <= N; i++) res.m[i][i] = 1;
36     while(n){
37         if(n&1) res = muti(res, a);
38         n>>=1;
39         a = muti(a, a);
40     }
41     return res;
42 }
43
44
45 int main()
46 {
47     int K, T_case;
48     scanf("%d", &T_case);
49     while(T_case--){
50         memset(base.m, 0, sizeof(base.m));
51         scanf("%d %d", &N, &K);
52         for(int i = 1; i <= N; i++){
53             for(int j = 1; j <= N; j++){
54                 scanf("%d", &base.m[i][j]);
55             }
56         }
57         base = qpow(base, K);
58         int ans = 0;
59         for(int i = 1; i <= N; i++){
60             ans = (ans + base.m[i][i])%Mod;
61         }
62         printf("%d\n", ans);
63     }
64
65     return 0;
66 }

原文地址:https://www.cnblogs.com/ymzjj/p/9926648.html

时间: 2024-08-26 15:10:36

HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】的相关文章

矩阵乘法及矩阵链乘的快速幂优化

一.矩阵乘法 1 struct datatype 2 { 3 int a[2][2]; 4 }; 5 datatype multiple(datatype x,datatype y) 6 { 7 datatype res; 8 memset(res.a,0,sizeof(res.a)); 9 for(int i=0;i<=1;i++) 10 { 11 for(int j=0;j<=1;j++) 12 { 13 for(int k=0;k<=1;k++) 14 { 15 res.a[i][

HDU 1575 Tr A(矩阵快速幂)

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5537    Accepted Submission(s): 4161 Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据.每组数据的第一行有n(2 <=

HDU 1575 Tr A(矩阵高速幂)

题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #i

hdu 1575 Tr A(矩阵快速幂入门)

Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2977    Accepted Submission(s): 2217 Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有

题解报告:hdu 1575 Tr A

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Problem Description 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 对应每组数据

hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速幂来解,不用说肯定wa,看题目的通过率也不高,我想会不会有啥坑啊.然而我就是那大坑,哈哈. 不说了,直接说题吧,先讨论k=1,2,3;时的解.这应该会解吧,不多说了: 从第四项开始f(4)=a^1+b^2;f(5)=a^2+b^3;f(6)=a^3+b^5......; 看出来了吧,a上的指数成斐波

HDU 1575 - Tr A

A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. 直接矩阵快速幂取模. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 #define LL long long 6 const int mod=9973; 7 struct P{ 8 int a[15][15]; 9 }c,s; 10 int t,n

51 Nod 1013 3的幂的和 矩阵链乘法||逆元+快速幂

这道题我写了两种写法 一种利用逆元 a/b%mod=a*c%mod; (c是b的逆元)易得2的逆元就是5~~~04: 一种是矩阵快速幂 利用递推式得出结论 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int mod=1000000007; int read(){ int ans=0,f=1,c=getchar(); while(c<'0'||c&

POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10521   Accepted: 7477 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequenc