結果

問題 No.16 累乗の加算
ユーザー kyo1kyo1
提出日時 2020-06-10 15:36:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,099 bytes
コンパイル時間 2,976 ms
コンパイル使用メモリ 198,188 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 10:48:43
合計ジャッジ時間 3,149 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename T, typename U>
std::enable_if_t<std::is_same<T, int>::value, T> pow(T x, U n, T mod) {
  T res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<T>(static_cast<int64_t>(res) * static_cast<int64_t>(x) %
                           mod);
    x = static_cast<T>(static_cast<int64_t>(x) * static_cast<int64_t>(x) % mod);
    n >>= 1;
  }
  return res;
}

template <typename T, typename U>
std::enable_if_t<std::is_same<T, int64_t>::value, T> pow(T x, U n, T mod) {
  T res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<T>(static_cast<__int128>(res) *
                           static_cast<__int128>(x) % mod);
    x = static_cast<T>(static_cast<__int128>(x) * static_cast<__int128>(x) %
                       mod);
    n >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int x, N;
  cin >> x >> N;
  int res = 0, mod = 1000003;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    res += pow(x, a, mod);
    res %= mod;
  }
  cout << res << '\n';
  return 0;
}
0