結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー 37zigen37zigen
提出日時 2018-07-27 23:05:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 1,095 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 75,064 KB
実行使用メモリ 56,240 KB
最終ジャッジ日時 2023-09-18 13:27:53
合計ジャッジ時間 6,015 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
56,004 KB
testcase_01 AC 122 ms
55,780 KB
testcase_02 AC 123 ms
55,580 KB
testcase_03 AC 123 ms
55,848 KB
testcase_04 AC 123 ms
55,364 KB
testcase_05 AC 123 ms
55,820 KB
testcase_06 AC 119 ms
55,652 KB
testcase_07 AC 119 ms
55,684 KB
testcase_08 AC 123 ms
56,156 KB
testcase_09 AC 123 ms
55,744 KB
testcase_10 AC 123 ms
56,016 KB
testcase_11 AC 123 ms
55,992 KB
testcase_12 AC 124 ms
55,996 KB
testcase_13 AC 122 ms
55,800 KB
testcase_14 AC 122 ms
56,092 KB
testcase_15 AC 121 ms
55,828 KB
testcase_16 AC 122 ms
55,804 KB
testcase_17 AC 121 ms
55,992 KB
testcase_18 AC 122 ms
56,240 KB
testcase_19 AC 121 ms
56,008 KB
testcase_20 AC 122 ms
55,920 KB
testcase_21 AC 126 ms
55,908 KB
testcase_22 AC 120 ms
55,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	final long MOD = 1000000007;

	void run() {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long[][] mat = { { 1, 1, 0, 0 }, { 0, 1, 1, 2 }, { 0, 1, 0, 0 }, { 0, 1, 0, 1 } };
		long[][] v = { { 0 }, { 1 }, { 0 }, { 0 } };
		mat = pow(mat, N);
		mat = mul(mat, v);
		System.out.println(mat[0][0]);
	}

	long[][] pow(long[][] a, long n) {
		long[][] ret = new long[a.length][a.length];
		for (int i = 0; i < a.length; ++i)
			ret[i][i] = 1;
		for (; n > 0; n >>= 1, a = mul(a, a)) {
			if (n % 2 == 1)
				ret = mul(a, ret);
		}
		return ret;
	}

	long[][] mul(long[][] a, long[][] b) {
		long[][] ret = new long[a.length][b[0].length];
		for (int i = 0; i < a.length; ++i) {
			for (int j = 0; j < b[i].length; ++j) {
				for (int k = 0; k < a[i].length; ++k) {
					ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % MOD;
				}
			}
		}
		return ret;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0