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