MatrixPowers-矩阵连乘

本文介绍了一个使用C++实现的矩阵快速幂算法,该算法能够高效地计算矩阵的高次幂,并支持模运算。适用于需要频繁进行矩阵乘法及求幂操作的应用场景,如在数论和组合数学中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>

using namespace std;

#define N 105

#define FF(i,n) for(i=0;i<n;i++)

int ans[N][N];

int init[N][N];

int buf[N][N];

void matrixMul(int a[][N],int b[][N],int n,int mod){

int i,j,k;

FF(i,n)FF(j,n) buf[i][j]=0;

FF(i,n)FF(k,n) if(a[i][k])FF(j,n) if(b[k][j])

buf[i][j]=(buf[i][j]+a[i][k]*b[k][j])%mod;

FF(i,n)FF(j,n) a[i][j]=buf[i][j];

}

void matrixMul(int n,int m,int mod){

int i,j;

FF(i,n)FF(j,n) ans[i][j]=(i==j);

for(;m;m>>=1){

if(m&1) matrixMul(ans,init,n,mod);

matrixMul(init,init,n,mod);

}

}


int main()

{

    int i,j,n,m,mod;

    while (scanf("%d%d%d",&n,&mod,&m)) {

        if (!n&&!m&&!mod)

            break;

        FF(i, n) FF(j, n) scanf("%d",&init[i][j]);

        matrixMul(n,m,mod);

        printf("\n");

        FF(i,n) {

            FF(j,n) {

                if (j)

                    printf(" ");

                printf("%d",ans[i][j]);

            }

            printf("\n");

        }

    }

    return 0;

}

/*

 Problem description

 Your task is to write a program to raise an integer matrix to a given power using modulo arithmetic.There is no back story here; at least not one that can be told. The application is too confidential(spying and military intelligence and all that) to be described in public. For example, to raise the 2 by 2 matrix 

 

 to the power 2 using modulo 17 arithmetic 

 

 

 

 Input

 The input consists of a number of problems. Each problem starts with a line holding three numbers (N, M, and P) separated by single spaces. 1 <= N <= 100 is the size (N by N) of the matrix to be processed. 2 <= M <= 32000 is the modulo base and 1 <= P <= 32000 is the power to which the matrix must be raised. Following this line are N lines, each holding the n integer values of successive rows of the matrix as a series of positive integers i: 0 <= i < M separated by single spaces. Input is terminated by a line with three zeros.

 

 Output

 Output for each problem consists of a blank line, followed by the N rows of the result matrix. Each row is output on a single line as a sequence of integer values separated by single spaces. It doesn´t matter that lines may be quite long-no-one will be allowed to read them anyway.

 

 Sample Input

 2 17 2

 1 2

 3 4

 0 0 0

 Sample Output

 

 7 10

 15 5

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值