Matrix 10 point "two" Poj 1575 Tr A poj 3233 Matrix Power Series

Source: Internet
Author: User

POJ 1575 Tr A

Topics Link: http://acm.hdu.edu.cn/showproblem.php?pid=1575

The main idea: A is a square, then tr A is a sign of a (that is, the a^k of the major diagonal), now requires TR (%9973).

The first line of data is a T, which indicates that there is a T group of data.


The first row of each group of data has n (2 <= n <= 10) and K (2 <= K < 10^9) two data. Next there are n rows, each row has n data, and the range of each data is [0,9]. Represents the content of square a.

A naked problem with a matrix high-speed power.

Exercises

#include <iostream> #include <stdio.h> #include <cstring> #define Mod 9973using namespace Std;const int MAX = 11;struct matrix{int v[max][max];}; int N, k, M;    Matrix Mtmul (Matrix A, matrix B)//Seeking matrices A * b{int i, j, K;    Matrix C;            for (i = 0; i < n; i + +) for (j = 0; J < N; j + +) {C.v[i][j] = 0;        for (k = 0; k < n; k + +) C.v[i][j] = (a.v[i][k] * B.v[k][j] + c.v[i][j])% Mod; } return C;}        Matrix Mtpow (Matrix A, int k)//seeking matrix A ^ k{if (k = = 0) {memset (a.v, 0, sizeof (A.V));        for (int i = 0; i < n; i + +) a.v[i][i] = 1;    return A;    } if (k = = 1) return A;    Matrix C = Mtpow (A, K/2);    if (k% 2 = = 0) return Mtmul (c, c); else return Mtmul (Mtmul (c, c), A);    int Solv (Matrix A) {int ans=0;    for (int i=0;i<n;i++) Ans+=a.v[i][i]%mod; return ans;} void out (Matrix A) {for (int. i=0;i<n;i++) {for (int j=0;j< n;j++) printf ("%d", a.v[i][j]);    cout<<endl;    }}int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&k);        Matrix A;        for (int i=0;i<n;i++) for (int j=0;j<n;j++) scanf ("%d", &a.v[i][j]);        Matrix ans;        Ans=mtpow (A,K);        Out (ans);    Cout<<solv (ANS)%mod<<endl; }}

The POJ 3233 Matrix power series has the same matrix high-speed power-up problem, which is mainly used for s (6) = (1 + a^3) * S (3) and S (7) = A + (A + a^4) * S (3), and these two last-time recursion will save a lot of time
#include <iostream> #include <cstring> #include <stdio.h>using namespace std;const int MAX = 32;struct matrix{int v[max][max];}; int N, k, M;    Matrix Mtadd (Matrix A, matrix B)//seeking matrices A + b{int i, J;    Matrix C;    for (i = 0; i < n; i + +) for (j = 0; J < N; j + +) c.v[i][j]= (A.v[i][j]+b.v[i][j])% M; return C;}    Matrix Mtmul (Matrix A, matrix B)//Seeking matrices A * b{int i, j, K;    Matrix C;            for (i = 0; i < n; i + +) for (j = 0; J < N; j + +) {C.v[i][j] = 0;        for (k = 0; k < n; k + +) C.v[i][j] = (a.v[i][k] * B.v[k][j] + c.v[i][j])% M; } return C;}        Matrix Mtpow (Matrix A, int k)//seeking matrix A ^ k{if (k = = 0) {memset (a.v, 0, sizeof (A.V));        for (int i = 0; i < n; i + +) a.v[i][i] = 1;    return A;    } if (k = = 1) return A;    Matrix C = Mtpow (A, K/2);    if (k% 2 = = 0) return Mtmul (c, c); else return Mtmul (Mtmul (C, c), A);    Matrix mtcal (Matrix A, int k)//Ask S (k) = A + A2 + A3 + ... + ak{if (k = = 1) return A;    Matrix B = Mtpow (A, (k+1)/2);    Matrix C = mtcal (A, K/2);   if (k% 2 = = 0) return Mtmul (Mtadd (Mtpow (A, 0), B), C); such as s (6) = (1 + a^3) * S (3).

else return Mtadd (A, Mtmul (Mtadd (A, B), C)); such as s (7) = A + (A + a^4) * S (3)}void out (Matrix A) {for (int i=0;i<n;i++) {for (int j=0;j<n-1;j++) cout<<a.v[i][j]<< ""; cout<<a.v[i][n-1]<<endl; }}int Main () {Matrix A; scanf ("%d%d%d", &n,&k,&m); for (int i=0;i<n;i++) for (int j=0;j<n;j++) scanf ("%d", &a.v[i][j]); Matrix c=mtcal (a,k); Out (C);}




Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Matrix 10 point "two" Poj 1575 Tr A poj 3233 Matrix Power Series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.