結果
問題 | No.1097 Remainder Operation |
ユーザー |
|
提出日時 | 2020-06-26 22:49:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 65 ms / 2,000 ms |
コード長 | 2,696 bytes |
コンパイル時間 | 3,738 ms |
コンパイル使用メモリ | 199,040 KB |
最終ジャッジ日時 | 2025-01-11 11:59:58 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 21 |
ソースコード
#include <bits/stdc++.h>using namespace std;template <class T> inline bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}template <class T> inline bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}void debug() { cerr << "\n"; }template <class T> void debug(const T &x) { cerr << x << "\n"; }template <class T, class... Args> void debug(const T &x, const Args &... args) {cerr << x << " ";debug(args...);}template <class T> void debugVector(const vector<T> &v) {for(const T &x : v) {cerr << x << " ";}cerr << "\n";}using ll = long long;#define ALL(v) (v).begin(), (v).end()#define RALL(v) (v).rbegin(), (v).rend()const double EPS = 1e-7;const int INF = 1 << 30;const ll LLINF = 1LL << 60;const double PI = acos(-1);constexpr int MOD = 1000000007;const int dx[4] = {1, 0, -1, 0};const int dy[4] = {0, 1, 0, -1};//-------------------------------------int main() {cin.tie(0);ios::sync_with_stdio(false);int n;cin >> n;vector<ll> a(n);for(auto &it : a) {cin >> it;}// ループ検出パートvector<bool> seen(n, false);vector<int> v;ll x = 0;while(1) {// cerr << x << " ";v.emplace_back(x % n);if(seen[x % n]) {break;}seen[x % n] = true;x += a[x % n];}// debug();// debugVector(v);vector<int> memo(n, -1);int loopsz = 0;int loopStartId = 0;for(int i = 0; i < v.size(); i++) {if(memo[v[i]] != -1) {loopsz = i - memo[v[i]];loopStartId = memo[v[i]];break;} else {memo[v[i]] = i;}}// debugVector(memo);// debug(loopsz, loopStartId);// ループに入る前までの累積和vector<ll> sum_before_loop(loopStartId + 1, 0);for(int i = 0; i < loopStartId; i++) {sum_before_loop[i + 1] = sum_before_loop[i] + a[v[i]];}// debugVector(sum_before_loop);vector<ll> sum_after_loop(loopsz + 1, 0);for(int i = 0; i < loopsz; i++) {int id = loopStartId + i;sum_after_loop[i + 1] = sum_after_loop[i] + a[v[id]];}// debugVector(sum_after_loop);int q;cin >> q;while(q--) {ll k;cin >> k;if(k <= loopStartId) {cout << sum_before_loop[k] << "\n";continue;}ll ans = sum_before_loop[loopStartId];k -= loopStartId;ans += (k / loopsz) * sum_after_loop[loopsz];ans += sum_after_loop[k % loopsz];cout << ans << "\n";}}