結果
問題 |
No.534 フィボナッチフィボナッチ数
|
ユーザー |
|
提出日時 | 2017-06-24 00:21:38 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 155 ms / 2,000 ms |
コード長 | 2,446 bytes |
コンパイル時間 | 2,272 ms |
コンパイル使用メモリ | 80,112 KB |
実行使用メモリ | 42,248 KB |
最終ジャッジ日時 | 2024-10-03 04:02:28 |
合計ジャッジ時間 | 8,848 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 42 |
ソースコード
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)); } }