【矩阵专题】入门题目

【矩阵】Fibonacci

题目链接

 

题目描述

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is

Given an integer n, your goal is to compute the last 4 digits of Fn.

 

输入

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

 

输出

For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

 

样例输入

0

9

999999999

1000000000

-1

 

样例输出

0

34

626

6875

 

提示

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

 

这个是最简单的题目了。

算是一个入门题吧,就是用矩阵乘法来表示前一项和后一项。

#include<bits/stdc++.h>
using namespace std;
const int N=2;
const int Mod=10000;
typedef struct Matrix{
    int mat[N][N];
}Matrix;
Matrix operator *(Matrix a,Matrix b){
    Matrix c;
    for(int i=0;i<N;i++){
        for(int k=0;k<N;k++){
            c.mat[i][k]=0;
            for(int j=0;j<N;j++){
                c.mat[i][k]=(c.mat[i][k]+(a.mat[i][j]*b.mat[j][k])%Mod)%Mod;
            }
        }
    }
    return c;
}
Matrix operator ^(Matrix a,int b){
    Matrix c;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            c.mat[i][j]=(i==j);
        }
    }
    while(b){
        if(b&1){
            c=c*a;
        }
        a=a*a;
        b>>=1;
    }
    return c;
}
int main()
{
    Matrix a;
    a.mat[0][0]=1;a.mat[0][1]=1;
    a.mat[1][0]=1;a.mat[1][1]=0;
    int n;
    while(~scanf("%d",&n)){
        if(n==-1)  return 0;
        Matrix fn;
        fn=a^n;
        printf("%d\n",fn.mat[0][1]);
    }
    return 0;
}

 


【矩阵】数列

题目链接

 

题目描述

一个数列定义如下:f(1)=1,f(2)=1,f(n)=(A*f(n-1)+B*f(n-2)) mod 7。给定A,B和n的值,要求计算f(n)的值。

输入

一行三个整数A,B和n,其中1≤A,B≤1000,1≤n≤1e8

输出

一行,一个整数,即f(n)的值

样例输入

1 1 3

样例输出

2

 

这个题目和第一题神似,主要还是通过前两项来推到下一项得到结果。

#include<bits/stdc++.h>
using namespace std;
const int N=2;
const int Mod=7;
typedef struct Matrix{
    int mat[N][N];
}Matrix;
Matrix operator *(Matrix a,Matrix b){
    Matrix c;
    for(int i=0;i<N;i++){
        for(int k=0;k<N;k++){
            c.mat[i][k]=0;
            for(int j=0;j<N;j++){
                c.mat[i][k]=((a.mat[i][j]*b.mat[j][k])%Mod+c.mat[i][k])%Mod;
            }
        }
    }
    return c;
}
Matrix operator ^(Matrix a,int b){
    Matrix c;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            c.mat[i][j]=(i==j);
        }
    }
    while(b){
        if(b&1){
            c=c*a;
        }
        a=a*a;
        b>>=1;
    }
    return c;
}
int main()
{
    int A,B,n;
    scanf("%d%d%d",&A,&B,&n);
    if(n<=2){
        return 0*printf("1\n");
    }
    Matrix a,b,fn;
    a.mat[0][0]=A,a.mat[0][1]=B;
    a.mat[1][0]=1,a.mat[1][1]=0;
 
    b.mat[0][0]=1,b.mat[0][1]=0;
    b.mat[1][0]=1,b.mat[1][1]=0;
 
    fn=a^(n-2);
    fn=fn*b;
    printf("%d\n",fn.mat[0][0]);
}

【矩阵】普通递推数列

题目描述

给出一个k阶齐次递推数列{fi}的通项公式fi=a1fi-1+a2fi-2+...+akfi-k(i≥k),以及初始值f0,f1,...fk-1,求fn。

输入

第一行2个整数:n(0≤n≤1000000)和k(1≤k≤100)。
第二行k个整数:a1,a2,...,ak(0≤ai≤10000,1≤i≤k)。
第三行k个整数:f0,f1,...,fk-1(0≤fi<10000,0≤i≤k)。

输出

一行一个整数p,是fn除以10000的余数。

样例输入

10 2

1 1

1 1

样例输出

89

主要还是经过前后两项的推导得出。

 

