結果

問題 No.16 累乗の加算
ユーザー scachescache
提出日時 2014-11-12 03:46:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 5,000 ms
コード長 1,284 bytes
コンパイル時間 5,667 ms
コンパイル使用メモリ 72,784 KB
実行使用メモリ 56,092 KB
最終ジャッジ日時 2023-09-08 11:30:23
合計ジャッジ時間 8,258 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,644 KB
testcase_01 AC 129 ms
56,092 KB
testcase_02 AC 138 ms
55,416 KB
testcase_03 AC 131 ms
55,816 KB
testcase_04 AC 131 ms
55,748 KB
testcase_05 AC 133 ms
55,588 KB
testcase_06 AC 135 ms
55,648 KB
testcase_07 AC 135 ms
55,840 KB
testcase_08 AC 137 ms
55,692 KB
testcase_09 AC 139 ms
55,508 KB
testcase_10 AC 139 ms
55,408 KB
testcase_11 AC 128 ms
55,744 KB
testcase_12 AC 136 ms
55,876 KB
testcase_13 AC 135 ms
55,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


import java.util.Scanner;

/**
 * yukicoder no.17
 * @author scache
 *
 */
public class PowAddition {
	public static void main(String[] args) {
		PowAddition p = new PowAddition();
	}

	public PowAddition() {
		Scanner sc = new Scanner(System.in);
		long x = sc.nextLong();
		int n = sc.nextInt();
		long[] a = new long[n];
		for(int i=0;i<n;i++)
			a[i] = sc.nextLong();
		solve(x, a);
	}

	public void solve(long x, long[] a) {
		long res = 0;
		for(int i=0;i<a.length;i++){
			res += powMod(x, a[i]);
			res %= 1000003;
		}
		System.out.println(res);
	}
	
	public long powMod(long x, long a){
		long c = x;
		long res = 1;
		while(a>0){
			if((a&1)==1)
				res = (res*c)%1000003;
			c = (c*c)%1000003;
			a = a>>1;
		}
		return res;
	}
	
	
	// フェルマーの小定理を使う方法
//	#include <iostream>
//	#include <algorithm>
//	using namespace std;
//	#define REP(i, n) for(int(i)=0;(i)<(n);++(i))
//	typedef unsigned long long ull;
//	int x, N, a, MOD = 1000003;
//	ull res;
//	int n[1000003];
//	 
//	int main(){
//	    cin >> x >> N;
//	    n[0] = 1;
//	    REP(i,MOD) n[i+1] = (n[i] * x) % MOD;
//	    REP(i,N){ cin >> a; res += n[a%(MOD-1)]; }
//	    cout << res%MOD << endl;
//	    return 0;
//	}
}
0