結果
| 問題 |
No.1097 Remainder Operation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-06-27 13:05:24 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 37 ms / 2,000 ms |
| コード長 | 2,198 bytes |
| コンパイル時間 | 2,193 ms |
| コンパイル使用メモリ | 165,164 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-11-30 18:53:39 |
| 合計ジャッジ時間 | 4,351 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 21 |
ソースコード
#pragma region
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std; typedef long double ld; typedef long long ll;
typedef unsigned long long ull;
#define endl "\n"
#define MP make_pair
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(x,arr) for(auto& x:arr)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define VPII vector<PII>
#define VPLL vector<PLL>
#define FI first
#define SE second
#define ALL(x) (x).begin(), (x).end()
constexpr int INF=1<<30; constexpr ll LINF=1LL<<60; constexpr ll mod=1e9+7; constexpr int NIL = -1;
template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
#pragma endregion
//-------------------
int n;
const int MX = 1000000 + 5;
int A[MX];
vector<bool> visited(100000+5, false);
int main(){
cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(15);
cin >> n;
FOR(i,0,n-1) {
cin >> A[i];
}
int x = 0;
vector<int> v;
while(true) {
v.emplace_back(x);
if(visited[x]) break;
visited[x] = true;
(x += A[x]) %= n;
}
int twice = v.back();
v.pop_back();
vector<int> loop;
while(v.back() != twice) {
loop.emplace_back(v.back());
v.pop_back();
}
// cout << v.size() << endl;
loop.emplace_back(twice);
v.pop_back();
reverse(ALL(loop));
int p = v.size();
int q = loop.size();
// for(auto &x: v) cout << x << " ";
// for(auto &x:loop) cout << x << " ";
// cout << p << " " << q << endl;
vector<ll> front_sum(p+1, 0LL);
vector<ll> loop_sum(q+1, 0LL);
FOR(i,1,p) front_sum[i] = front_sum[i-1] + A[v[i-1]];
FOR(i,1,q) loop_sum[i] = loop_sum[i-1] + A[loop[i-1]];
int Q; cin >> Q;
while(Q--) {
ll k; cin >> k;
if(k <= p) {
cout << front_sum[k] << endl;
continue;
}
ll ans = front_sum[p];
k -= p;
cout << ans + loop_sum[q] * (k/q) + loop_sum[k%q] << endl;
}
return 0;
}