結果

問題 No.16 累乗の加算
ユーザー h_nosonh_noson
提出日時 2016-02-16 14:15:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 594 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 66,500 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 11:54:52
合計ジャッジ時間 1,238 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
using namespace std;

map<int,int> powx;

int mod = 1000003;

long long pow(int x, int n) {
    long long ret;
    if (powx.count(n))
        ret = powx[n];
    else if (n == 0)
        ret = 1;
    else if (n == 1)
        ret = x;
    else {
        ret = pow(x,n/2) * pow(x,n-n/2) % mod;
        powx[n] = ret;
    }
    return ret;
}

int main() {
    int x,n;
    cin >> x >> n;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        ans = (ans + pow(x,a)) % mod;
    }
    cout << ans << endl;
    return 0;
}
0