結果

問題 No.534 フィボナッチフィボナッチ数
ユーザー 37zigen37zigen
提出日時 2017-06-24 02:32:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 2,322 bytes
コンパイル時間 2,495 ms
コンパイル使用メモリ 79,596 KB
実行使用メモリ 54,608 KB
最終ジャッジ日時 2024-04-14 06:56:13
合計ジャッジ時間 10,391 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
54,212 KB
testcase_01 AC 146 ms
54,308 KB
testcase_02 AC 136 ms
53,848 KB
testcase_03 AC 134 ms
54,484 KB
testcase_04 AC 135 ms
54,228 KB
testcase_05 AC 136 ms
53,976 KB
testcase_06 AC 135 ms
54,300 KB
testcase_07 AC 133 ms
54,376 KB
testcase_08 AC 134 ms
54,168 KB
testcase_09 AC 134 ms
53,972 KB
testcase_10 AC 136 ms
54,128 KB
testcase_11 AC 135 ms
54,216 KB
testcase_12 AC 138 ms
54,348 KB
testcase_13 AC 138 ms
54,256 KB
testcase_14 AC 137 ms
54,352 KB
testcase_15 AC 138 ms
54,296 KB
testcase_16 AC 138 ms
54,456 KB
testcase_17 AC 140 ms
54,312 KB
testcase_18 AC 136 ms
54,096 KB
testcase_19 AC 137 ms
54,220 KB
testcase_20 AC 136 ms
54,236 KB
testcase_21 AC 153 ms
54,456 KB
testcase_22 AC 147 ms
53,960 KB
testcase_23 AC 151 ms
54,460 KB
testcase_24 AC 148 ms
54,500 KB
testcase_25 AC 148 ms
54,256 KB
testcase_26 AC 149 ms
54,452 KB
testcase_27 AC 149 ms
54,112 KB
testcase_28 AC 146 ms
54,364 KB
testcase_29 AC 150 ms
54,100 KB
testcase_30 AC 147 ms
54,420 KB
testcase_31 AC 146 ms
54,608 KB
testcase_32 AC 147 ms
54,300 KB
testcase_33 AC 159 ms
54,540 KB
testcase_34 AC 147 ms
54,232 KB
testcase_35 AC 157 ms
54,132 KB
testcase_36 AC 146 ms
54,132 KB
testcase_37 AC 149 ms
54,408 KB
testcase_38 AC 145 ms
54,304 KB
testcase_39 AC 157 ms
54,244 KB
testcase_40 AC 146 ms
54,276 KB
testcase_41 AC 145 ms
54,388 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, 2 * MODULO + 2)[1][0];
		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