結果

問題 No.1097 Remainder Operation
ユーザー hedwig100hedwig100
提出日時 2020-08-16 11:52:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,690 bytes
コンパイル時間 2,007 ms
コンパイル使用メモリ 172,020 KB
実行使用メモリ 5,812 KB
最終ジャッジ日時 2023-08-01 19:13:27
合計ジャッジ時間 6,482 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 35 ms
4,436 KB
testcase_13 AC 36 ms
4,540 KB
testcase_14 AC 35 ms
4,432 KB
testcase_15 AC 35 ms
4,424 KB
testcase_16 AC 35 ms
4,536 KB
testcase_17 AC 37 ms
4,376 KB
testcase_18 AC 35 ms
5,756 KB
testcase_19 AC 36 ms
5,812 KB
testcase_20 AC 36 ms
5,620 KB
testcase_21 AC 35 ms
5,672 KB
testcase_22 AC 34 ms
5,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N; cin >> N;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];

    int now = 0;
    vector<int> visited(N,0);
    vector<int> route;
    while (visited[now] == 0) {
        visited[now] = 1;
        route.push_back(now);
        now = (now + A[now])%N;
    }

    vector<ll> before,loop;
    ll before_size,loop_size;
    ll sumbefore = 0,sumloop = 0;
    bool flag = false;
    rep(i,route.size()) {
        if (route[i] == now) flag = true;
        if (flag) {
            loop.push_back(A[route[i]]);
            sumloop += A[route[i]];
        } else {
            before.push_back(A[route[i]]);
            sumbefore += A[route[i]];
        }
    }
    before_size = before.size();
    loop_size = loop.size();

    rep(i,before_size - 1) before[i + 1] += before[i];
    rep(i,loop_size - 1) loop[i + 1] += loop[i];

    int Q; cin >> Q;
    rep(i,Q) {
        ll K; cin >> K;
        if (K <= before_size) cout << before[K - 1] << '\n';
        else {
            ll ret = sumbefore;
            K -= before_size;
            ret += K/loop_size * sumloop;
            if (K%loop_size > 0) ret += loop[K%loop_size - 1];
            cout << ret << '\n';
        }
    }
    return 0;
}
0