【matrix】

Real 

The encapsulated matrix multiplication structure useful in Chicken

Matrix A × B

Given two matrices A and B are compatible, i.e., the number of columns A and B is equal to the number of rows, if = A (A IK ) is a mXn matrix, = and B (B kJ ) nXp is a matrix, then their product C = AB is a matrix mXp = C (C ij of )

Wherein for i = 1,2, ..., m j = 1,2, ..., p '       

mXp nXm matrix A by B matrix

for(int i=1;i<=n;++i)
for(int j=1;j<=p;++j)
for(int k=1;k<=m;++k)
c[i][j]+=a[i][k]*b[k][j];

 Fibonacci item n

P1962 Fibonacci number

f [i] = 1Xf [i-1] + 1Xf [i-2] f [i-1] = 1Xf [i-1] + 0Xf [i-2] Tokuita .......

#include<bits/stdc++.h>
#define ll long long const int N=100+5,M=100+5,P=1000000007,INF=1e9+7,inf=0x3f3f3f3f; ll n,m; template <class t>void rd(t &x){ x=0;int w=0;char ch=0; while(!isdigit(ch)) w|=ch=='-',ch=getchar(); while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); x=w?-x:x; } struct Maxtrix{ int a[3][3]; Maxtrix(){memset(a,0,sizeof(a));} Maxtrix operator*(const Maxtrix &b)const{ Maxtrix res; for(int i=1;i<=2;++i) for(int j=1;j<=2;++j) for(int k=1;k<=2;++k) res.a[i][j]=(res.a[i][j]+(ll)a[i][k]*b.a[k][j])%P; return res; } }ans,base; void qmul(ll b){ while(b){ if(b&1) ans=ans*base; base=base*base,b>>=1; } } int main(){ // freopen("in2.txt","r",stdin); rd(n); if(n<=2) return puts("1"),0; base.a[1][1]=base.a[1][2]=base.a[2][1]=1; ans.a[1][1]=ans.a[1][2]=1; qmul(n-2); printf("%d",ans.a[1][1]); return 0; }

 Fibonacci first n items and

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=100+5,M=100+5,P=1000000007,INF=1e9+7,inf=0x3f3f3f3f;
ll n,m;
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

struct Maxtrix{
    ll a[4][4];
    Maxtrix(){memset(a,0,sizeof(a));}
    Maxtrix operator*(const Maxtrix &b)const{
        Maxtrix res;
        for(int i=1;i<=3;++i)
        for(int j=1;j<=3;++j)
        for(int k=1;k<=3;++k)
        res.a[i][j]=(res.a[i][j]+(ll)a[i][k]*b.a[k][j])%m;
        return res;
    }
}ans,base;

void qmul(ll b){
    while(b){
        if(b&1) ans=ans*base;
        base=base*base,b>>=1;
    }
}

int main(){
 //   freopen("in2.txt","r",stdin);
     rd(n),rd(m);
     if(n==1) return puts("1"),0;
     if(n==2) return puts("2"),0;
     base.a[1][1]=base.a[2][1]=base.a[2][2]=base.a[2][3] = Base .a [ 3 ] [ 2 ] = 1 ; 
     ans.a [ 1 ] [ 1 ] = ans.a [ 1 ] [ 2 ] = ans.a [ 1 ] [ 3 ] = 1 ; 
     qmul (n - 1 ); 
     printf ( " % lld " , ans.a [ 1 ] [ 1 ]);
    return  0 ; 
}

Allison Fibonacci

Seeking T (n) = 1 × f [1] + 2 × f [2] + ... + n × [n] f

emmmm these three questions are routine to launch a recursive formula and then do a quick power matrix

#includeM=<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5,M=100+5,P=1e8,INF=1e9+7,inf=0x3f3f3f3f;
ll n,m;
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

struct Maxtrix{
    int a[N][N];
    Maxtrix(){memset(a,0,sizeof(a));}
    Maxtrix operator*(const Maxtrix &b)const{
        Maxtrix c;
        for(int i=1;i<N;++i)
        for(int j=1;j<N;++j)
        for(int k=1;k<N;++k)
        c.a[i][j]=(c.a[i][j]+(ll)a[i][k]*b.a[k][j])%m;
        return c;
    }
}ans,base;

void qmul(int b){
    while(b){
        if(b&1) ans=base*ans;
        base=base*base,b>>=1;
    }
}

int main(){
 //   freopen("in2.txt","r",stdin);
     rd(n),rd(m);
     if(n==1) return puts("1"),0;
     if(n==2) return puts("3"),0;
     base.a[1][1]=base.a[1][2]=base.a[2][2]=base.a[2][3]=base.a[3][3]=base.a[3] [ 4 ] = base .a [ 4 ] [ 3 ] = 1 ; 
     ans.a [ 2 ] [ 1 ] = ans.a [ 3 ] [ 1 ] = ans.a [ 4 ] [ 1 ] = 1 ; 
    qmul (n - 1 ); 
     printf ( " % d " , (- ans.a [ 1 ] [ 1 ] + (II) No ans.a [ 2 ] [ 1 ] + m)% m);
    return  0 ; 
}

...... not want to write

In fact, I think the rules do not rote matrix multiplication, you only find out you used to do, then just write based on what you engage in

Front (I just shot down fun)

Introduction matrix from part of the algorithm

concept

 

NXn square matrix is ​​very common. We are usually a few exceptions on the square of interest.

 1. If a matrix for any i ≠ j, have a, = 0, then the matrix is ​​a diagonal matrix. Because the diagonal matrix of non-diagonal elements are zero, so only need to list it on the diagonal elements can represent a diagonal matrix:

2. Weigh the diagonal elements are nXn 1 diagonal matrix matrix nXn the I the n-  :

I-th column of the matrix is a unit vector E i .

 

Matrix basic operations

 

 

Guess you like

Origin www.cnblogs.com/lxyyyy/p/11342844.html