結果

問題 No.1013 〇マス進む
ユーザー 👑 hitonanodehitonanode
提出日時 2023-06-04 17:33:09
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 4,435 bytes
コンパイル時間 1,451 ms
コンパイル使用メモリ 100,764 KB
実行使用メモリ 39,588 KB
最終ジャッジ日時 2023-08-28 08:17:39
合計ジャッジ時間 10,187 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 18 ms
9,292 KB
testcase_15 AC 31 ms
12,300 KB
testcase_16 AC 27 ms
12,260 KB
testcase_17 AC 15 ms
8,036 KB
testcase_18 AC 21 ms
9,616 KB
testcase_19 AC 10 ms
5,916 KB
testcase_20 AC 33 ms
12,028 KB
testcase_21 AC 15 ms
8,084 KB
testcase_22 AC 21 ms
9,308 KB
testcase_23 AC 7 ms
5,132 KB
testcase_24 AC 39 ms
15,724 KB
testcase_25 AC 36 ms
14,436 KB
testcase_26 AC 8 ms
5,268 KB
testcase_27 AC 13 ms
7,200 KB
testcase_28 AC 6 ms
4,652 KB
testcase_29 AC 32 ms
12,816 KB
testcase_30 AC 14 ms
7,288 KB
testcase_31 AC 24 ms
10,892 KB
testcase_32 AC 16 ms
9,052 KB
testcase_33 AC 33 ms
19,596 KB
testcase_34 AC 57 ms
27,000 KB
testcase_35 AC 64 ms
27,344 KB
testcase_36 AC 4 ms
4,868 KB
testcase_37 AC 4 ms
4,604 KB
testcase_38 AC 62 ms
30,536 KB
testcase_39 AC 21 ms
11,960 KB
testcase_40 AC 76 ms
28,572 KB
testcase_41 AC 16 ms
9,668 KB
testcase_42 AC 35 ms
18,096 KB
testcase_43 AC 21 ms
12,056 KB
testcase_44 AC 36 ms
19,944 KB
testcase_45 AC 32 ms
17,588 KB
testcase_46 AC 17 ms
10,088 KB
testcase_47 AC 29 ms
16,524 KB
testcase_48 AC 37 ms
20,176 KB
testcase_49 AC 72 ms
26,836 KB
testcase_50 AC 51 ms
25,712 KB
testcase_51 AC 44 ms
21,236 KB
testcase_52 AC 10 ms
7,220 KB
testcase_53 AC 13 ms
8,912 KB
testcase_54 AC 49 ms
27,836 KB
testcase_55 AC 19 ms
10,412 KB
testcase_56 AC 80 ms
34,656 KB
testcase_57 AC 62 ms
24,728 KB
testcase_58 AC 104 ms
38,424 KB
testcase_59 AC 96 ms
39,496 KB
testcase_60 AC 81 ms
39,588 KB
testcase_61 AC 31 ms
12,560 KB
testcase_62 AC 20 ms
5,336 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "other_algorithms/test/binary_lifting.yuki1013.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1013"

#line 2 "other_algorithms/binary_lifting.hpp"
#include <cassert>
#include <vector>

// Binary lifting (Doubling)
template <class S, S (*op)(S, S)> class binary_lifting {
    int n = 0;
    std::vector<std::vector<int>> _nexts;
    std::vector<std::vector<S>> _prods;

    void build_next() {
        std::vector<int> _next(n);
        std::vector<S> _prod(n);

        for (int i = 0; i < n; ++i) {
            if (int j = _nexts.back().at(i); isin(j)) {
                _next.at(i) = _nexts.back().at(j);
                _prod.at(i) = op(_prods.back().at(i), _prods.back().at(j));
            } else {
                _next.at(i) = j;
                _prod.at(i) = _prods.back().at(i);
            }
        }

        _nexts.push_back(std::move(_next));
        _prods.push_back(std::move(_prod));
    }

    inline bool isin(int i) const noexcept { return 0 <= i and i < n; }

public:
    // (up to) 2^d steps from `s`
    // Complexity: O(d) (Already precalculated) / O(nd) (First time)
    int pow_next(int s, int d) {
        assert(isin(s));
        while (int(_nexts.size()) <= d) build_next();
        return _nexts.at(d).at(s);
    }

    // Product of (up to) 2^d elements from `s`
    const S &pow_prod(int s, int d) {
        assert(isin(s));
        while (int(_nexts.size()) <= d) build_next();
        return _prods.at(d).at(s);
    }

    binary_lifting() = default;
    binary_lifting(const std::vector<int> &g, const std::vector<S> &w)
        : n(g.size()), _nexts(1, g), _prods(1, w) {
        assert(g.size() == w.size());
    }

    // (up to) k steps from `s`
    template <class Int> int kth_next(int s, Int k) {
        for (int d = 0; k > 0 and isin(s); ++d, k >>= 1) {
            if (k & 1) s = pow_next(s, d);
        }
        return s;
    }

    // Product of (up to) `len` elements from `s`
    template <class Int> S prod(int s, Int len) {
        assert(isin(s));
        assert(len > 0);
        int d = 0;
        while (!(len & 1)) ++d, len /= 2;

        S ret = pow_prod(s, d);
        s = pow_next(s, d);
        for (++d, len /= 2; len and isin(s); ++d, len /= 2) {
            if (len & 1) {
                ret = op(ret, pow_prod(s, d));
                s = pow_next(s, d);
            }
        }
        return ret;
    }

    // `start` から出発して「`left_goal` 以下または `right_goal` 以上」に到達するまでのステップ数
    // 単調性が必要
    int distance_monotone(int start, int left_goal, int right_goal) {
        assert(isin(start));

        if (start <= left_goal or right_goal <= start) return 0;

        int d = 0;
        while (left_goal < pow_next(start, d) and pow_next(start, d) < right_goal) {
            if ((1 << d) >= n) return -1;
            ++d;
        }

        int ret = 0, cur = start;
        for (--d; d >= 0; --d) {
            if (int nxt = pow_next(cur, d); left_goal < nxt and nxt < right_goal) {
                ret += 1 << d, cur = nxt;
            }
        }

        return ret + 1;
    }

    template <class F> long long max_length(const int s, F f, const int maxd = 60) {
        assert(isin(s));
        int d = 0;
        while (d <= maxd and f(pow_prod(s, d))) {
            if (!isin(pow_next(s, d))) return 1LL << maxd;
            ++d;
        }
        if (d > maxd) return 1LL << maxd;

        --d;

        int cur = pow_next(s, d);
        long long len = 1LL << d;
        S p = pow_prod(s, d);

        for (int e = d - 1; e >= 0; --e) {
            if (S nextp = op(p, pow_prod(cur, e)); f(nextp)) {
                std::swap(p, nextp);
                cur = pow_next(cur, e);
                len += 1LL << e;
            }
        }

        return len;
    }
};
#line 4 "other_algorithms/test/binary_lifting.yuki1013.test.cpp"
#include <iostream>
#include <tuple>
#line 7 "other_algorithms/test/binary_lifting.yuki1013.test.cpp"
using namespace std;

using S = long long;
S op(S l, S r) { return l + r; }

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<int> g(N);
    vector<long long> w(N);
    for (int i = 0; i < N; ++i) {
        cin >> w.at(i);
        g.at(i) = (i + w.at(i)) % N;
    }

    binary_lifting<long long, op> bl(g, w);

    for (int i = 0; i < N; ++i) cout << i + 1 + bl.prod(i, K) << '\n';
}
0