C-matrix Power SeriesTime
limit:3000MS
Memory Limit:131072KB
64bit IO Format:%i64d &%I 64u Submit Status Practice POJ 3233
Description
Given a n x n Matrix a and a positive integer K, find the sum S = C18>a + a2 + a3 + ... + Ak.
Input
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤30), K (K ≤109) and C4>m (m < 104). Then follow n lines each containing n nonnegative integers below 32,768, giving A' s elements in Row-major order.
Output
Output the elements of S modulo m in the same- A is given.
Sample Input
2 2 40) 11 1
Sample Output
1 22 3
This problem uses two points and a fast power. A very classical problem of the fast power of matrices.
The first is two points, a matrix is continuous and can be divided into odd pairs to discuss, so that its size halved.
For example, when an odd number of times
Ak= (a1+a2+ ... A ((K-1)/2)) +a ((k+1)/2) +a ((k+1)/2) * (a1+a2+ ....) A ((K-1)/2))
When an even number is
Ak= (a1+a2+ ... A (K/2)) +a (K/2) * (a1+a2+ ... A (K/2))
Then execute with two function modules, one is add (a, b), and the other is Mul (A, B)
Set a matrix of functions that are currently halved (with fast power), node now;
Set a matrix to store successive and consecutive results (call this function with a recursive return), node temp;
When an odd number of times
Temp=add (Temp,mul (Temp,now));
Temp=add (Temp,now);
When an even number is
Temp=add (Temp,mul (Temp,now));
Last output temp to get results
Then is the Matrix fast power cal (int k) (k is the power number)
The idea of the fast power of matrices comes from a^b%m, which is also converted to binary, and then calculated based on the number of bits in the binary.
For example a^19 = (a^16) * (a^2) * (a^1), obviously in such a way to calculate the number of factors will be log (n) level (the original factor is N), not only that, there is a link between the factors, such as a^4 through (a^2) * (a^2) to get, a^ 8 can be obtained through (a^4) * (A^4), which also makes full use of the available results as a favourable condition. Here is an example to illustrate:
A^156 is now required, while 156 (10) =10011100 (2)
There are a^156=> (a^4) * (a^8) * (a^16) * (a^128) Considering the relationship between the factors, we start from the far right side of binary 10011100 to the leftmost. The core code is
1Node Cal (intk)2 {3Node Res=ori;//initialize into a unit array4 while(k)5 {6 if(k&1)//the judgment is odd (1).7Res=res*a;//the number on the bits is multiplied by the matrix of the power.8k>>=1;//K Move Right One9A*=a;//The matrix is squared at square speedTen } One returnRes; A}
Or it could be.
1Node Cal (intK//Matrix Fast Power2 {3 node p,q;4P=init;//Initial Array5Q=unit;//Unit Array6 while(k!=1)7 {8 if(k&1)//Power is odd9 {Tenk--; OneQ=mul (P,Q);//Empty The space of a single ride and multiply it at the end A } - Else - { thek>>=1;//In addition to two -P=mul (P,P);//two points - } - } +p=Mul (p,q); - returnp; +}
The code in the decimal angle, look at the binary algorithm, the odd number minus one, even when the time is two. It is the form of the number of matrices with N-times of ans= two, which are left to the N-th square.
These two points are the key to the problem, which can decrease the time complexity of the calculation, with the complete code attached
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <math.h>5#include <algorithm>6 using namespacestd;7 intn,k,m;8 structnode9 {Ten intmapp[ -][ -]; One }init,res,unit,ret; A node Add (node A,node b) - { - inti,j; the node C; - for(i=0; i<n;i++) - for(j=0; j<n;j++) -{c.mapp[i][j]=a.mapp[i][j]+B.mapp[i][j]; +c.mapp[i][j]%=m;} - returnC; + } A node Mul (node A,node b) at { - inti,j,k; - node C; - for(i=0; i<n;i++) - for(j=0; j<n;j++) -c.mapp[i][j]=0; in for(i=0; i<n;i++) - for(j=0; j<n;j++) to for(k=0; k<n;k++) + { -c.mapp[i][j]+=a.mapp[i][k]*B.mapp[k][j]; thec.mapp[i][j]%=m; * } $ returnC;Panax Notoginseng } -Node Cal (intK//Matrix Fast Power the { + node p,q; AP=init;//Initial Array theQ=unit;//Unit Array + while(k!=1) - { $ if(k&1)//Power is odd $ { -k--; -Q=mul (P,Q);//Empty The space of a single ride and multiply it at the end the } - ElseWuyi { thek>>=1;//In addition to two -P=mul (P,P);//two points Wu } - } Aboutp=Mul (p,q); $ returnp; - } -Node sum (intk) - { A if(k==1) + returnInit; the node Temp,now; -Temp=sum (k/2);//sum $ if(k&1)//by binary, bitwise to determine whether it is an odd number the { theNow=cal (k/2+1);//Current one matrix thetemp=Add (Temp,mul (Temp,now)); thetemp=Add (now,temp); - } in Else the { theNow=cal (k/2); Abouttemp=Add (Temp,mul (Temp,now)); the } the returntemp; the } + intMain () - { the inti,j;Bayiscanf"%d%d%d",&n,&k,&m); the for(i=0; i<n;i++) the for(j=0; j<n;j++) - { -scanf"%d",&init.mapp[i][j]); theinit.mapp[i][j]%=m; the if(i==j) theunit.mapp[i][j]=1; the Else -unit.mapp[i][j]=0; the } theret=sum (k); the for(i=0; i<n;i++)94{ for(j=0; j<n;j++) theprintf"%d", Ret.mapp[i][j]); theprintf"\ n"); the }98 return 0; About}
Matrix Power Series (fast power to matrices)