結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー 37zigen37zigen
提出日時 2018-07-28 21:53:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 2,073 bytes
コンパイル時間 3,645 ms
コンパイル使用メモリ 79,340 KB
実行使用メモリ 56,296 KB
最終ジャッジ日時 2023-09-20 16:55:46
合計ジャッジ時間 7,886 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,536 KB
testcase_01 AC 124 ms
55,784 KB
testcase_02 AC 124 ms
55,872 KB
testcase_03 AC 123 ms
56,060 KB
testcase_04 AC 122 ms
56,296 KB
testcase_05 AC 125 ms
56,208 KB
testcase_06 AC 122 ms
56,128 KB
testcase_07 AC 121 ms
55,768 KB
testcase_08 AC 121 ms
55,600 KB
testcase_09 AC 121 ms
55,868 KB
testcase_10 AC 121 ms
55,992 KB
testcase_11 AC 121 ms
55,808 KB
testcase_12 AC 122 ms
53,720 KB
testcase_13 AC 122 ms
55,840 KB
testcase_14 AC 122 ms
56,100 KB
testcase_15 AC 123 ms
55,744 KB
testcase_16 AC 122 ms
56,240 KB
testcase_17 AC 121 ms
56,060 KB
testcase_18 AC 123 ms
55,976 KB
testcase_19 AC 122 ms
55,888 KB
testcase_20 AC 121 ms
56,180 KB
testcase_21 AC 122 ms
55,880 KB
testcase_22 AC 122 ms
55,884 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 = 1_000_000_000 + 7;

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

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

	long[][] pow(long[][] a, long n) {
		long[][] ret = new long[a.length][a.length];
		for (int i = 0; i < ret.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) % MOD;
				}
			}
		}
		return ret;
	}

	long[][] inv(long[][] a) {
		if (a.length != 2 || a[0].length != 2) {
			throw new AssertionError();
		}
		long det = (a[0][0] * a[1][1] % MOD - a[0][1] * a[1][0] % MOD + MOD) % MOD;
		long coe = inv(det);
		if (det * coe % MOD != 1)
			throw new AssertionError();
		long[][] ret = new long[][] { { coe * a[1][1] % MOD, (MOD - coe * a[0][1] % MOD) % MOD },
				{ (MOD - coe * a[1][0] % MOD) % MOD, coe * a[0][0] % MOD } };
		return ret;
	}

	long pow(long a, long n) {
		long ret = 1;
		for (; n > 0; n >>= 1, a = a * a % MOD) {
			if (n % 2 == 1) {
				ret = ret * a % MOD;
			}
		}
		return ret;
	}

	long inv(long a) {
		return pow(a, MOD - 2);
	}

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