HDU4965-Fast Matrix Calculation(矩阵快速幂)

题目链接

题意:n*k的矩阵A和一个k*n的矩阵B,C = A * B。求M = (C)^(n * n)时,矩阵M中每个元素的和(每个元素都要MOD6)

思路:因为n最大到1000,所以不能直接用矩阵快速幂求AB的n*n次幂,但是可以将公式稍微转换下,M = AB * AB...* AB = A * (BA) *... * (BA) * B,这样BA的n*n -1次幂就能用矩阵快速幂求解,之后再分别乘以A,B即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1005;
const int MOD = 6;

struct mat{
    int s[6][6];
    mat() {
        memset(s, 0, sizeof(s));
    }
    mat operator * (const mat& c) {
        mat ans;
        memset(ans.s, 0, sizeof(ans.s));
        for (int i = 0; i < 6; i++)
            for (int j = 0; j < 6; j++)
                for (int k = 0; k < 6; k++)
                    ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD;
        return ans;
    }
};

int a[N][6], b[6][N], temp[N][N], sum[N][N];
int n, m;

void init() {
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            scanf("%d", &a[i][j]);
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            scanf("%d", &b[i][j]);
}

mat pow_mod(mat c, int k) {
    if (k == 1)
        return c;
    mat a = pow_mod(c, k / 2);
    mat ans = a * a;
    if (k % 2)
        ans = ans * c;
    return ans;
}

int main() {
    while (scanf("%d%d", &n, &m)) {
        if (n == 0 && m == 0)
            break;
        init();
        mat c;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < m; j++)
                for (int k = 0; k < n; k++)
                    c.s[i][j] = (c.s[i][j] + b[i][k] * a[k][j]) % MOD;
        int cnt = n * n - 1;
        mat ans = pow_mod(c, cnt);

        memset(temp, 0, sizeof(temp));
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                for (int k = 0; k < m; k++)
                    temp[i][j] = (temp[i][j] + a[i][k] * ans.s[k][j]) % MOD;
        cnt = 0;
        memset(sum, 0, sizeof(sum));
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < m; k++)
                    sum[i][j] = (sum[i][j] + temp[i][k] * b[k][j]) % MOD;
                cnt += sum[i][j];
            }
        printf("%d\n", cnt);
    }
    return 0;
}

时间: 2024-08-23 03:25:57

HDU4965-Fast Matrix Calculation(矩阵快速幂)的相关文章

hdu 4965 Fast Matrix Calculation(矩阵快速幂)

题目链接:hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N: 矩阵C = A*B 矩阵M=CN?N 将矩阵M中的所有元素取模6,得到新矩阵M' 计算矩阵M'中所有元素的和 解题思路:因为矩阵C为N*N的矩阵,N最大为1000,就算用快速幂也超时,但是因为C = A*B, 所以CN?N=ABAB-AB=AC′N?N?1B,C' = B*A, 为K*K的矩阵,K最大为6,完全可以接受. #include <cstdio> #inc

HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

HDU 4965 Fast Matrix Calculation(矩阵高速幂)

题目大意:给你两个数字n和k,然后给你两个矩阵a是n*k的和b是k*n的,矩阵c = a*b,让你求c^(n*n). 直接求的话c是n*n的矩阵所以是1000*1000.会超时的啊. 能够转化一下:(a*b)^(n*n)=a*(b*a)^(n*n-1)*b.b*a能够得到一个k*k的矩阵,k非常小所以不会超时.高速幂一下就能够了啊. Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 13

ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 233

hdu 5015 233 Matrix (矩阵快速幂)

233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 749    Accepted Submission(s): 453 Problem Description In our daily life we often use 233 to express our feelings. Actually, we may s

HDU 4965 Fast Matrix Calculation 矩阵乘法 乘法结合律

一种奇葩的写法,纪念一下当时的RE. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 #include <string> 8 #include <queue> 9 #include <stack>

HDU 4965 Fast Matrix Caculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 )

HDU 4965 Fast Matrix Calculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 1001 #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 6 typedef long lo

HDU 4965 Fast Matrix Calculation(矩阵快速幂)

HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次,可以变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个只有6x6,就可以用矩阵快速幂搞了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int M = 10; int n, m; int A[N][M], B[M][N], C[M][M], CC[N]

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation                                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description One day, Alice and Bob felt bored again, Bob knows Ali