結果

問題 No.1097 Remainder Operation
ユーザー KoDKoD
提出日時 2020-06-26 22:37:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 2,714 bytes
コンパイル時間 727 ms
コンパイル使用メモリ 82,944 KB
実行使用メモリ 6,116 KB
最終ジャッジ日時 2023-09-18 06:08:17
合計ジャッジ時間 5,438 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 23 ms
4,376 KB
testcase_08 AC 23 ms
4,380 KB
testcase_09 AC 23 ms
4,380 KB
testcase_10 AC 23 ms
4,376 KB
testcase_11 AC 22 ms
4,376 KB
testcase_12 AC 221 ms
4,380 KB
testcase_13 AC 215 ms
4,376 KB
testcase_14 AC 216 ms
4,380 KB
testcase_15 AC 217 ms
4,376 KB
testcase_16 AC 215 ms
4,376 KB
testcase_17 AC 226 ms
4,380 KB
testcase_18 AC 211 ms
6,116 KB
testcase_19 AC 203 ms
4,856 KB
testcase_20 AC 208 ms
4,852 KB
testcase_21 AC 208 ms
5,532 KB
testcase_22 AC 207 ms
5,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { ++i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { --i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  i32 N;
  std::cin >> N;
  std::vector<i32> A(N);
  for (auto &x: A) {
    std::cin >> x;
  }
  std::vector<i32> to(N);
  for (auto i: range(0, N)) {
    to[i] = (i + A[i]) % N;
  }
  std::vector<i64> branch, loop;
  branch.push_back(0);
  {
    std::vector<bool> done(N);
    i32 cur = 0;
    while (true) {
      done[cur] = true;
      branch.push_back(branch.back() + A[cur]);
      cur = to[cur];
      if (done[cur]) {
        i32 pos = cur;
        loop.push_back(0);
        while (true) {
          loop.push_back(loop.back() + A[pos]);
          pos = to[pos];
          if (pos == cur) {
            break;
          }
        }
        break;
      }
    }
  }
  i32 Q;
  std::cin >> Q;
  while (Q--) {
    i64 K;
    std::cin >> K;
    if (K < branch.size()) {
      std::cout << branch[K] << '\n';
    }
    else {
      K -= (branch.size() - 1);
      i64 ans = 0;
      ans += branch.back();
      ans += (K / (loop.size() - 1)) * loop.back();
      ans += loop[K % (loop.size() - 1)];
      std::cout << ans << '\n';
    }
  }
  return 0;
}
0