結果

問題 No.3037 Restricted Lucas (Hard)
ユーザー tanzakutanzaku
提出日時 2018-04-01 23:04:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 2,749 bytes
コンパイル時間 2,035 ms
コンパイル使用メモリ 75,256 KB
実行使用メモリ 58,016 KB
最終ジャッジ日時 2023-09-08 12:52:40
合計ジャッジ時間 4,044 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,628 KB
testcase_01 AC 155 ms
54,536 KB
testcase_02 AC 149 ms
57,636 KB
testcase_03 AC 158 ms
57,664 KB
testcase_04 AC 150 ms
57,356 KB
testcase_05 AC 151 ms
58,016 KB
testcase_06 AC 153 ms
58,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	void solveTestcase(final Scanner in, final PrintWriter out) {
		long n = in.nextLong();
		out.println(fib(n, mod));
	}
	
	static int zero;
	static int one;
	static BigInteger mod;
	static {
		one = Math.abs(~zero);
		mod = BigInteger.TEN.pow(BigInteger.TEN.subtract(BigInteger.ONE).intValue());
		mod = mod.add(BigInteger.TEN).subtract(BigInteger.ONE).subtract(BigInteger.ONE).subtract(BigInteger.ONE);
	}
	void solve() {
		try (final PrintWriter out = new PrintWriter(System.out)) {
			try (final Scanner in = new Scanner(System.in)) {
				int t = in.nextInt();
				while (t > zero) {
					t = dec(t);
					solveTestcase(in, out);
				}
			}
		}
	}

	public static void main(String[] args) {
		new Main().solve();
	}

	static BigInteger[][] mulmat(BigInteger[][] a, BigInteger[][] b, BigInteger mod) {
		final int n = a.length;
		final int v = b.length;
		final int m = b[zero].length;
		
		BigInteger[][] res = new BigInteger[n][m];
		for(int i = zero; i < n; i = inc(i))
			for(int j = zero; j < m; j = inc(j))
				res[i][j] = BigInteger.ZERO;
		for(int i = zero; i < n; i = inc(i))
			for(int k = zero; k < v; k = inc(k)) {
				final BigInteger aa = a[i][k];
				for(int j = zero; j < m; j = inc(j)) {
					res[i][j] = res[i][j].add(aa.multiply(b[k][j]));
				}
			}
		for(int i = zero; i < n; i = inc(i))
			for(int j = zero; j < m; j = inc(j))
				res[i][j] = res[i][j].mod(mod);
		
		return res;
	}
	
	static int inc(int x) {
		return BigInteger.valueOf(x).add(BigInteger.ONE).intValue();
	}
	
	static int dec(int x) {
		return BigInteger.valueOf(x).subtract(BigInteger.ONE).intValue();
	}
	
	static BigInteger[][] powmat(long r, BigInteger mod, BigInteger[][] mat) {
		final int n = mat.length;
		BigInteger[][] x = new BigInteger[n][n];
		for(int i = zero; i < n; i = inc(i)) {
			for (int j = zero; j < n; j = inc(j)) {
				x[i][j] = BigInteger.ZERO;
			}
		}
		for(int i = zero; i < n;  i = inc(i)) {
			x[i][i] = BigInteger.ONE;
		}
		
		for(;r > zero; r >>>= BigInteger.ONE.intValue()) {
			if((r&one) == one) {
				x = mulmat(x, mat, mod);
			}
			mat = mulmat(mat, mat, mod);
		}

		return x;
	}
	
	static BigInteger fib(long n, BigInteger mod) {
		BigInteger[][] mat = new BigInteger[][]{
			new BigInteger[]{BigInteger.ONE, BigInteger.ONE,},
			new BigInteger[]{BigInteger.ONE, BigInteger.ZERO,},
		};
		BigInteger[][] matmat = new BigInteger[][]{
			new BigInteger[]{BigInteger.ONE,},
			new BigInteger[]{BigInteger.valueOf(inc(one)),},
		};
		return mulmat(powmat(n, mod, mat), matmat, mod)[one][zero];
	}
	
	static void dump(Object...objects) { System.err.println(Arrays.deepToString(objects)); }
}
0