#include #include #include #include #include #include #include #include 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 void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) > 0) cout << " "; print(std::forward(tail)...); } # endif void in() { } template void in(Head&& head, Tail&&... tail) { cin >> head; in(std::forward(tail)...); } vector a; ll n; struct Ret { ll position, over; }; vector> 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(n); REP(i, n) cin >> a[i]; ll input(q); table = vector>(n + 1, vector(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); } }