結果

問題 No.613 Solitude by the window
ユーザー 37zigen37zigen
提出日時 2017-12-22 01:35:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 1,265 bytes
コンパイル時間 2,531 ms
コンパイル使用メモリ 77,640 KB
実行使用メモリ 41,976 KB
最終ジャッジ日時 2024-05-09 17:08:46
合計ジャッジ時間 7,313 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
41,504 KB
testcase_01 AC 131 ms
41,976 KB
testcase_02 AC 132 ms
41,356 KB
testcase_03 AC 133 ms
41,376 KB
testcase_04 AC 132 ms
41,424 KB
testcase_05 AC 131 ms
41,656 KB
testcase_06 AC 132 ms
41,744 KB
testcase_07 AC 125 ms
41,620 KB
testcase_08 AC 128 ms
41,544 KB
testcase_09 AC 127 ms
41,564 KB
testcase_10 AC 134 ms
41,376 KB
testcase_11 AC 135 ms
41,508 KB
testcase_12 AC 136 ms
41,888 KB
testcase_13 AC 128 ms
41,360 KB
testcase_14 AC 136 ms
41,388 KB
testcase_15 AC 139 ms
41,872 KB
testcase_16 AC 139 ms
41,492 KB
testcase_17 AC 135 ms
41,628 KB
testcase_18 AC 134 ms
41,248 KB
testcase_19 AC 139 ms
41,728 KB
testcase_20 AC 137 ms
41,464 KB
testcase_21 AC 134 ms
41,760 KB
testcase_22 AC 138 ms
41,460 KB
testcase_23 AC 136 ms
41,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

class Main {
	long N;
	long M;

	class S {
		long a, b;

		public S(long a_, long b_) {
			a = a_ % M;
			b = b_ % M;
		}

		S mul(S s) {
			return new S(a * s.a % M + 3 * b % M * s.b % M, a * s.b % M + b * s.a % M);
		}

		S add(S s) {
			return new S(a + s.a, b + s.b);
		}

		S pow(long n) {
			S ret = new S(1, 0);
			S mul = new S(a, b);
			for (; n > 0; n >>= 1, mul = mul.mul(mul)) {
				if (n % 2 == 1) {
					ret = ret.mul(mul);
				}
			}
			return ret;
		}
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		M = sc.nextLong();
		S s1 = new S(2, 1);
		S s2 = new S(2, -1);
		S ret;
		if (f(3) == 1) {
			ret = s1.pow(pow(2, N, M - 1)).add(s2.pow(pow(2, N, M - 1)));
		} else {
			ret = s1.pow(pow(2, N, M + 1)).add(s2.pow(pow(2, N, M + 1)));
		}
		System.out.println((ret.a - 2 + M) % M);

	}

	long f(long a) {
		return pow(a, (M - 1) / 2, M);
	}

	long pow(long a, long n, long mod) {
		long ret = 1;
		for (; n > 0; n >>= 1, a = a * a % mod) {
			if (n % 2 == 1) {
				ret = ret * a % mod;
			}
		}
		return ret;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

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

}
0