Title Address: HDU 1575
Matrix high-speed power naked problem.
Beginner matrix high-speed power. I have learned the power of high speed. Today a look at the matrix of high-speed power, the original principle is the same, this is good to do more. Is the use of two points of the idea of continuous multiplication. It just turns the number into a matrix.
The code is as follows:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include < stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include < Set> #include <algorithm>using namespace std;const int mod=9973;int n;struct matrix{int ma[20][20];} Init, Res;matrix Mult (Matrix x, Matrix y) {matrix tmp; int I, j, K; for (i=0;i<n;i++) {for (j=0;j<n;j++) {tmp.ma[i][j]=0; for (k=0;k<n;k++) {tmp.ma[i][j]= (tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod; }}} return tmp;} Matrix Pow (Matrix x, int k) {matrix tmp; int I, J; for (i=0;i<n;i++) {for (j=0;j<n;j++) {tmp.ma[i][j]= (i==j); }} while (k) {if (k&1) Tmp=mult (tmp,x); X=mult (X,X); k>>=1; } return TMP; int main () {int T, I, J, ans, K; scanf ("%d", &t); WhiLe (t--) {scanf ("%d%d", &n,&k); for (i=0;i<n;i++) {for (j=0;j<n;j++) {scanf ("%d", &init.ma[i][j]); }} res=pow (Init,k); ans=0; for (i=0;i<n;i++) {ans= (ans+res.ma[i][i])%mod; } printf ("%d\n", ans); } return 0;}
HDU 1575 Tr A (Matrix high-speed power)