[Matrix multiplication practices] HDU 1575 -- Tr

Source: Internet
Author: User

It is required to solve Tr (a ^ k) % 9937. Be sure not to wait until the final result is reached. Just a moment after each processing (matrix nature: each number in the matrix is divided by/multiplied by the same integer at the same time, and the properties of the matrix remain unchanged (including the trace of the matrix, the rank of the matrix, and the simplest tiered determining factor of the matrix, etc.); otherwise, the number overflows. [Cpp] # include <iostream> # include <cmath> # include <cstring> using namespace std; class Mat {public: int mat [15] [15];}; int n; // dimension, that is, the number of rows in matrix A int MOD = 9973; // The number Mat E after the remainder is required for many questions; void initE () {for (int I = 0; I <15; I ++) E. mat [I] [I] = 1;} Mat operator * (Mat a, Mat B) {int I, j, k; Mat c; for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {c. mat [I] [j] = 0; for (k = 0; k <n; k ++) {c. mat [I] [j] + = (. mat [I] [k] * B. mat [k] [j]);} c. mat [I] [j] % = MOD;} return c;} Mat operator + (Mat a, Mat B) {Mat c; int I, j; for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) c. mat [I] [j] =. mat [I] [j] + B. mat [I] [j]; c. mat [I] [j] % = 9973;} return c;} Mat operator ^ (Mat a, int x) {Mat p = E, q =; while (x> = 1) {if (x % 2 = 1) p = p * q; x/= 2; q = q * q;} return p ;} mat solve (Mat a, int p) {if (p = 1) return a; else if (p & 1) return (a ^ p) + solve (, p-1); else return (a ^ (p> 1) + E) * solve (a, p> 1);} int main () {int testcase; cin> testcase; Mat at, bt; int res; int kp; while (testcase --) {res = 0; initE (); memset (. mat, 0, sizeof (. mat); memset (bt. mat, 0, sizeof (bt. mat); cin> n> kp; for (int I = 0; I <n; I ++) {www.2cto.com for (int j = 0; j <n; j ++) {cin>. mat [I] [j] ;}} bt = at ^ kp; for (int I = 0; I <n; I ++) {res + = bt. mat [I] [I];} cout <res % 9973 <endl;} 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.