#include<bits/stdc++.h>
using namespace std;
const int N=105;
const int Mod=10000;
int n,K;
typedef struct Matrix{
    int mat[N][N];
}Matrix;
Matrix operator *(Matrix a,Matrix b){
    Matrix c;
    for(int i=0;i<K;i++){
        for(int k=0;k<K;k++){
            c.mat[i][k]=0;
            for(int j=0;j<K;j++){
                c.mat[i][k]=((a.mat[i][j]*b.mat[j][k])%Mod+c.mat[i][k])%Mod;
            }
        }
    }
    return c;
}
Matrix operator ^(Matrix a,int b){
    Matrix c;
 
    for(int i=0;i<K;i++){
        for(int j=0;j<K;j++){
            c.mat[i][j]=(i==j);
        }
    }
 
    while(b){
        if(b&1){
            c=c*a;
        }
        a=a*a;
        b>>=1;
    }
    return c;
}
int main()
{
    Matrix a,b;
    int d[N],f[N];
    scanf("%d%d",&n,&K);
    for(int i=0;i<K;i++){
        scanf("%d",&d[i]);
        a.mat[0][i]=d[i];
    }
    for(int i=0;i<K;i++){
        scanf("%d",&f[i]);
    }
    for(int i=1;i<K;i++){
        for(int j=1;j<K;j++){
            a.mat[i][j-1]=(i==j);
        }
    }
    if(n<=K-1){
        return 0*printf("%d\n",f[n]);
    }
    b=a^(n-K+1);
    int ans=0;
    for(int i=0;i<K;i++){
        ans=((b.mat[0][i]*f[(K-1)-i])%Mod+ans)%Mod;
    }
    printf("%d\n",ans);
    return 0;
}

 

B. Jzzhu and Sequences


Jzzhu has invented a kind of sequences, they meet the following property:

You are given x and y, please calculate fn modulo 1000000007 (109 + 7).


Input

The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).


Output

Output a single integer representing fn modulo 1000000007 (109 + 7).


 

Input

2 3

3

Output

1


Input

0 -1

2

Output

1000000006


Note

In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.

In the second sample, f2 = - 1; - 1 modulo (109 + 7) equals (109 + 6).

 

通过手动实现推导公式得到:

\binom{f_{n}}{f_{n-1}}=\binom{f_{n-1}}{f_{n-2}}*{\binom{1\ \ -1}{1\ \ \ \ \ 0}}^{n-2}\ (n>2)

然后直接矩阵快速幂即可,注意注意,一定需要longlong ,不然爆了自己也没有发现。

#include<cstdio>
using namespace std;
const int N=2;
const int Mod=1e9+7;
typedef struct Matrix{
    long long mat[2][2];
}Matrix;
Matrix operator *(Matrix a,Matrix b){
    Matrix c;
    for(int i=0;i<N;i++){
        for(int k=0;k<N;k++){
            c.mat[i][k]=0;
            for(int j=0;j<N;j++){
                c.mat[i][k]=((a.mat[i][j]*b.mat[j][k]+Mod)%Mod+c.mat[i][k]+Mod)%Mod;
            }
        }
    }
    return c;
}
Matrix operator ^(Matrix a,int b){
    Matrix c;

    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            c.mat[i][j]=(i==j);
        }
    }
    while(b){
        if(b&1){
            c=c*a;
        }
        a=a*a;
        b>>=1;
    }
    return c;
}
int main()
{
    long long  x,y,n;
    scanf("%lld%lld%lld",&x,&y,&n);
    Matrix a,b;
    a.mat[0][0]= 1;  a.mat[0][1]=-1;
    a.mat[1][0]= 1;  a.mat[1][1]= 0;

    if(n==1){
        printf("%d\n",(x+Mod)%Mod);
    }else if(n==2){
        printf("%d\n",(y+Mod)%Mod);
    }else{
        b=a^(n-2);
        long long  ans=0;
        ans=(ans+(b.mat[0][0]*y)%Mod+Mod)%Mod;
        ans=(ans+(b.mat[0][1]*x)%Mod+Mod)%Mod;
        //ans=(ans+Mod)%Mod;
        printf("%lld\n",ans);
    }
    return 0;
}

 


Count

【题意】:

a1=1,a2=2 给出一个递推式子出来

\LARGE a_{n}=a_{n-1}+2a_{n-2}+n^3

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=6;
const int mod=123456789;
typedef struct Matrix {
    ll mat[N][N];
}Matrix;

Matrix operator * (Matrix a,Matrix b){
    Matrix c;
    for ( int i=0;i<N;i++) {
        for(int k=0;k<N;k++){
            c.mat[i][k]=0;
            for(int j=0;j<N;j++){
                c.mat[i][k]=(c.mat[i][k]+a.mat[i][j]*b.mat[j][k]);
                if( c.mat[i][k] >= mod ) c.mat[i][k]%=mod;
            }
        }
    }
    return c;
}
Matrix operator ^ (Matrix a,ll b){
    Matrix c;
    for(int i=0;i<N;i++)
       for(int j=0;j<N;j++)
            c.mat[i][j]=(i==j);
    while(b){
        if(b&1) c=c*a;
        b>>=1;  a=a*a;
    }
    return c;
}
int t[N][N]={
    1,2,1,0,0,0,
    1,0,0,0,0,0,
    0,0,1,3,3,1,
    0,0,0,1,2,1,
    0,0,0,0,1,1,
    0,0,0,0,0,1
};
int X[N]={
    2,1,27,9,3,1
};
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        ll n;
        scanf("%lld",&n);
        Matrix a,b;
        for(int i=0;i<N;i++) for(int j=0;j<N;j++){
            a.mat[i][j] = t[i][j];
        }
        if( n <=2 ) { printf("%lld\n",n); continue; }
        b=a^(n-2);
        ll ans=0;
        for(int i=0;i<N;i++)
            ans=(ans+(b.mat[0][i]*X[i])%mod+mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值