結果

問題 No.1097 Remainder Operation
ユーザー trineutron
提出日時 2020-06-27 07:22:54
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 822 bytes
コンパイル時間 4,145 ms
コンパイル使用メモリ 196,016 KB
最終ジャッジ日時 2025-01-11 12:35:18
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> a(n), last(n, -1);
    for (int i = 0; i < n; i++) {
        cin >> a.at(i);
    }
    int64_t p, r, s;
    vector<int64_t> x(n + 1);
    last.at(0) = 0;
    for (int i = 1;; i++) {
        x.at(i) = x.at(i - 1) + a.at(x.at(i - 1) % n);
        if (last.at(x.at(i) % n) != -1) {
            p = i - last.at(x.at(i) % n);
            r = last.at(x.at(i) % n);
            s = x.at(i) - x.at(r);
            break;
        }
        last.at(x.at(i) % n) = i;
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int64_t k;
        cin >> k;
        if (k < r) {
            cout << x.at(k) << "\n";
        } else {
            cout << x.at((k - r) % p + r) + (k - r) / p * s << "\n";
        }
    }
}
0