結果

問題 No.16 累乗の加算
ユーザー spacia
提出日時 2016-01-04 11:21:27
言語 Java
(openjdk 23)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 878 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 76,208 KB
実行使用メモリ 36,908 KB
最終ジャッジ日時 2024-06-26 05:07:17
合計ジャッジ時間 3,798 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.math.*;

class Main {
	
	public static void out (Object o) {
		System.out.println(o);
	}
	
	public static int power (int x , int a , int m) {
		long x2 = x;
		long ret = 1;
		while (a > 0) {
			if (a % 2 == 1)
				ret = ret * x2 % m;
			//out("ret = " + ret + " , a = " + a + " , x2 = " + x2);
			a /= 2;
			x2 = x2 * x2 % m;
		}
		return (int)ret;
	}
	
	public static void main (String[] args) throws IOException {
		BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in));
		
		String[] line1 = br.readLine().split(" ");
		String[] line2 = br.readLine().split(" ");
		
		int x = Integer.parseInt(line1[0]);
		int n = Integer.parseInt(line1[1]);
		int ans = 0, m = 1000003;
		
		for (int i = 0; i < n; i++) {
			int a = Integer.parseInt(line2[i]);
			ans = (ans + power(x,a,m)) % m;
		}
		
		out(ans);
	}
}
0