矩阵快速幂问题以及快速幂在线性递推中的应用

1、先写一个数字的快速幂,矩阵的快速幂和他是一样的,只不过把初始ans换成了单位矩阵,把两个数字的乘积换成了两个矩阵相乘

import java.util.Scanner;

public class sixth {
    public static void main(String[] args) {
        Scanner sc=  new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();

        int x = f(a,b);
        System.out.println(x);
    }
    static int f(int base,int n){

        int ans =1;
        while (n!=0){
            if((n&1)==1){
                ans = ans*base;
            }
            base= base*base;
            n>>=1;
        }
        return ans;
    }
}

2、矩阵快速幂

class third{
    public static void main(String[] args) {

    }
    static int Mod = 10000;
    static int[][] mat_mul(int[][] x,int[][]y){
        int [][] res = new int[2][2];

        for(int i = 0;i<2;i++){
            for(int j = 0;j<2;j++){
                for(int k = 0;k<2;k++){
                    res[i][j]+=x[i][k]*y[k][j];
                    res[i][j]%= Mod;
                }
            }
        }
        return res;
    }
    static int[][] pow(int n){
        int [][] res = new int[n][n];
        int[][] yi = new int[2][2];
        yi[0][0] = 1;
        yi[0][1] = 1;
        yi[1][0]= 1;
        yi[1][1] = 0;
        for(int i = 0;i<n;i++){//单位矩阵
            res[i][i] = 1;
        }
        while (n!=0){
            if((n&1)==1) res = mat_mul(res,yi);
            yi = mat_mul(yi,yi);
            n=n>>1;
        }
        return res;
    }


}

3、先写一个简单的引用一下https://blog.csdn.net/a17865569022/article/details/78309994
4、有关快速幂在线性递推中的应用 https://blog.csdn.net/arrowlll/article/details/52480335
一道例题:https://ac.nowcoder.com/acm/contest/903/B
这道题就是 s[i] = s[i-1]*q +q;
构造
q 1
1 1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值