Linear Algebra Study Notes
Matrix (Matrix)
About Matrix and matrix accelerated
Brief introduction
In mathematics, a matrix is a rectangular array arranged in accordance with a set of complex or real numbers from the first matrix coefficients and constants of equations consisting of - Baidu Encyclopedia
Popular terms, the number of some of the collection filled into a rectangle to obtain a matrix
definition
A $ m \ times n $ number $ a_ {i, j} $ called number table is arranged in a matrix row $ $ $ m $ n-column referred $ m \ times n $ matrix.
$$
A = \ {bmatrix the begin A_ {1,1} {1,2} & A_ & A_ {...} &. 1, n-} \ A_ A_ {2,1} {2,2 & & & ... & A_ {} 2, n-} \ ... & ... & ... & ... \ A_ {m, & A_ {m}. 1, 2} & ... & A_ {m, n-} \} End {bmatrix
$$
this $ n \ times m $ $ a $ number called a matrix element, referred to as metadata. . . (Baidu Encyclopedia of the rest are crap
There $ m $ $ row $ n-column matrix is also referred to as $ A_ {mn} $
In particular, two $ n, m $ are the same isotype matrix is called matrix
$ N = m $ $ called matrix or matrix of order n-$ $ $ n-order square matrix
Basic operations
addition
$$
\begin{bmatrix}a_{1,1}&...&a_{1,n}\...&...&...\a_{m,1}&...&a_{m,n}\end{bmatrix}+\begin{bmatrix}b_{1,1}&...&b_{1,n}\...&...&...\b_{m,1}&...&b_{m,n}\end{bmatrix}=\begin{bmatrix}a_{1,1}+b_{1,1}&...&a_{1,n}+b_{1,n}\...&...&...\a_{m,1}+b_{m,1}&...&a_{m,n}+b_{m,n}\end{bmatrix}
$$
Meanwhile, the matrix addition and addition of real numbers as associative and commutative
Immediate
Dorudoru
A Tasu B = B Tasu A
Dorudoru
$$
(A+B)+C=A+(B+C)
$$
It should be noted, can only be an addition between the same type of matrix
Subtraction
The number of focus, inverse operation of subtraction as addition
In the matrix is the same
$$
\ {bmatrix the begin A_ {1,1}} & {... & A_. 1, n-} \ ... & ... & ... \ A_ {m,. 1} &. .. & a_ {m, n} \ end {bmatrix} - \ begin {bmatrix} b_ {1,1} & ... & b_ {1, n} \ ... & ... & ... \ b_ { m, 1} & ... & b_ {m, n} \ end {bmatrix} = \ begin {bmatrix} a_ {1,1} -b_ {1,1} & ... & a_ {1, n} -b_ {1, n} \ ... & ... & ... \ a_ {m, 1} -b_ {m, 1} & ... & a_ {m, n} -b_ {m, n} \ end bmatrix} {
$$
it also satisfies the law on the set of real numbers
Multiplication
Incorporated in the matrix multiplication of the concept, i.e. a multiplication of a matrix
$$
\ MU · \ {bmatrix the begin A_ {1,1}} & {... & A_. 1, n-} \ & ... .. . & ... \ a_ {m, 1} & ... & a_ {m, n} \ end {bmatrix} = \ begin {bmatrix} \ mu · a_ {1,1} & ... & \ mu · {. 1 A_, n-} \ \\ ... & ... & ... {MU A_ · m,. 1} & ... & \ A_ MU · {m, n-} \} End {bmatrix
$$
matrix the multiplication satisfies the following rules:
$$
\ the lambda (\ MU a) = \ MU (\ the lambda a)
$$
$$
\ lambda (\) = (\ lambda \)
$$
$$
(\ lambda + \) = \ lambda + \
$$
$$
\lambda(A+B)=\lambda A+\lambda B
$$
multiplication
Multiplication of two matrices can be defined only if the number of rows equal to the number of columns of a first matrix $ A $ and $ B $ of another matrix.
Referred to as
$$
A_ B_ {Mn}} NP {C_ {MP} =
$$
also be
$$
AB = C
$$
element of $ C $ $ c_ {i, j} $ value
$$
C_ {I , J} = \ sum_ {K =. 1} ^ Na_ {I, K} \ Times B_ {K, J}
$$
e.g.
$$
\ the begin {bmatrix}. 1 & 0 & 2 \ -1 &. 3 &. 1 \ End {bmatrix} \ Times \ begin {bmatrix} 3 & 1 \ 2 & 1 \ 1 & 0 \ end {bmatrix} = \ begin {bmatrix} (1 \ times 3 + 0 \ times2 + 2 \ times1) & (1 \ times1 + 0 \ times1 + 2 \ times0) \ ( -1 \ times3 +. 3 \ times2 +. 1 \ times1) & (-. 1 \ times1 +. 3 \ times1 +. 1 \ times0) \ End {bmatrix} = \ the begin {bmatrix}. 5 &. 1 \. 4 & 2 \ End {bmatrix}
$$
while multiplying the following matrix calculation law:
Associativity:
$$
(AB) C = A (the BC)
$$
left distributive property:
$$
(A + B) + C = the BC the AC
$$
Right distributive law:
$$
C (A + B) = the CA + CB
$$
Matrix accelerate
Fast power
The need to say it?
int base=a,ans=1;
while(b>0){
if(b&1){
ans*=base
ans%=c;
}
base*=base
base%=c;
b=b>>1;
}
return ans;
Is very simple Bitwise
So we respect the power of a matrix computation can be so, so from time complexity $ O (n) $ down to $ O (log_2n) $
This very comfortable
But this what use is it?
Example a
a[1]=a[2]=a[3]=1
a[x]=a[x-3]+a[x-1] (x>3)
Find a value of the number of columns n items of 1,000,000,007 (10 ^ 9 + 7) modulo a.
At first glance this topic
?
This is not to send it points
One by one I do not like to push? ? ?
To 100% of the data T <= 100, n <= 2 * 10 ^ 9;
Smile gradually solidified
So now reflect the usefulness of the matrix accelerate
For a number of columns $ f (n) = f (nx) + f (ny) + ... $
We can construct a $ k \ times1 $ matrix representation of the current state, that is, with $ f (x), f (y) ... $ to indicate our current already know
Then we construct a transfer matrix, so that our original matrix multiply after this transition matrix can be turned into a new matrix to obtain a new $ f (x) $, in order to move forward step by step, to get an answer
Obviously, since we need to use $ f (n-1) $ and $ f (n-3) $ , so we need to retain two states, $ f (n-3) $ is the states $ f (n-2) $ inherited, so we also need to retain, so the original matrix to be constructed in this way
$$
\ bmatrix the begin {F} (. 3) \ F (2) \ F (. 1) \ {End bmatrix }
$$
ie
$$
\ bmatrix the begin {} 1 \ 1 \ 1 \ {End} bmatrix
$$
then think about how we get the next matrix of this matrix
Our current design matrix
$$
\ bmatrix the begin {F} (. 1-n-) \ F (n-2-) \ F (. 3-n-) \} End {bmatrix
$$
we want the matrix
$$
\ the begin { F} bmatrix (n-) \ F (. 1-n-) \ F (n-2-) \} End {bmatrix
$$
shall
$$
\ bmatrix the begin {F} (. 1-n-) \ times1 + F (n-2- ) \ times 0 + f (n -3) \ times1 \ f (n-1) \ times1 + f (n-2) \ times0 + f (n-3) \ times0 \ f (n-1) \ times0 + F (n--2) \ times1 + F (n--. 3) \ times0 \ End {bmatrix}
$$
apparent our transition matrix
$$
\ the begin {bmatrix}. 1 & 0 &. 1 \. 1 & 0 & 0 \ 0 &. 1 & 0 \ End {bmatrix}
$$
Since the first item is $ f (3) $, so we just need to take the $ n-3 $ times the original matrix transition matrix we can get!
But here, this is not a $ O (n) $ algorithm it?But also a lot of slow
of course not! We think about associativity of matrix multiplication
We set the original matrix $ A $, the transfer matrix $ B $, then our final answer is the matrix
$$
\ underbrace {B (B (B (B (B (B (B ... (the n-B} _ { a} -3))))))
$$
the matrix multiplication is associative, this formula can be simplified as
$$
B ^ {a}. 3-n-
$$
here, and we can use fast power matrix is obtained the final answer!
$ O (log_2n) $ is really fast (
I pasted the code extremely ugly giant:
#include<cstdio>
#include<cstring>
#include<iostream>
#define mod 1000000007
using namespace std;
struct Ju{
long long p[5][5];
Ju operator *(const Ju &a)const{
Ju c;
for(long long i=1;i<=3;i++){
for(long long j=1;j<=3;j++){
c.p[i][j]=0;
}
}
for(long long i=1;i<=3;i++){
for(long long j=1;j<=3;j++){
for(long long k=1;k<=3;k++){
c.p[i][j]+=(a.p[i][k]*p[k][j])%mod;
c.p[i][j]%=mod;
}
}
}
return c;
}
}ans,base;
void build(){
for(long long i=1;i<=3;i++){
for(long long j=1;j<=3;j++){
base.p[i][j]=0;
}
}
base.p[1][1]=base.p[1][3]=base.p[2][1]=base.p[3][2]=1;
ans.p[1][1]=ans.p[2][1]=ans.p[3][1]=1;
}
void qsm(long long p){
while(p!=0){
if(p&1){
ans=ans*base;
}
base=base*base;
p>>=1;
}
}
long long T,n;
int main(){
cin>>T;
for(long long i=1;i<=T;i++){
cin>>n;
if(n<=3){
printf("1\n");
continue;
}
build();
qsm(n-3);
cout<<ans.p[1][1]<<endl;
}
return 0;
}
Two examples
Fibonacci let you ask paragraph n of Number, the data range is longlong level
Obviously matrix accelerate
Structure of the original matrix:
$$
\ bmatrix the begin {} F (2) \ F (. 1) \ bmatrix End {} = \ {bmatrix the begin. 1} \. 1 \} End {bmatrix
$$
Consideration of answer on how transferred from a state
$$
\ bmatrix the begin {F} (n-) \ F (. 1-n-) \ bmatrix End {} = \ bmatrix the begin {F} (. 1-n-) \ times1 + F (n-2-) \ times1 \ F (n- -1) \ times1 + F (n-2-) \ times0 \} End {bmatrix
$$
so transfer matrix
$$
\ bmatrix the begin {}. 1. 1 & \ 0. 1 & \} End {bmatrix
$$
following is a simple matrix fast power