题目链接:https://www.dotcpp.com/oj/problem1907.html
题目分析:
- 本来以为直接for循环模拟可以过的,但是显示超时了
- 将快速幂算法扩展为矩阵快速幂,然后求解。

注:但是这个在C语言网上显示运行错误,尚不清楚错哪,逻辑和在本地上运行是正确的。
import java.util.*;public class Main{public static long mod = 99999999;public static long[][] fact(long[][] mat, int n){if (n == 1){return mat;}long[][] pow = new long[mat.length][mat.length];for(int i = 0; i < mat.length; i++){for (int j = 0; j < mat.length; j++){pow[i][j] = mat[i][j];}}while(n != 0){if((n & 1) == 1){long[][] var = new long[mat.length][mat.length];for(int i = 0; i < mat.length; i++){for (int j = 0; j < mat.length; j++){for (int k = 0; k < mat.length; k++){var[i][j] = (var[i][j] + (mat[i][k] * pow[k][j]) % mod) % mod;}}}for(int i = 0; i < mat.length; i++){for (int j = 0; j < mat.length; j++){mat[i][j] = var[i][j];}}}else{long[][] var = new long[mat.length][mat.length];for(int i = 0; i < mat.length; i++){for (int j = 0; j < mat.length; j++){for (int k = 0; k < mat.length; k++){var[i][j] = (var[i][j] + (pow[i][k] * pow[k][j]) % mod) % mod;}}}for(int i = 0; i < mat.length; i++){for (int j = 0; j < mat.length; j++){pow[i][j] = var[i][j];}}}n >>= 1;}return mat;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();if(n == 1){System.out.println(2);System.out.println(3);}else if(n == 2){System.out.println(1);System.out.println(4);}else if(n == 3){System.out.println(6);System.out.println(5);}else{long[] v = {6, 5, 1, 4, 2, 3, 1};long[][] mat = {{0, 1, 1, 0, 0, 0, 0},{1, 0, 0, 1, 0, 0, 0},{0, 0, 0, 0, 1, 0, 0},{0, 0, 0, 0, 0, 1, 0},{2, 3, 0, 0, 0, 0, 0},{0, 2, 0, 0, 0, 0, 0},{5, 3, 0, 0, 0, 0, 1}};long[][] mat2 = fact(mat, n - 3);long[] ans = new long[2];for(int k = 0; k < 2; k++){long temp = 0;for(int i = 0; i < v.length; i++){temp = (temp + (v[i] * mat[i][k]) % mod) % mod;}ans[k] = temp;}System.out.println(ans[0]);System.out.println(ans[1]);}}}
