結果

問題 No.534 フィボナッチフィボナッチ数
ユーザー 37zigen37zigen
提出日時 2017-06-24 00:21:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 2,446 bytes
コンパイル時間 2,545 ms
コンパイル使用メモリ 82,064 KB
実行使用メモリ 54,676 KB
最終ジャッジ日時 2024-04-14 06:46:42
合計ジャッジ時間 10,423 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
53,232 KB
testcase_01 AC 151 ms
54,572 KB
testcase_02 AC 131 ms
54,172 KB
testcase_03 AC 129 ms
53,996 KB
testcase_04 AC 129 ms
54,460 KB
testcase_05 AC 130 ms
54,308 KB
testcase_06 AC 130 ms
54,364 KB
testcase_07 AC 130 ms
54,016 KB
testcase_08 AC 130 ms
54,676 KB
testcase_09 AC 130 ms
54,240 KB
testcase_10 AC 131 ms
54,384 KB
testcase_11 AC 129 ms
54,076 KB
testcase_12 AC 132 ms
54,024 KB
testcase_13 AC 132 ms
54,096 KB
testcase_14 AC 129 ms
54,272 KB
testcase_15 AC 131 ms
54,300 KB
testcase_16 AC 135 ms
54,288 KB
testcase_17 AC 131 ms
54,164 KB
testcase_18 AC 117 ms
53,096 KB
testcase_19 AC 128 ms
54,312 KB
testcase_20 AC 132 ms
54,304 KB
testcase_21 AC 155 ms
54,372 KB
testcase_22 AC 157 ms
54,384 KB
testcase_23 AC 155 ms
54,436 KB
testcase_24 AC 140 ms
53,712 KB
testcase_25 AC 154 ms
54,420 KB
testcase_26 AC 156 ms
54,352 KB
testcase_27 AC 159 ms
54,264 KB
testcase_28 AC 157 ms
54,464 KB
testcase_29 AC 157 ms
54,540 KB
testcase_30 AC 156 ms
54,152 KB
testcase_31 AC 147 ms
54,356 KB
testcase_32 AC 155 ms
54,016 KB
testcase_33 AC 146 ms
54,372 KB
testcase_34 AC 158 ms
54,296 KB
testcase_35 AC 154 ms
54,428 KB
testcase_36 AC 157 ms
54,336 KB
testcase_37 AC 144 ms
54,428 KB
testcase_38 AC 154 ms
54,144 KB
testcase_39 AC 154 ms
54,008 KB
testcase_40 AC 159 ms
54,428 KB
testcase_41 AC 147 ms
54,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

class Main implements Runnable {
	public static void main(String[] args) {
		long t = System.currentTimeMillis();
		new Main().run();
		// new Thread(null, new Main(), "",
		// Runtime.getRuntime().maxMemory()).start();
		System.err.println(System.currentTimeMillis() - t);
	}

	long MODULO = 1_000_000_000 + 7;

	@SuppressWarnings("unchecked")
	public void run() {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long[][] x = { { 1, 1 }, { 1, 0 } };
		BigInteger[][] x_big = { { BigInteger.ONE, BigInteger.ONE }, { BigInteger.ONE, BigInteger.ZERO } };
		// BigInteger[][] y_big = { { BigInteger.ZERO, BigInteger.valueOf(MODULO
		// - 1) },
		// { BigInteger.valueOf(MODULO - 1), BigInteger.ONE } };
		BigInteger fn = pow(x_big, n, MODULO * MODULO - 1)[1][0];
		// tr(fn.divide(BigInteger.valueOf(MODULO)));
		// long r = new Long(fn.mod(BigInteger.valueOf(MODULO)).toString());
		long[][] z = pow(x, new Long(fn.toString()), MODULO);
		System.out.println(z[1][0]);
	}

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

	BigInteger[][] mul(BigInteger[][] a, BigInteger[][] b, long MODULO) {
		BigInteger[][] ret = new BigInteger[2][2];
		for (int i = 0; i < 2; ++i) {
			for (int j = 0; j < 2; ++j) {
				ret[i][j] = BigInteger.ZERO;
			}
		}
		for (int i = 0; i < 2; ++i) {
			for (int j = 0; j < 2; ++j) {
				for (int k = 0; k < 2; ++k) {
					ret[i][j] = ret[i][j].add(a[i][k].multiply(b[k][j]));
					ret[i][j] = ret[i][j].mod(BigInteger.valueOf(MODULO));
				}
			}
		}
		return ret;
	}

	long[][] pow(long[][] a, long n, long MODULO) {
		long[][] ret = { { 1, 0 }, { 0, 1 } };
		for (; n > 0; n >>= 1, a = mul(a, a, MODULO)) {
			if (n % 2 == 1) {
				ret = mul(ret, a, MODULO);
			}
		}
		return ret;
	}

	BigInteger[][] pow(BigInteger[][] a, long n, long MODULO) {
		BigInteger[][] ret = { { BigInteger.ONE, BigInteger.ZERO }, { BigInteger.ZERO, BigInteger.ONE } };
		for (; n > 0; n >>= 1, a = mul(a, a, MODULO)) {
			if (n % 2 == 1) {
				ret = mul(ret, a, MODULO);
			}
		}
		return ret;
	}

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