結果
問題 | No.1097 Remainder Operation |
ユーザー |
![]() |
提出日時 | 2020-07-03 14:31:59 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 235 ms / 2,000 ms |
コード長 | 1,477 bytes |
コンパイル時間 | 1,859 ms |
コンパイル使用メモリ | 175,908 KB |
実行使用メモリ | 69,504 KB |
最終ジャッジ日時 | 2024-09-16 17:02:20 |
合計ジャッジ時間 | 7,148 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 21 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define ll long long#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)#define rep(i,n) REP(i,0,n)#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)#define rrep(i,n) RREP(i,n-1,0)#define all(v) v.begin(), v.end()const int inf = 1e9+7;const ll longinf = 1LL<<60;const ll mod = 1e9+7;class Doubling {public:int N;int logK;vector<vector<pair<int, ll>>> next;Doubling(int N, vector<int>& ini, vector<ll>& A, ll K) {logK = log2(K);next = vector<vector<pair<int, ll>>>(logK+1, vector<pair<int, ll>>(N));rep(i, N) {next[0][i].first = ini[i];next[0][i].second = A[i];}rep(k, logK) {rep(i, N) {next[k+1][i].first = next[k][next[k][i].first].first;next[k+1][i].second = next[k][i].second + next[k][next[k][i].first].second;}}}pair<int, ll> query(int p, ll q) {ll sum = 0;for(int k=logK; k>=0; k--) {if(p == -1) break;if((q>>k)&1) {sum += next[k][p].second;p = next[k][p].first;}}return make_pair(p, sum);}};int main() {cin.tie(0);ios::sync_with_stdio(false);int N; cin >> N;vector<ll> A(N); rep(i, N) cin >> A[i];int Q; cin >> Q;vector<ll> K(Q); rep(i, Q) cin >> K[i];vector<int> ini(N);rep(i, N) ini[i] = (i+A[i])%N;Doubling db(N, ini, A, *max_element(all(K)));rep(i, Q) cout << db.query(0, K[i]).second << "\n";return 0;}