結果

問題 No.3036 Restricted Lucas (Easy)
ユーザー GrenacheGrenache
提出日時 2018-04-02 00:58:45
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,799 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 75,556 KB
実行使用メモリ 58,968 KB
最終ジャッジ日時 2023-09-08 13:12:55
合計ジャッジ時間 4,293 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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