結果

問題 No.1097 Remainder Operation
コンテスト
ユーザー CPer
提出日時 2026-10-08 17:27:17
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
OLE  
実行時間 -
コード長 1,245 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,831 ms
コンパイル使用メモリ 354,536 KB
最終ジャッジ日時 2026-10-08 17:27:38
合計ジャッジ時間 15,293 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample OLE * 1 -- * 1
other -- * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// WRONG #8 (PREFIX_INT): prefix sums and cycle sum are stored in 32-bit int.
// Input: T, then T cases of (N / A / Q / K_1..K_Q).
// Fails when (tail + one cycle) sum exceeds 2^31-1 (big A, long path).
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

static void solve() {
    int n; scanf("%d", &n);
    vector<ll> a(n);
    for (auto &x : a) scanf("%lld", &x);

    vector<int> seen(n, -1);
    vector<int> pre(1, 0);                  // BUG: should be long long
    int r = 0, steps = 0;
    while (seen[r] == -1) {
        seen[r] = steps;
        pre.push_back(pre.back() + (int)a[r]);   // overflows past 2^31-1
        steps++;
        r = pre[steps] % n;                 // overflowed value -> wrong/negative index
    }
    int mu = seen[r], lam = steps - mu;
    int S = pre[steps] - pre[mu];           // BUG: cycle sum in int

    int q; scanf("%d", &q);
    while (q--) {
        ll k; scanf("%lld", &k);
        if (k <= steps) { printf("%lld\n", (ll)pre[k]); continue; }
        ll t = k - mu, full = t / lam, rem = t % lam;
        ll ans = (ll)pre[mu] + full * (ll)S + (ll)(pre[mu + rem] - pre[mu]);
        printf("%lld\n", ans);
    }
}

int main() {
    int T; scanf("%d", &T);
    while (T--) solve();
}
0