結果
問題 |
No.16 累乗の加算
|
ユーザー |
|
提出日時 | 2022-05-17 20:39:28 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 79 ms / 5,000 ms |
コード長 | 1,210 bytes |
コンパイル時間 | 3,975 ms |
コンパイル使用メモリ | 79,108 KB |
実行使用メモリ | 37,952 KB |
最終ジャッジ日時 | 2024-09-15 15:57:25 |
合計ジャッジ時間 | 5,621 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 14 |
ソースコード
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; } }