結果

問題 No.793 うし数列 2
ユーザー GrenacheGrenache
提出日時 2019-04-27 23:41:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 2,068 bytes
コンパイル時間 3,651 ms
コンパイル使用メモリ 74,348 KB
実行使用メモリ 56,172 KB
最終ジャッジ日時 2023-08-20 02:47:37
合計ジャッジ時間 7,725 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,612 KB
testcase_01 AC 126 ms
55,640 KB
testcase_02 AC 129 ms
55,612 KB
testcase_03 AC 131 ms
55,596 KB
testcase_04 AC 131 ms
55,608 KB
testcase_05 AC 128 ms
55,956 KB
testcase_06 AC 125 ms
53,896 KB
testcase_07 AC 125 ms
55,616 KB
testcase_08 AC 125 ms
55,324 KB
testcase_09 AC 128 ms
55,704 KB
testcase_10 AC 127 ms
55,868 KB
testcase_11 AC 126 ms
56,084 KB
testcase_12 AC 125 ms
55,900 KB
testcase_13 AC 125 ms
55,792 KB
testcase_14 AC 127 ms
55,564 KB
testcase_15 AC 128 ms
55,360 KB
testcase_16 AC 126 ms
55,988 KB
testcase_17 AC 125 ms
56,172 KB
testcase_18 AC 125 ms
53,868 KB
testcase_19 AC 122 ms
55,688 KB
testcase_20 AC 122 ms
55,772 KB
testcase_21 AC 124 ms
55,760 KB
testcase_22 AC 123 ms
55,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.Scanner;


public class Main_yukicoder793 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		final int MOD = 1_000_000_007;

		long n = sc.nextLong();

		int[] a = {10, 1};
		int[][] x = {
				{10, 0},
				{1, 0},
		};
		
		long ans = f(x, a, n, MOD);

		int[] b = {3, 0, 1};
		int[][] xx = {
				{10, 0, 3},
				{0, 10, 3},
				{0, 0, 1},
		};
		ans += f(xx, b, n, MOD);
		ans %= MOD;
		
		pr.println(ans);
	}
	
	private static long f(int[][] x, int[] a, long n, int MOD) {
		long loop = n;
		while (loop > 0) {
			if (loop % 2 == 1) {
				a = Matrix.mul(x, a, MOD);
			}
			x = Matrix.mul(x, x, MOD);
			loop /= 2;
		}
		
		return a[1];
	}

	static class Matrix {
		static int[] mul(int[][] a, int[] x, int MOD) {
			int m = a.length;
			int n = a[0].length;
			if (n != x.length) {
				throw new IllegalArgumentException();
			}

			int[] y = new int[m];
			for (int i = 0; i < m; i++) {
				long tmp = 0;
				for (int j = 0; j < n; j++) {
					tmp += (long)a[i][j] * x[j];
					tmp %= MOD;
				}
				y[i] = (int)tmp;
			}

			return y;
		}

		static int[][] mul(int[][] a, int[][] a2, int MOD) {
			int m = a.length;
			int n = a2[0].length;
			int l = a[0].length;
			if (l != a2.length) {
				throw new IllegalArgumentException();
			}

			int[][] y = new int[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					long tmp = 0;
					for (int k = 0; k < l; k++) {
						tmp += (long)a[i][k] * a2[k][j];
						tmp %= MOD;
					}
					y[i][j] = (int)tmp;
				}
			}

			return y;
		}

		static int[][] getI(int n) {
			int[][] mat = new int[n][];
			for (int i = 0; i < n; i++) {
				mat[i] = new int[n];
				mat[i][i] = 1;
			}

			return mat;
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);
			
		solve();
			
		pr.close();
		sc.close();
	}

	static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0