hdoj 1575 Tr A 【矩阵快速幂】

Tr A

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3020    Accepted Submission(s): 2251

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

分析:跟快速幂差不多,就是一个是数,一个是矩阵

代码:

#include <stdio.h>
#include <string.h>
#define mod 9973

struct node{
    int map[15][15];
};
node a, b;
int n;
node mul(node a, node b){
    node c;
    int i, j, k;
    for(i = 0; i < n; i ++)
        for(j = 0; j < n; j ++){
            c.map[i][j] = 0;
            for(k = 0; k < n; k ++)
                c.map[i][j] += a.map[i][k]*b.map[k][j];
                c.map[i][j] %= mod;
        }
    return c;
}

node f(int k, node a){
    b = a; k-=1;  //将
    while(k){
        if(k&1) b = mul(b, a);
        k>>=1;
        a = mul(a, a);
    }
    return b;
}

int main(){
    int t, k;
    scanf("%d", &t);
    while(t --){
        scanf("%d%d", &n, &k);
        int i, j;
        for(i = 0; i < n; i ++){
            for(j = 0; j < n; j ++){
                scanf("%d", &a.map[i][j]);
            }
        }
        node ans = f(k, a);
        int res = 0;
        for(i = 0; i < n; i ++) res += ans.map[i][i];
        printf("%d\n", res%mod);
    }
    return 0;
}
时间: 2024-08-05 14:57:25

hdoj 1575 Tr A 【矩阵快速幂】的相关文章

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组数据. 每组数据的第一行有

HDOJ 2294 - Pendant(DP+矩阵快速幂)

题目链接 题意:有个高富帅,要送个很装逼的吊坠给他女朋友.他有k种珠子,然后要串成一个珠子个数小于等于n 的链子(k种珠子都必须要用到).输入n和k,输出他可以做出多少种不一样的项链. 思路:可以想到递推式f(x, y) = f(x – 1, y) * y + f(x – 1, y – 1) * (k – y + 1)(表示x个珠子用了y种类型),因为n过大,无法直接求出来,所以要使用矩阵快速幂. GG队友的思路 代码: #include <iostream> #include <cst

hdoj 4062 Queuing 【矩阵快速幂优化递推公式】

Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3609    Accepted Submission(s): 1629 Problem Description Queues and Priority Queues are data structures which are known to most computer

HDU 1575 Tr A(矩阵高速幂)

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

HDU1575:Tr A(矩阵快速幂模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1575 #include <iostream> #include <string.h> #include <stdlib.h> #include <cstdio> #include <algorithm> #define mod 9973 using namespace std; struct matrix { int a[11][11]; } init,res

hdu 1575 try a 矩阵快速幂

#include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<algorithm> #include<iostream> using namespace std; #define ll long long int const int m=9973; ll

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的迹(就是主对角线上各项的和),现要

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 &amp;&amp; 1757 矩阵快速幂&amp;&amp;构造矩阵入门

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

Tr A(矩阵快速幂)

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