https://blog.csdn.net/zhangxiaoduoduo/article/details/81807338
这个的矩阵快速幂讲的蛮好的
这题简单的说就是你要在一个骰子上垒n个骰子,
就要在{4,4,4,4,4,4}(最底下的骰子每个面朝上有四种可能)
上乘n个矩阵A(A[i][j]表示如果下面骰子i朝上,上面骰子j朝上有多少种可能)
因为数据太大需要用到快速幂,模板如下
long long fastPower(long long base, long long power) {
long long result = 1;
while (power > 0) {
if (power & 1) {
result = result * base % mod;
}
power >>= 1;
base = (base * base) % mod;
}
return result;
}
将底数换成矩阵就行了
import java.util.*;
import java.io.*;
public class Main {
static class Input {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
}
public static void main(String[] args) throws IOException {
Input in = new Input();
int n = in.nextInt() - 1,m = in.nextInt(),mod = 1000000007;
long[][] A = new long[7][7];
for (int i = 1; i <= 6; i++) Arrays.fill(A[i],4);
for (int i = 0; i < m; i++) {
int a = in.nextInt(),b = in.nextInt();
A[a][(b + 2) % 6 + 1] = 0;
A[b][(a + 2) % 6 + 1] = 0;
}
long[][] Res = new long[7][7];
for (int i = 1; i <= 6; i++) Res[i][i] = 1;
long[][] T;
while (n > 0) { // 矩阵快速幂
T = new long[7][7];
if(n % 2 == 1) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 6; j++) {
for (int k = 1; k <= 6; k++) {
T[i][j] = (T[i][j] + Res[i][k] * A[k][j]) % mod;
}
}
}
Res = T;
}
n >>= 1;
T = new long[7][7];
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 6; j++) {
for (int k = 1; k <= 6; k++) {
T[i][j] = (T[i][j] + A[i][k] * A[k][j]) % mod;
}
}
}
A = T;
}
long result = 0;
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 6; j++) {
result = (result + Res[i][j] * 4) % mod;
}
}
System.out.println(result);
}
}