結果

問題 No.1097 Remainder Operation
ユーザー ぷらぷら
提出日時 2021-05-08 19:42:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 1,292 bytes
コンパイル時間 2,207 ms
コンパイル使用メモリ 209,128 KB
実行使用メモリ 5,260 KB
最終ジャッジ日時 2023-10-17 04:21:09
合計ジャッジ時間 7,136 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 22 ms
4,348 KB
testcase_08 AC 22 ms
4,348 KB
testcase_09 AC 22 ms
4,348 KB
testcase_10 AC 22 ms
4,348 KB
testcase_11 AC 22 ms
4,348 KB
testcase_12 AC 216 ms
4,348 KB
testcase_13 AC 215 ms
4,348 KB
testcase_14 AC 214 ms
4,348 KB
testcase_15 AC 214 ms
4,348 KB
testcase_16 AC 214 ms
4,348 KB
testcase_17 AC 220 ms
4,348 KB
testcase_18 AC 207 ms
5,260 KB
testcase_19 AC 205 ms
5,260 KB
testcase_20 AC 205 ms
5,260 KB
testcase_21 AC 205 ms
5,260 KB
testcase_22 AC 208 ms
5,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    long long X = 0;
    vector<bool> used(N);
    used[0] = true;
    vector<long long> res;
    res.push_back(0);
    int tmp = 0;
    while (true) {
        X += A[X%N];
        if(used[X%N]) {
            tmp = X%N;
            break;
        }
        used[X%N] = true;
        res.push_back(X);
    }
    int tmp2 = 0;
    for(int i = 0; i < res.size(); i++) {
        if(res[i]%N == tmp) {
            tmp2 = i;
        }
    }
    vector<long long>sum(tmp2+1);
    for(int i = 0; i < tmp2; i++) {
        sum[i+1] = sum[i]+A[res[i]%N];
    }
    int tmp3 = (int)res.size()-tmp2;
    vector<long long>sum2(tmp3+1);
    for(int i = 0; i < tmp3; i++) {
        sum2[i+1] = sum2[i]+A[res[i+tmp2]%N];
    }
    int Q;
    cin >> Q;
    while (Q--) {
        long long K;
        cin >> K;
        if(K <= tmp2) {
            cout << sum[K] << endl;
        }
        else {
            long long ans = 0;
            if(tmp2) {
                ans += sum[tmp2];
            }
            K -= tmp2;
            ans += sum2[tmp3]*(K/tmp3);
            ans += sum2[K%tmp3];
            cout << ans << endl;
        }
    }
}
0