結果

問題 No.16 累乗の加算
ユーザー atkrymatkrym
提出日時 2016-11-07 15:46:35
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,532 bytes
コンパイル時間 2,013 ms
コンパイル使用メモリ 81,732 KB
実行使用メモリ 84,316 KB
最終ジャッジ日時 2024-05-03 22:39:21
合計ジャッジ時間 9,259 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 OLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    private static long[] ary;
    public static void main(String[] args) throws Exception {
        long x = nextLong();
        int n = nextInt();
        ary = nextLong(n);
        long ans = 0;
        for (int i = 0;i < n;i++) {
            ans += pow(x, ary[i])%1000003;
        }
        System.out.println(ans);
    }
    
    private static long pow(long n, long p) {
        long ans;
        if (p==0) return 1;
        if (p==1) return n;
        if (p==2) return n*n;
        if (p%2==0) {
            ans = pow(n,p/2)*pow(n,p/2);
        } else {
            ans = pow(n,p/2)*pow(n,p/2)*n;
        }
        System.out.println(n+" "+p+" "+ans);
        return ans%1000003;
    }
    
    private static int nextInt() { return sc.nextInt(); }
    private static long nextLong() { return sc.nextLong(); }
    private static float nextFloat() { return sc.nextFloat(); }

    private static int[] nextInt(int n) {
        int[] ret = new int[n];
        for (int i = 0;i < n;i++) {
            ret[i] = nextInt();
        }
        return ret;
    }

    private static long[] nextLong(int n) {
        long[] ret = new long[n];
        for (int i = 0;i < n;i++) {
            ret[i] = nextLong();
        }
        return ret;
    }

    private static float[] nextFloat(int n) {
        float[] ret = new float[n];
        for (int i = 0;i < n;i++) {
            ret[i] = nextFloat();
        }
        return ret;
    }
}
0