HDU 1575 tr

Source: Internet
Author: User

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 ;}

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.