結果

問題 No.16 累乗の加算
ユーザー YamaKasaYamaKasa
提出日時 2018-06-13 01:47:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 5,000 ms
コード長 625 bytes
コンパイル時間 2,761 ms
コンパイル使用メモリ 72,824 KB
実行使用メモリ 56,492 KB
最終ジャッジ日時 2023-09-13 03:45:42
合計ジャッジ時間 4,752 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,884 KB
testcase_01 AC 120 ms
55,848 KB
testcase_02 AC 132 ms
56,480 KB
testcase_03 AC 126 ms
56,316 KB
testcase_04 AC 127 ms
56,268 KB
testcase_05 AC 126 ms
56,344 KB
testcase_06 AC 131 ms
56,004 KB
testcase_07 AC 129 ms
55,980 KB
testcase_08 AC 130 ms
56,492 KB
testcase_09 AC 135 ms
56,224 KB
testcase_10 AC 132 ms
56,052 KB
testcase_11 AC 121 ms
55,828 KB
testcase_12 AC 135 ms
56,344 KB
testcase_13 AC 130 ms
56,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int x = scan.nextInt();
		int N = scan.nextInt();
		long []a = new long[N];
		for(int i = 0; i < N; i++) {
			a[i] = scan.nextInt();
		}
		scan.close();
		int k = 1000003;
		long ans = 0;
		for(int i = 0; i < N; i++) {
			ans += pow(x, a[i], k);
			ans = ans % k;
		}
		System.out.println(ans % k);
	}
	public static long pow(long x, long n, int k) {
		if(n == 0) {
			return 1;
		}
		if(n % 2  == 0) {
			return pow(x * x % k, n / 2, k);
		}else {
			return x * pow(x, n - 1, k) % k;
		}
	}
}
0