結果

問題 No.3036 Restricted Lucas (Easy)
ユーザー GrenacheGrenache
提出日時 2018-04-02 01:01:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 2,890 bytes
コンパイル時間 2,576 ms
コンパイル使用メモリ 75,712 KB
実行使用メモリ 59,304 KB
最終ジャッジ日時 2023-09-08 13:13:15
合計ジャッジ時間 4,208 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,820 KB
testcase_01 AC 136 ms
55,492 KB
testcase_02 AC 203 ms
58,368 KB
testcase_03 AC 204 ms
58,864 KB
testcase_04 AC 195 ms
59,304 KB
testcase_05 AC 193 ms
58,788 KB
testcase_06 AC 201 ms
58,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.ByteArrayInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		BigInteger BMOD = BigInteger.ONE;
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.multiply(BigInteger.TEN);
		BMOD = BMOD.add(BigInteger.ONE);
		BMOD = BMOD.add(BigInteger.ONE);
		BMOD = BMOD.add(BigInteger.ONE);
		BMOD = BMOD.add(BigInteger.ONE);
		BMOD = BMOD.add(BigInteger.ONE);
		BMOD = BMOD.add(BigInteger.ONE);
		BMOD = BMOD.add(BigInteger.ONE);

		long MOD = BMOD.longValue();

		int t = sc.nextInt();

		for (BigInteger tcase = BigInteger.ZERO; tcase.compareTo(BigInteger.valueOf(t)) < ZERO; tcase = tcase.add(BigInteger.ONE)) {
			int n = sc.nextInt();

			pr.println(fibonacciNumber(n, MOD));
		}
	}

	private static int ZERO = BigInteger.ZERO.intValue();
	private static int ONE = BigInteger.ONE.intValue();
	private static int TWO = BigInteger.ONE.add(BigInteger.ONE).intValue();
	private static int THREE = BigInteger.ONE.add(BigInteger.ONE).add(BigInteger.ONE).intValue();;

	private static long fibonacciNumber(long n, long MOD) {
		BigInteger[] ba = {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ZERO};
		BigInteger[] bx = {BigInteger.ONE, BigInteger.ONE.add(BigInteger.ONE)};
		while(n > ZERO) {
			if ((n & ONE) != ZERO) {
				BigInteger btmpzero = bx[ZERO].multiply(ba[ZERO]).add(bx[ONE].multiply(ba[ONE])).mod(BigInteger.valueOf(MOD));
				BigInteger btmpone = bx[ZERO].multiply(ba[TWO]).add(bx[ONE].multiply(ba[THREE])).mod(BigInteger.valueOf(MOD));
				bx[ZERO] = btmpzero;
				bx[ONE] = btmpone;
			}
			BigInteger btmpzero = ba[ZERO].multiply(ba[ZERO]).add(ba[ONE].multiply(ba[TWO])).mod(BigInteger.valueOf(MOD));
			BigInteger btmpone = ba[ONE].multiply(ba[ZERO].add(ba[THREE])).mod(BigInteger.valueOf(MOD));
			BigInteger btmptwo = ba[TWO].multiply(ba[ZERO].add(ba[THREE])).mod(BigInteger.valueOf(MOD));
			BigInteger btmpthree = ba[THREE].multiply(ba[THREE]).add(ba[ONE].multiply(ba[TWO])).mod(BigInteger.valueOf(MOD));
			ba[ZERO] = btmpzero;
			ba[ONE] = btmpone;
			ba[TWO] = btmptwo;
			ba[THREE] = btmpthree;

			n >>= ONE;
		}

		return bx[ONE].longValue();
	}

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

		solve();

		pr.flush();
	}

	static String INPUT = null;

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