結果
問題 |
No.16 累乗の加算
|
ユーザー |
|
提出日時 | 2022-05-18 07:58:38 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 71 ms / 5,000 ms |
コード長 | 1,279 bytes |
コンパイル時間 | 4,313 ms |
コンパイル使用メモリ | 78,252 KB |
実行使用メモリ | 37,900 KB |
最終ジャッジ日時 | 2024-09-16 08:31:54 |
合計ジャッジ時間 | 5,660 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 14 |
ソースコード
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class No16_2 { static int K = 1000003; public static void main(String[] args) throws IOException{ //累乗の加算 二分累乗法で作り直し String[] text = readStr(); int x = Integer.parseInt(text[0].split(" ")[0]); int N = Integer.parseInt(text[0].split(" ")[1]) , a , i , len; int sum = 0 , rui; int[] r = new int[32]; String bit; r[0] = x; for(i = 1;i < 32;i++) { r[i] = (int)((long)Math.pow(r[i - 1], 2) % K); } for(i = 0;i < N;i++) { rui = 1; a = Integer.parseInt(text[1].split(" ")[i]); bit = Integer.toBinaryString(a); len = bit.length(); for(int k = 0;k < len;k++) { if(bit.charAt(k) == '1') { rui = (int)((long)rui * (long)r[len - k - 1] % K); } } sum = (sum + rui) % K; } System.out.println(sum); } 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; } }