結果

問題 No.16 累乗の加算
ユーザー jp_stejp_ste
提出日時 2016-02-12 16:06:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 5,000 ms
コード長 683 bytes
コンパイル時間 2,544 ms
コンパイル使用メモリ 71,444 KB
実行使用メモリ 56,000 KB
最終ジャッジ日時 2023-09-08 11:53:48
合計ジャッジ時間 4,885 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,628 KB
testcase_01 AC 125 ms
55,572 KB
testcase_02 AC 139 ms
55,536 KB
testcase_03 AC 126 ms
55,624 KB
testcase_04 AC 129 ms
55,688 KB
testcase_05 AC 132 ms
55,924 KB
testcase_06 AC 132 ms
55,784 KB
testcase_07 AC 132 ms
55,288 KB
testcase_08 AC 136 ms
55,488 KB
testcase_09 AC 133 ms
55,676 KB
testcase_10 AC 132 ms
56,000 KB
testcase_11 AC 127 ms
55,576 KB
testcase_12 AC 134 ms
55,800 KB
testcase_13 AC 132 ms
55,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		long sum = 0;		
		int x = scan.nextInt();
		int N = scan.nextInt();
		int m = 1000003;
		
		for(int i=0; i<N; i++) {
			int a = scan.nextInt();
			sum += pow(x,a,m);
		}
		scan.close();
		
		System.out.println(sum % m);
	}
	
	static long pow(int x, int a, int m) {
		if(a==0) return 1;
		long ret = 1L;
		
		long v = x % m;
		String b = Integer.toBinaryString(a);
		if(b.charAt(b.length()-1) == '1') ret = v;
		
		for(int i=b.length()-2; i>=0; i--) {
			v = (v * v) % m;
			if(b.charAt(i) == '1') {
				ret = (ret * v) % m;
			}
		}
		return ret;
	}
}
0