結果

問題 No.16 累乗の加算
ユーザー shinshin
提出日時 2022-05-17 20:39:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 65 ms / 5,000 ms
コード長 1,210 bytes
コンパイル時間 5,730 ms
コンパイル使用メモリ 75,432 KB
実行使用メモリ 50,484 KB
最終ジャッジ日時 2023-10-13 20:17:46
合計ジャッジ時間 7,342 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,252 KB
testcase_01 AC 44 ms
49,436 KB
testcase_02 AC 65 ms
50,200 KB
testcase_03 AC 46 ms
49,280 KB
testcase_04 AC 49 ms
49,392 KB
testcase_05 AC 50 ms
49,440 KB
testcase_06 AC 54 ms
49,440 KB
testcase_07 AC 52 ms
47,736 KB
testcase_08 AC 57 ms
49,364 KB
testcase_09 AC 64 ms
50,484 KB
testcase_10 AC 54 ms
49,504 KB
testcase_11 AC 45 ms
49,656 KB
testcase_12 AC 52 ms
49,264 KB
testcase_13 AC 50 ms
49,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class No16 {
	
	static long K = 1000003;

	public static void main(String[] args) throws IOException{
		//累乗の加算
		String[] text = readStr();
		Long x = Long.parseLong(text[0].split(" ")[0]) , a;
		int N = Integer.parseInt(text[0].split(" ")[1]);
		long sum = 0;
		
		for(int i = 0;i < N;i++) {
			a = Long.parseLong(text[1].split(" ")[i]);
			sum += div(x , a);
			sum %= K;
		}
		
		System.out.println(sum);
		
	}
		
	
	public static long div(long x , long a) {
		long ans , c = 1;
		
		if(x <= 1) {
			ans = x;
		}else {
			while((long)Math.pow(x, c) < K) c++;
			if(c > a) {
				ans = (long)Math.pow(x, a);
			}else {
				ans = (div((long)Math.pow(x, c) % K , a / c) * (long)Math.pow(x, a % c)) % K;
			}
		}
		
		return ans;
	}
	
	public static String[] readStr() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList<String> list = new ArrayList<>();

		do {
			list.add(br.readLine());
		}while(br.ready());

		br.close();

		String[] text = new String[list.size()];
		list.toArray(text);

		return text;

	}

}
0