結果

問題 No.16 累乗の加算
ユーザー DialBird
提出日時 2016-12-17 12:01:26
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 797 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 62,600 KB
実行使用メモリ 7,284 KB
最終ジャッジ日時 2024-12-14 04:19:53
合計ジャッジ時間 1,289 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>

using namespace std;

#define REP(i,first,last) for (int i=first;i<last;i++)
#define FOR(it,con) for (auto it=con.begin();it!=con.end();it++)

const int devider = 1000002;
int x,N;
vector<int> amari_list(devider, -1);

int pow(long x, int a) {
  int res = 1;
  if (a > 0) {
    res = pow(x*x%(devider+1), a/2);
    if (a & 1) res = res*x%(devider+1);
  }
  return res;
}

int main(){
  cin >> x;
  cin >> N;
  int val;
  long result = 0;
  REP(i,0,N) {
    cin >> val;
    int target = val % devider;

    if (amari_list[target] == -1) {
      int amari = pow(x, val);
      amari_list[target] = amari;
    }
    result += amari_list[target];
    result %= (devider+1);
  }

  cout << (result % (devider + 1)) << endl;
}
0