結果

問題 No.1097 Remainder Operation
ユーザー siro53siro53
提出日時 2020-06-26 22:49:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,696 bytes
コンパイル時間 2,547 ms
コンパイル使用メモリ 204,984 KB
実行使用メモリ 5,480 KB
最終ジャッジ日時 2023-09-18 06:42:38
合計ジャッジ時間 4,896 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 37 ms
4,380 KB
testcase_13 AC 37 ms
4,404 KB
testcase_14 AC 37 ms
4,380 KB
testcase_15 AC 37 ms
4,408 KB
testcase_16 AC 37 ms
4,404 KB
testcase_17 AC 38 ms
4,384 KB
testcase_18 AC 36 ms
5,316 KB
testcase_19 AC 37 ms
5,480 KB
testcase_20 AC 36 ms
5,476 KB
testcase_21 AC 37 ms
5,412 KB
testcase_22 AC 37 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
    }
}
0