結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー GrenacheGrenache
提出日時 2018-09-18 12:31:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 1,498 bytes
コンパイル時間 4,540 ms
コンパイル使用メモリ 75,204 KB
実行使用メモリ 55,940 KB
最終ジャッジ日時 2023-09-25 09:44:14
合計ジャッジ時間 8,821 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,920 KB
testcase_01 AC 129 ms
55,800 KB
testcase_02 AC 128 ms
55,688 KB
testcase_03 AC 127 ms
55,940 KB
testcase_04 AC 126 ms
55,684 KB
testcase_05 AC 128 ms
55,800 KB
testcase_06 AC 128 ms
54,248 KB
testcase_07 AC 130 ms
55,940 KB
testcase_08 AC 126 ms
55,684 KB
testcase_09 AC 125 ms
55,704 KB
testcase_10 AC 126 ms
55,484 KB
testcase_11 AC 130 ms
55,800 KB
testcase_12 AC 127 ms
55,712 KB
testcase_13 AC 127 ms
55,448 KB
testcase_14 AC 127 ms
55,756 KB
testcase_15 AC 129 ms
55,688 KB
testcase_16 AC 129 ms
55,828 KB
testcase_17 AC 126 ms
55,808 KB
testcase_18 AC 134 ms
55,440 KB
testcase_19 AC 126 ms
55,748 KB
testcase_20 AC 127 ms
55,672 KB
testcase_21 AC 127 ms
55,828 KB
testcase_22 AC 126 ms
55,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder718 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		final int MOD = 1_000_000_007;
		
		long n = sc.nextLong();

		long fn1 = fibonacciNumber(n + 1, MOD);
		long fn = fibonacciNumber(n, MOD);
		
		long ans = (fn1 * fn1 % MOD - fn * fn % MOD) % MOD + MOD;
		ans %= MOD;
		
		if ((n + 1) % 2 == 1) {
			ans -= 1;
		} else {
			ans -= -1;
		}

		pr.println(ans);
	}

	private static long fibonacciNumber(long n, int MOD) {
		// f(0)=0, f(1)=1, f(2)=1,...

		long[] a = {1, 1, 1, 0};
		long[] x = {1, 0};
		while(n > 0) {
			if (n % 2 != 0) {
				long tmp0 = (x[0] * a[0] + x[1] * a[1]) % MOD;
				long tmp1 = (x[0] * a[2] + x[1] * a[3]) % MOD;
				x[0] = tmp0;
				x[1] = tmp1;
			}
			long tmp0 = (a[0] * a[0] + a[1] * a[2]) % MOD;
			long tmp1 = a[1] * (a[0] + a[3]) % MOD;
			long tmp2 = a[2] * (a[0] + a[3]) % MOD;
			long tmp3 = (a[3] * a[3] + a[1] * a[2]) % MOD;
			a[0] = tmp0;
			a[1] = tmp1;
			a[2] = tmp2;
			a[3] = tmp3;
			n /= 2;
		}

		return x[1];
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(INPUT == null ? System.in : new ByteArrayInputStream(INPUT.getBytes()));
		pr = new Printer(System.out);

		solve();

//		pr.close();
		pr.flush();
//		sc.close();
	}

	static String INPUT = null;

	private static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0