結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー YamaKasaYamaKasa
提出日時 2018-07-29 00:02:18
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,919 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 76,200 KB
実行使用メモリ 58,084 KB
最終ジャッジ日時 2023-09-20 19:33:50
合計ジャッジ時間 6,517 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 121 ms
55,988 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 119 ms
55,960 KB
testcase_18 AC 121 ms
56,304 KB
testcase_19 WA -
testcase_20 AC 126 ms
55,708 KB
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	static long d = 1000000007L;
	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		long N = scan.nextLong();
		scan.close();
		long m;
		if(N % 2 == 1) {
			m = N / 2 + 1;
		}else {
			m = N / 2;
		}
		long [][][]A = new long[40][2][2];
		A[0][0][0] = 1;
		A[0][0][1] = 1;
		A[0][1][0] = 1;
		A[0][1][1] = 0;
		for(int i = 1; i < 30; i++) {
			long a00 = A[i -1][0][0];
			long a01 = A[i -1][0][1];
			long a10 = A[i -1][1][0];
			long a11 = A[i -1][1][1];
			A[i][0][0] = (a00 * a00 + a01 * a10) % d;
			A[i][0][1] = (a00 * a01 + a01 * a11) % d;
			A[i][1][0] = (a10 * a00 + a11 * a01) % d;
			A[i][1][1] = (a10 * a01 + a11 * a11) % d;
		}

		String num = Long.toBinaryString(4 * m - 1);
		long [][]Am = new long[2][2];
		Am[0][0] = 1;
		Am[1][1] = 1;


		for(int i = 0; i < num.length(); i++) {
			char c = num.charAt(num.length() - i - 1);
			if(c == '1') {
				long [][]At = new long[2][2];
				At = square(Am, A[i]);
				for(int j = 0; j < 2; j++) {
					for(int k = 0; k < 2; k++) {
						Am[j][k] = At[j][k];
					}
				}
				for(int j = 0; j < 2; j++) {
					for(int k = 0; k < 2; k++) {
						Am[j][k] = Am[j][k] % d;
					}
				}
			}
		}

		long a = Am[0][0] % d;
		long b = Am[0][1] % d;

		if(N % 2 == 1) {
			long  ans = (1 - b  + 2 * a) / 5;
			ans = ans % d;
			System.out.println(ans);
		}else {
			long  ans = (-1 + b  + 3 * a) / 5;
			ans = ans % d;
			System.out.println(ans);
		}
	}
	static long[][] square(long[][]A1, long[][]A2) {
		long[][]A = new long[2][2];
		for(int i = 0; i < 2; i++) {

			for(int j = 0; j < 2; j++) {
				long t = 0;
				for(int k = 0; k < 2; k++) {
					t += (A1[i][k] * A2[k][j]) % d;
				}
				t = t % d;
				A[i][j] = t;
			}
		}
		return A;
	}
	static void disp(long [][]A) {
		for(int i = 0; i < 2; i++) {
			System.out.println(A[i][0] + " " + A[i][1]);
		}
		System.out.println();
	}
}
0