HDU 4965 fast matrix calculation matrix fast power

Source: Internet
Author: User

Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4965

Question: a matrix of N * K, a matrix of K * n, B (4 <= n <=, 2 <= k <= 6 ). First, perform matrix multiplication c = a * B, then perform the power operation of D = C ^ (N * n), and then modulo 6 for each number of D, finally, the sum of all the positions and numbers of D is obtained.

Idea: Like the previous matrix multiplication, it is impossible to directly multiply a very large matrix without the help of small rules (currently, even the strassen matrix algorithm will not speed up to the required) the C matrix given in the question is a 1000*1000 matrix, and the rapid power is timed out. So I noticed that the number of columns in matrix A and the number of rows in matrix B cannot exceed six, however, it is no problem to perform a rapid power operation on the 6*6 matrix. D = a * B *... * a * B, which can be viewed as D = A * (B * *... * B * A) * B. In this way, the relationship between left multiplication and right multiplication of the matrix is not affected, and the number of rows and columns is too large. (The small details in the question are very powerful. First, the stack explosion problem needs to be solved, and then the matrix assignment operation is also the complexity of O (N)

Code:

# Pragma comment (linker, "/Stack: 1024000000,1024000000 ") # include <algorithm> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # include <ctime> # include <ctype. h> # include <iostream> # include <map> # include <queue> # include <set> # include <stack> # include <string> # include <vector> # define EPS 1e-8 # define INF 0x7fffffff # define PI ACOs (-1.0) # define seed 31 // 131,1313 typedef long ll; typedef unsigned long ull; # define mod 6 # define maxn 1005 # define maxm 1005 using namespace STD; struct matrix {int N, m; int A [maxn] [maxm]; void Init () {n = m = 0; memset (A, 0, sizeof ());} matrix Operator + (const Matrix & B) const {matrix TMP; TMP. N = N; TMP. M = m; For (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) TMP. A [I] [J] = A [I] [J] + B. A [I] [J]; return TMP;} Matrix Operator-(const Matrix & B) const {matrix TMP; TMP. N = N; TMP. M = m; For (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) TMP. A [I] [J] = A [I] [J]-B. A [I] [J]; return TMP;} Matrix Operator * (const Matrix & B) const {matrix TMP; TMP. N = N; TMP. M = B. m; For (INT I = 0; I <n; I ++) {for (Int J = 0; j <B. m; j ++) TMP. A [I] [J] = 0 ;}for (INT I = 0; I <n; I ++) for (Int J = 0; j <B. m; j ++) for (int K = 0; k <m; k ++) {TMP. A [I] [J] + = A [I] [k] * B. A [k] [J] ;}for (INT I = 0; I <n; I ++) for (Int J = 0; j <B. m; j ++) TMP. A [I] [J] % = MOD; return TMP;} void copy (const Matrix & X) {n = x. n; M = x. m; For (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) A [I] [J] = x. A [I] [J] ;}}; // A × B makes sense only when the number of columns in matrix A is equal to the number of rows in matrix B. Matrix m_quick_pow (matrix m, int K) {matrix TMP; TMP. N = m. n; TMP. M = m. m; // M = N can be used for Fast Power (INT I = 0; I <TMP. n; I ++) {for (Int J = 0; j <TMP. n; j ++) {if (I = J) TMP. A [I] [J] = 1; else TMP. A [I] [J] = 0 ;}while (k) {If (K & 1) TMP. copy (TMP * m); k> = 1; M. copy (M * m);} return TMP;} matrix A, B; int main () {int N, K; while (scanf ("% d ", & N, & K) {If (n = 0 & K = 0) break;. N = N;. M = K; For (INT I = 0; I <. n; I ++) for (Int J = 0; j <. m; j ++) scanf ("% d", &. A [I] [J]); B. N = K; B. M = N; For (INT I = 0; I <B. n; I ++) for (Int J = 0; j <B. m; j ++) scanf ("% d", & B. A [I] [J]); matrix E, C; C. copy (B * A); C. copy (m_quick_pow (C, N * N-1); E. copy (A * C); E. copy (E * B); LL ans = 0; For (INT I = 0; I <E. n; I ++) {for (Int J = 0; j <E. m; j ++) {ans + = (LL) (E. A [I] [J]) ;}} printf ("% i64d \ n", ANS);} 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.