Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1575
Question: multiplication of the matrix of percentages
Idea: Because K is relatively large, I use binary optimization. In the end, I just need to add up the numbers on the main diagonal and finally remove the modulo.
// Danceonly # include <cstdio> # include <cstdlib> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; typedef long ll; const double INF = 100000007; const double EPS = 1e-9; const int maxn = 15; const int mod = 9973; # define min (A, B) (A> B? B: A) # define max (A, B) (A> B? A: B) int maze [40] [maxn] [maxn]; int ans [maxn] [maxn], temp [maxn] [maxn]; bool TT [40]; int N, K; void Fu () {for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) ans [I] [J] = temp [I] [J];} void Init (int A) {for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {int sum = 0; For (int K = 1; k <= N; k ++) sum + = (maze [a] [I] [k] * maze [a] [k] [J]) % MOD; maze [A + 1] [I] [J] = sum % mod ;}} void add (int n) {for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {int sum = 0; For (int K = 1; k <= N; k ++) sum + = (ANS [I] [k] * maze [N] [k] [J]) % MOD; temp [I] [J] = sum % MOD;} Fu ();} void show () {for (int K = 1; k <= 10; k ++) {printf ("k = % d \ n", k); For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) printf ("% d", maze [k] [I] [J]); printf ("\ n ");} printf ("\ n") ;}} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & K); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {scanf ("% d", & maze [1] [I] [J]); ans [I] [J] = maze [1] [I] [J];} For (INT I = 1; I <= 32; I ++) init (I); // show (); int e = 1, CNT = 0; while (k> = e) {e * = 2; CNT ++ ;} memset (TT, 0, sizeof (TT); For (INT I = 0; k; I ++) {If (k> = e) {k-= E; TT [CNT-I] = 1;} else TT [CNT-I] = 0; E/= 2;} For (INT I = 1; I <= CNT; I ++) if (TT [I]) add (I + 1); int sum = 0; For (INT I = 1; I <= N; I ++) sum + = ans [I] [I]; printf ("% d \ n", sum % mod);} return 0 ;}