結果

問題 No.1097 Remainder Operation
ユーザー hipotaf_cpphipotaf_cpp
提出日時 2020-10-20 16:44:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,380 ms / 2,000 ms
コード長 1,613 bytes
コンパイル時間 819 ms
コンパイル使用メモリ 85,884 KB
実行使用メモリ 164,312 KB
最終ジャッジ日時 2023-09-28 13:43:40
合計ジャッジ時間 10,805 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 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,380 KB
testcase_07 AC 39 ms
19,216 KB
testcase_08 AC 40 ms
19,188 KB
testcase_09 AC 39 ms
19,188 KB
testcase_10 AC 39 ms
19,200 KB
testcase_11 AC 39 ms
19,212 KB
testcase_12 AC 406 ms
164,088 KB
testcase_13 AC 412 ms
164,048 KB
testcase_14 AC 420 ms
164,056 KB
testcase_15 AC 434 ms
164,132 KB
testcase_16 AC 404 ms
164,312 KB
testcase_17 AC 371 ms
164,120 KB
testcase_18 AC 378 ms
164,020 KB
testcase_19 AC 887 ms
164,148 KB
testcase_20 AC 894 ms
164,088 KB
testcase_21 AC 1,364 ms
164,080 KB
testcase_22 AC 1,380 ms
164,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <numeric>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

#if __has_include("debugger.cpp")
  #include "debugger.cpp"
#else
void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(tail) > 0) cout << " ";
  print(std::forward<Tail>(tail)...);
}
# endif

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

vector<ll> a;
ll n;
struct Ret {
  ll position, over;
};
vector<vector<Ret>> table;
#define DP table[i][k]
Ret dfs(ll i, ll k) {
  i %= n;
  if (DP.position != -1) return DP;
  if (k == 0) return DP = {(i + a[i]) % n, a[i] + i};
  Ret left = dfs(i, k - 1);
  Ret right = dfs(left.position, k - 1);
  return DP = {right.position, left.over + right.over - left.position};
}

int main() {
  cin >> n;
  a = vector<ll>(n);
  REP(i, n) cin >> a[i];
  ll input(q);

  table = vector<vector<Ret>>(n + 1, vector<Ret>(100, {-1, -1}));

  REP(j, q) {
    ll input(k);

    ll t = 1;
    while (1ll << t < k) t++;
    ll ans = 0;
    REP(i, t + 1) {
      if (k & 1) {
        ans = dfs(ans, i).over + (ans - ans % n);
      }
      k >>= 1;
    }
    print(ans);
  }
}
0