結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー YamaKasaYamaKasa
提出日時 2018-07-29 12:25:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 3,165 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 77,204 KB
実行使用メモリ 56,440 KB
最終ジャッジ日時 2023-09-22 23:46:45
合計ジャッジ時間 6,391 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,068 KB
testcase_01 AC 122 ms
56,024 KB
testcase_02 AC 122 ms
56,440 KB
testcase_03 AC 122 ms
56,100 KB
testcase_04 AC 123 ms
56,124 KB
testcase_05 AC 126 ms
55,828 KB
testcase_06 AC 123 ms
55,808 KB
testcase_07 AC 122 ms
56,076 KB
testcase_08 AC 123 ms
56,008 KB
testcase_09 AC 124 ms
56,240 KB
testcase_10 AC 122 ms
55,928 KB
testcase_11 AC 123 ms
55,648 KB
testcase_12 AC 122 ms
55,908 KB
testcase_13 AC 124 ms
55,884 KB
testcase_14 AC 125 ms
55,768 KB
testcase_15 AC 126 ms
55,592 KB
testcase_16 AC 125 ms
55,924 KB
testcase_17 AC 123 ms
56,412 KB
testcase_18 AC 122 ms
56,064 KB
testcase_19 AC 121 ms
55,596 KB
testcase_20 AC 121 ms
56,248 KB
testcase_21 AC 124 ms
56,396 KB
testcase_22 AC 123 ms
55,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	static long d = 1000000000 + 7;

	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 < 40; i++) {
			long a00 = A[i -1][0][0] % d;
			long a01 = A[i -1][0][1] % d;
			long a10 = A[i -1][1][0] % d;
			long a11 = A[i -1][1][1] % d;
			A[i][0][0] = ((a00 * a00) % d + (a01 * a10) % d) % d;
			A[i][0][1] = ((a00 * a01) % d + (a01 * a11) % d) % d;
			A[i][1][0] = ((a10 * a00) % d + (a11 * a01) % d) % d;
			A[i][1][1] = ((a10 * a01) % d + (a11 * a11) % d) % d;
		}

		String num = Long.toBinaryString(4 * m - 1);
		long [][]Am = new long[2][2];
		Am[0][0] = 1;
		Am[1][1] = 1;
		//System.out.println(num.length());
		for(int i = 0; i < num.length(); i++) {
			char c = num.charAt(num.length() - i - 1);
			//char c = num.charAt(i);
			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;
		//System.out.println(a);
		//System.out.println(b);

		// y = 5^(d - 2) mod m を求める
//		long []f = new long[35];
//		f[0] = 1;
//		f[1] = 5;
//		String num1 = Long.toBinaryString(d - 2);
//		for(int i = 2; i < 34; i++) {
//			f[i] = (f[i - 1] * f[i - 1]) % d;
//		}
//		for(int i = 0; i < 5; i++) {
//			System.out.println(f[i]);
//		}
//		long y = 0;
//		for(int i = 0; i < num1.length(); i++) {
//			char c = num1.charAt(num1.length() - i - 1);
//			if(c == '1') {
//				y += f[i];
//				y = y % d;
//			}
//		}

		if(N % 2 == 1) {
			long  ans = (1L - b  + 2L * a) % d;
			long x =  ((2 * d + 1) / 5) % d;
			// ans = (ans * x) % d;
			BigInteger D = new BigInteger(Long.toString(d));
			BigInteger v1 = new BigInteger(Long.toString(ans));
			BigInteger X = new BigInteger(Long.toString(x));
			BigInteger y = v1.multiply(X).mod(D);

			System.out.println(y.toString());
		}else {
//			long  ans = (-1L + b  + 3L * a) / 5L;
//			System.out.println(ans);
			long ans = (-1L + b  + 3L * a) % d;

			long x = ((2 * d + 1) / 5) % d;

			BigInteger D = new BigInteger(Long.toString(d));
			BigInteger v1 = new BigInteger(Long.toString(ans));
			BigInteger X = new BigInteger(Long.toString(x));
			BigInteger y = v1.multiply(X).mod(D);

			System.out.println(y.toString());

		}
	}
	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