結果

問題 No.534 フィボナッチフィボナッチ数
ユーザー tanzakutanzaku
提出日時 2017-03-18 12:43:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 3,464 bytes
コンパイル時間 5,474 ms
コンパイル使用メモリ 75,196 KB
実行使用メモリ 49,368 KB
最終ジャッジ日時 2023-09-18 02:18:19
合計ジャッジ時間 8,764 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
48,884 KB
testcase_01 AC 43 ms
49,044 KB
testcase_02 AC 45 ms
49,036 KB
testcase_03 AC 43 ms
48,912 KB
testcase_04 AC 43 ms
49,024 KB
testcase_05 AC 43 ms
48,712 KB
testcase_06 AC 43 ms
48,828 KB
testcase_07 AC 43 ms
48,748 KB
testcase_08 AC 43 ms
48,844 KB
testcase_09 AC 43 ms
48,848 KB
testcase_10 AC 43 ms
48,852 KB
testcase_11 AC 43 ms
49,048 KB
testcase_12 AC 43 ms
48,872 KB
testcase_13 AC 43 ms
48,912 KB
testcase_14 AC 43 ms
48,720 KB
testcase_15 AC 43 ms
49,032 KB
testcase_16 AC 44 ms
48,872 KB
testcase_17 AC 43 ms
48,924 KB
testcase_18 AC 44 ms
48,952 KB
testcase_19 AC 44 ms
48,828 KB
testcase_20 AC 44 ms
48,916 KB
testcase_21 AC 43 ms
48,936 KB
testcase_22 AC 44 ms
48,916 KB
testcase_23 AC 44 ms
48,964 KB
testcase_24 AC 43 ms
48,804 KB
testcase_25 AC 43 ms
48,856 KB
testcase_26 AC 43 ms
49,040 KB
testcase_27 AC 44 ms
49,368 KB
testcase_28 AC 43 ms
48,796 KB
testcase_29 AC 43 ms
48,800 KB
testcase_30 AC 43 ms
49,028 KB
testcase_31 AC 42 ms
48,852 KB
testcase_32 AC 44 ms
49,076 KB
testcase_33 AC 43 ms
48,824 KB
testcase_34 AC 44 ms
48,880 KB
testcase_35 AC 43 ms
48,888 KB
testcase_36 AC 43 ms
48,788 KB
testcase_37 AC 43 ms
48,840 KB
testcase_38 AC 43 ms
49,200 KB
testcase_39 AC 43 ms
49,024 KB
testcase_40 AC 42 ms
48,944 KB
testcase_41 AC 43 ms
48,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class _p1481_Validate {
	public static void main(String[] args) throws IOException {
		new _p1481_Validate().solve();
	}
	
	long fib(long n, int mod) {
		long[][] mat = new long[][]{
			new long[]{1,1,},
			new long[]{1,0,},
		};
		mat = powmat(n, mod, mat);
		long[][] x = new long[][]{
			new long[]{1,},
			new long[]{0,},
		};
		long res = mulmat(mat, x, mod)[1][0];
		return res;
	}
	void test() {
		long a = 0, b = 1;
		for (long i = 1; ; i++) {
			long t = b;
			b += a;
			a = t;
			b %= mod;
			if (a == 0 && b == 1) {
				dump(i);
				break;
			}
		}
	}

	static int mod = (int)1e9+7;
	void solve() throws IOException {
		if (false) { test(); return; }
		
		try (final InputValidator in = new InputValidator(System.in)) {
//		try (final Scanner in = new Scanner(System.in)) {
			long n = in.nextLong(0, 1_000000000_000000000L);
			in.newLine();
			in.eof();
			System.out.println(fib(fib(n, 2000000016), mod));
		}
	}

	// long
	// a [n,v] * b [v,m] => c[n,m]
	static long[][] mulmat(long[][] a, long[][] b, int mod) {
		assert(a[0].length == b.length);
		
		final int n = a.length;
		final int v = b.length;
		final int m = b[0].length;
		
		long[][] res = new long[n][m];
		for(int i = 0; i < n; i++)
			for(int k = 0; k < v; k++) {
				final long aa = a[i][k];
				for(int j = 0; j < m; j++) {
					res[i][j] += aa * b[k][j] % mod;
					res[i][j] %= mod;
				}
			}
		
		return res;
	}
	
	static long[][] powmat(long r, int mod, long[][] mat) {
		final int n = mat.length;
		long[][] x = new long[n][n];
		for(int i = 0; i < n; i++) {
			x[i][i] = 1;
		}
		
		for(;r > 0; r >>>= 1) {
			if((r&1) == 1) {
				x = mulmat(x, mat, mod);
			}
			mat = mulmat(mat, mat, mod);
		}

		return x;
	}
	
	// for debug
	static void dump(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}
	
	static class InputValidator implements Closeable {
		final InputStream in;
		
		final int NO_BUFFER = -2;
		int buffer;
		
		public InputValidator(final InputStream in) {
			this.in = in;
			buffer = NO_BUFFER;
		}
		
		int read() throws IOException {
			final int res = buffer == NO_BUFFER ? in.read() : buffer;
			buffer = NO_BUFFER;
			return res;
		}
		
		void unread(int ch) throws IOException {
			buffer = ch;
		}
		
		// [low, high]
		long nextLong(long low, long high) throws IOException {
			long val = 0;
			int ch = -1;
			boolean read = false;
			while (true) {
				ch = read();
				if (!Character.isDigit(ch)) break;
				read = true;
				val = val * 10 + ch - '0';
				check(val <= high);
			}
			check(read);
			check(ch >= 0);
			check(val >= low);
			unread(ch);
			return val;
		}
		int nextInt(int low, int high) throws IOException { return (int)nextLong(low, high); }
		int[] nextInts(int n, int low, int high) throws IOException {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt(low, high);
				if (i + 1 != n) space();
			}
			newLineOrEOF();
			return res;
		}
		
		void space() throws IOException { int ch = read(); check(ch == ' '); }
		void newLine() throws IOException {
			int ch = read();
			if (ch == '\r') ch = read();
			check(ch == '\n');
		}
		void newLineOrEOF() throws IOException { int ch = read(); check(ch == '\r' || ch == '\n' || ch < 0); }
		void eof() throws IOException { int ch = read(); check(ch < 0); }
		void check(boolean b) { if (!b) throw new RuntimeException(); }

		@Override
		public void close() throws IOException {
		}
	}
}
0