[ACM] poj 3233 matrix power series (evaluate matrix A + A ^ 2 + A ^ 3... + A ^ K, bipartite summation)

Source: Internet
Author: User

Matrix Power Series
Time limit:3000 Ms   Memory limit:131072 K
Total submissions:15417   Accepted:6602

Description

GivenN×NMatrixAAnd a positive integerK, Find the sumS=A+A2 +A3 +... +AK.

Input

The input contains exactly one test case. The first line of input contains three positive integersN(N≤ 30 ),K(K≤ 109) andM(M<104). Then followNLines each containingNNonnegative integers below 32,768, givingA'S elements in row-Major Order.

Output

Output the elementsSModuloMIn the same wayAIs given.

Sample Input

2 2 40 11 1

Sample output

1 22 3

Source

Poj monthly -- 2007.06.03, Huang, Jinsong


Solution:

Given the matrix A (represented by Ori in the code below), and K, Mod, calculate a + A ^ 2 + A ^ 3 + ...... A ^ K and mod remainder.

Start with a loop of K times, recurrence, timeout...

View the question report, and use the binary summation.

The sum is represented by S (k.

When K is an even number:

For example, K = 6, then a + A ^ 2 + A ^ 3 + A ^ 4 + A ^ 5 + A ^ 6 = a + A ^ 2 + A ^ 3 + A ^ 3 * (a + A ^ 2 + A ^ 3)

S (K) = S (K/2) + A ^ (n/2) * s (K/2) that is S (k) = (E + A ^ (n/2) * S (n/2) (E is the unit matrix)

When K is an odd number:

S (K) = S (k-1) * A ^ K, then the k-1 is an even number, according to the above two points

PS: the code should be carefully written; otherwise, a small error will be checked for half a day ..... calculate the RET when two matrices are multiplied. arr [I] [J] + =. arr [I] [k] * B. arr [k] [J]; actually written as ret. arr [I] [J] + =. arr [I] [k] *. arr [k] [J]; t

Code:

# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; const int maxn = 31; int N, K, MOD; struct mat {int arr [maxn] [maxn]; MAT () {memset (ARR, 0, sizeof (ARR) ;}}; mat MUL (MAT a, mat B) {mat ret; For (INT I = 0; I <n; I ++) for (int K = 0; k <n; k ++) {if (. arr [I] [k]) for (Int J = 0; j <n; j ++) {ret. arr [I] [J] + =. arr [I] [k] * B. arr [k] [J]; If (Ret. arr [I] [J]> = mod) ret. arr [I] [J] % = MOD;} return ret;} mat add (MAT A, mat B) {mat an; For (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) {. arr [I] [J] =. arr [I] [J] + B. arr [I] [J]; If (. arr [I] [J]> = mod). arr [I] [J] % = MOD;} return an;} mat power (MAT P, int K) {If (k = 1) return P; MAT E; for (INT I = 0; I <n; I ++) E. arr [I] [I] = 1; if (k = 0) Return e; while (k) {If (K & 1) E = MUL (p, e ); P = MUL (p, p); k >>= 1;} return E;} void output (MAT ans) {for (INT I = 0; I <N; I ++) for (Int J = 0; j <n; j ++) {If (j = N-1) c Out <ans. arr [I] [J] <Endl; else cout <ans. arr [I] [J] <";}} mat Cal (MAT Ori, int K) {If (k = 1) return Ori; If (K & 1) return add (CAL (ORI, k-1), power (ORI, k); // when K is an odd number, minus 1 to an even number S (K) = S (K-1) + Ori ^ K else return MUL (add (Power (ORI, 0), power (ORI, k> 1), Cal (ORI, k> 1 )); // when K is an even number, S (K) = (1 + Ori ^ (K/2) * s (K/2)} int main () {While (scanf ("% d", & N, & K, & mod )! = EOF) {mat Ori, ans; For (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) {scanf ("% d", & Ori. arr [I] [J]); If (Ori. arr [I] [J]> = mod) Ori. arr [I] [J] % = MOD;} ans = CAL (ORI, k); output (ANS);} return 0 ;}


[ACM] poj 3233 matrix power series (evaluate matrix A + A ^ 2 + A ^ 3... + A ^ K, bipartite summation)

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.