結果

問題 No.16 累乗の加算
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-11-17 23:30:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 138 ms / 5,000 ms
コード長 699 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 71,184 KB
実行使用メモリ 56,320 KB
最終ジャッジ日時 2023-09-08 12:03:39
合計ジャッジ時間 4,900 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,672 KB
testcase_01 AC 126 ms
55,504 KB
testcase_02 AC 138 ms
55,548 KB
testcase_03 AC 130 ms
55,504 KB
testcase_04 AC 132 ms
55,644 KB
testcase_05 AC 131 ms
55,704 KB
testcase_06 AC 135 ms
55,624 KB
testcase_07 AC 131 ms
55,520 KB
testcase_08 AC 134 ms
55,792 KB
testcase_09 AC 135 ms
55,372 KB
testcase_10 AC 133 ms
55,556 KB
testcase_11 AC 123 ms
55,480 KB
testcase_12 AC 135 ms
56,320 KB
testcase_13 AC 132 ms
55,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        int N = sc.nextInt();
        long m = 1000003;
        long sum=0;
        for(int i=0; i<N; i++){
            long a = sc.nextInt();
            sum+=pow(x,a);
            sum%=m;
        }
        System.out.println(sum);
    }
    static long pow(long x, long n){
        long m = 1000003;
        x%=m;
        if(n==0){
            return 1;
        }else if(n%2==1){
            long r = pow(x,n-1)%m;
            return x*r%m;
        }else{
            long r = pow(x,n/2)%m;
            return r*r%m;
        }
    }
}
0