結果

問題 No.16 累乗の加算
ユーザー zn_kiezn_kie
提出日時 2019-07-11 23:16:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 898 bytes
コンパイル時間 416 ms
コンパイル使用メモリ 59,508 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 06:26:23
合計ジャッジ時間 972 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i = 0; i < (n); ++i)

ll mypow(ll x, ll a){
    // 繰り返し2乗和
    // 例) x^22を計算する場合
    //     22(10進数)=10110(2進数)
    //     = 16 + 4 + 2 
    //     x^22 = x^16 * x^4 * x^2
    //     x^2がわかればそれの2乗がx^4,その2乗がx^8,その2乗がx^16
    int ret = 1;
    while(a){
        if(a&1){ // 最下位bitに1が立ってたらxをかける
            ret=ret*x%1000003;
        }
            a=a>>1; // bitの右シフト
            x=x*x%1000003; 
    }
    return ret;
}

int main(){
    ll x,N;
    cin >> x >> N;
    ll a;
    int ans = 0;
    // ループ内で1要素ずつ処理する
    rep(i,N){
        cin >> a;
        ans += mypow(x,a);
    }
    ans%=1000003;
    cout << ans << endl;
    return 0;
}
0