結果

問題 No.2394 部分和乗総和
ユーザー ks2mks2m
提出日時 2023-07-28 22:58:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 467 ms / 2,000 ms
コード長 886 bytes
コンパイル時間 2,337 ms
コンパイル使用メモリ 76,232 KB
実行使用メモリ 55,308 KB
最終ジャッジ日時 2024-04-16 06:55:36
合計ジャッジ時間 6,471 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
36,776 KB
testcase_01 AC 53 ms
37,124 KB
testcase_02 AC 51 ms
36,628 KB
testcase_03 AC 51 ms
36,616 KB
testcase_04 AC 52 ms
37,080 KB
testcase_05 AC 52 ms
37,100 KB
testcase_06 AC 53 ms
36,780 KB
testcase_07 AC 52 ms
36,480 KB
testcase_08 AC 52 ms
37,112 KB
testcase_09 AC 52 ms
36,592 KB
testcase_10 AC 52 ms
36,968 KB
testcase_11 AC 54 ms
37,084 KB
testcase_12 AC 52 ms
36,852 KB
testcase_13 AC 134 ms
40,404 KB
testcase_14 AC 279 ms
45,304 KB
testcase_15 AC 297 ms
45,316 KB
testcase_16 AC 342 ms
48,676 KB
testcase_17 AC 458 ms
55,172 KB
testcase_18 AC 467 ms
55,228 KB
testcase_19 AC 462 ms
55,188 KB
testcase_20 AC 434 ms
55,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		long m = Long.parseLong(sa[1]);
		int b = Integer.parseInt(sa[2]);
		sa = br.readLine().split(" ");
		long[] a = new long[n];
		for (int i = 0; i < n; i++) {
			a[i] = Long.parseLong(sa[i]);
		}
		br.close();

		long ans = power(m, a[0], b) + 1;
		ans %= b;
		for (int i = 1; i < n; i++) {
			ans += ans * power(m, a[i], b) % b;
			ans %= b;
		}
		System.out.println(ans);
	}

	static long power(long x, long n, int m) {
		if (n == 0) {
			return 1;
		}
		long val = power(x, n / 2, m);
		val = val * val % m;
		if (n % 2 == 1) {
			x %= m;
			val = val * x % m;
		}
		return val;
	}
}
0