結果
| 問題 |
No.16 累乗の加算
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-03 16:57:46 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 153 ms / 5,000 ms |
| コード長 | 988 bytes |
| コンパイル時間 | 4,065 ms |
| コンパイル使用メモリ | 74,308 KB |
| 実行使用メモリ | 41,376 KB |
| 最終ジャッジ日時 | 2024-12-24 06:34:33 |
| 合計ジャッジ時間 | 6,919 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 14 |
ソースコード
import java.util.*;
import java.io.*;
import java.math.*;
public class No16 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(), n = sc.nextInt();
long w = 1000003;
long total = 0;
for(int i = 0; i < n; i++) {
//double t = Math.pow(x, sc.nextInt());
double t = pow(x, sc.nextLong(), w);
// System.out.println(t);
total += t;
}
// System.out.println("total = " + (total % w));
System.out.println((total % w));
}
// 繰り返し2乗法
static public long pow(long x, long n, long w) {
long ans = 1;
while(n > 0) {
if((n & 1) == 1) {
ans = ans * x % w;
}
// 2進数で考え、桁を上げるたびに底数をかけていく。一周する度にx, x^2, x^4, x^8となる
x = x * x % w;
// System.out.println(x);
// 桁をずらす。「n = n >> 1」と同じ
n >>= 1;
}
return ans;
}
}