As the name implies, it is a fast calculation of the number of times the power. Its time complexity is O (log₂n), and the Simplicity of O (N) compared to the efficiency has been greatly improved. The following is to introduce the B to a binary. The right of the second bit of the binary number is for example
11 binary is 1011 11 = 2³x1 + 2²x0 + 2¹x1 + 2ºx1 Therefore, we will convert a¹¹ to calculate------Baidu Encyclopedia
Example: Calculating a23,23 with binary expansion (10111):
23 = 1 * 24 + 0 * 23 + 1 * 22 + 1 * 21 + 1 * 20,
The iteration starts from the low, the K bit is 0, that is, does not operate; The K-bit is 1,base *2k-1.
int fun (int a,int b)
{
int r=1,base=a;
while (b!=0)
{
if (b&1)//To determine the current number of odd-even
r*=base;
Base*=base;
b>>=1; Move right
}
return r;
The Fast power modulus algorithm is based on the basic properties of modulo operations: (a*b)%m = ((a%m) * (b%m))%m
int fun (int a,int b,int m)
{
int r=1,base=a;
while (b!=0)
{
if (b&1)//To determine the current number of odd-even
r=r*base%m;
base=base*base%m;
b>>=1;
}
return r;
}
The fast power of matrices is used to efficiently calculate the high-order of matrices. Reduce the time complexity of Simple O (n) to log (n).
Here first the principle (mainly using the binding law of matrix multiplication):
Generally a matrix of n-Squares, we will pass the n-1 times to get its n power.
But the simple improvement can reduce the number of times of the multiplication, the method is as follows:
22 Grouping of n matrices, for example: A*a*a*a*a*a = (a*a) * (a*a) * (a*a)
The benefit of this change is that you only need to calculate the a*a once and then multiply the result (a*a) by yourself twice to get a^6, ie (a*a) ^3=a^6. It is found that this time has been multiplied by 3 times, less than the original 5 times.
The fast power problem of matrices is the same as the fast power of the general number (bits by weight), which is illustrated in the following example:
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 following is an example of HDU 1575:
A is a square, then tr a represents a trace (which is the a^k of the main diagonal) and is now required by TR (%9973).
The matrix quickly powers any matrix multiplied by the unit matrix, and its value does not change.
#include <iostream> #include <stdio.h> #include <string.h> const int n=10,mod=9973;
struct Matrix {int m[n][n];};
int n;
Matrix Mul (Matrix A,matrix B)//matrices multiplication {matrix C;
memset (c.m,0,sizeof (C.M));
for (int i=0;i<n;i++) for (int j=0;j<n;j++) for (int k=0;k<n;k++) {
C.M[I][J] + = ((A.m[i][k]*b.m[k][j])%mod)%mod;
C.m[i][j]%=mod;
} return C;
} Matrix Fastm (Matrix A,int k) {Matrix res;
memset (res.m,0,sizeof (RES.M));
for (int i=0; i<n; i++)//tectonic unit matrix {res.m[i][i]=1;
} while (k) {if (k&1) res = Mul (res,a);
k>>=1;
A = Mul (a,a);
} return res;
} int main () {int t,k,sum;
scanf ("%d", &t);
Matrix A;
while (t--) {sum=0;
scanf ("%d%d", &n,&k); for (int i=0, i<n; i++) {for (int j=0; J<n;
J + +) {scanf ("%d", &a.m[i][j]);
}} a=fastm (A,k);
for (int i=0; i<n; i++) {sum+=a.m[i][i]%mod;
} printf ("%d\n", sum%mod);
} return 0; }