結果

問題 No.1097 Remainder Operation
ユーザー tonyu0tonyu0
提出日時 2020-06-26 23:13:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 1,487 ms
コンパイル使用メモリ 84,712 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2023-09-18 07:40:37
合計ジャッジ時間 6,209 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 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 1 ms
4,376 KB
testcase_07 AC 22 ms
4,376 KB
testcase_08 AC 22 ms
4,380 KB
testcase_09 AC 22 ms
4,376 KB
testcase_10 AC 22 ms
4,376 KB
testcase_11 AC 22 ms
4,376 KB
testcase_12 AC 213 ms
4,380 KB
testcase_13 AC 207 ms
4,376 KB
testcase_14 AC 208 ms
4,376 KB
testcase_15 AC 208 ms
4,376 KB
testcase_16 AC 209 ms
4,380 KB
testcase_17 AC 216 ms
4,380 KB
testcase_18 AC 206 ms
7,424 KB
testcase_19 AC 193 ms
5,260 KB
testcase_20 AC 194 ms
5,204 KB
testcase_21 AC 200 ms
6,796 KB
testcase_22 AC 199 ms
6,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#define rep(i, j, n) for (i64 i = (j); i < (n); ++i)
#define rrep(i, j, n) for (i64 i = (n)-1; i >= (j); --i)
using namespace std;
using i64 = int64_t;
constexpr int INF = 1 << 30;

int main()
{
  int n, q;
  cin >> n;
  int *a = new int[n];
  rep(i, 0, n) cin >> a[i];
  cin >> q;

  vector<int> map(n, 0);
  rep(i, 0, n) map[i] = (a[i] + i) % n;

  vector<int> used(n, 0);
  used[0] = true;
  int now = 0;
  i64 cycle = 0;
  i64 len = 0;
  i64 cval = 0;
  i64 lval = 0;
  vector<i64> val;
  vector<i64> cv;

  val.push_back(0);
  cv.push_back(0);
  while (true)
  {
    ++len;
    lval += a[now];
    val.push_back(lval);
    now = map[now];
    if (used[now] == 1)
    {
      ++cycle;
      cval += a[now];
      cv.push_back(cval);
    }
    if (used[now] == 2)
      break;
    ++used[now];
  }

  i64 path = len - cycle;
  rep(i, 0, q)
  {
    i64 k;
    cin >> k;
    if (k <= len)
      cout << val[k] << '\n';
    else
    {
      i64 ans = lval;
      i64 rem = k - len;
      ans += rem / cycle * cval;
      ans += cv[rem % cycle];
      cout << ans << '\n';
    }
  }
  delete[] a;
  return 0;
}
0