結果

問題 No.1097 Remainder Operation
ユーザー kk
提出日時 2020-09-01 18:21:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,241 bytes
コンパイル時間 2,730 ms
コンパイル使用メモリ 202,932 KB
実行使用メモリ 4,888 KB
最終ジャッジ日時 2023-08-12 16:54:37
合計ジャッジ時間 7,861 ms
ジャッジサーバーID
(参考情報)
judge8 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 17 ms
4,380 KB
testcase_08 AC 17 ms
4,384 KB
testcase_09 AC 17 ms
4,380 KB
testcase_10 AC 18 ms
4,380 KB
testcase_11 AC 17 ms
4,380 KB
testcase_12 AC 155 ms
4,380 KB
testcase_13 AC 159 ms
4,380 KB
testcase_14 AC 158 ms
4,380 KB
testcase_15 AC 154 ms
4,380 KB
testcase_16 AC 154 ms
4,384 KB
testcase_17 AC 158 ms
4,384 KB
testcase_18 AC 157 ms
4,824 KB
testcase_19 AC 160 ms
4,888 KB
testcase_20 AC 159 ms
4,880 KB
testcase_21 AC 158 ms
4,832 KB
testcase_22 AC 161 ms
4,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++)
#define ALL(x) (x).begin(), (x).end()

const double PI = acos(-1);

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  vector<int> a(n);
  REP (i, n) cin >> a[i];

  int len = -1;
  int s = -1;
  vector<int> vis(n, -1);
  int x = 0;
  for (int i = 0; ; i++) {
    if (vis[x] != -1) {
      s = vis[x];
      len = i - s;
      break;
    }
    vis[x] = i;
    x += a[x];
    x %= n;
  }

  vector<long long> pref;
  vector<long long> cyc;
  x = 0;
  for (int i = 0; i < s + len; i++) {
    if (i < s) {
      long long tmp = pref.size() ? pref.back() : 0;
      pref.push_back(tmp + a[x]);
    } else {
      long long tmp = cyc.size() ? cyc.back() : 0;
      cyc.push_back(tmp + a[x]);
    }
    x += a[x];
    x %= n;
  }

  int q;
  cin >> q;
  while (q--) {
    long long k;
    cin >> k;
    --k;

    long long ret = 0;
    if (k < s) {
      ret = pref[k];
    } else {
      if (pref.size())
        ret += pref.back();
      k -= s;
      ret += k / len * cyc.back();
      ret += cyc[k % len];
    }
    cout << ret << endl;
  }
  
  return 0;
}
0