結果

問題 No.3 ビットすごろく
ユーザー lowrlowr
提出日時 2018-11-02 01:44:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 825 bytes
コンパイル時間 1,687 ms
コンパイル使用メモリ 171,472 KB
実行使用メモリ 9,940 KB
最終ジャッジ日時 2023-09-14 01:09:07
合計ジャッジ時間 3,151 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,844 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 5 ms
6,732 KB
testcase_06 AC 3 ms
4,920 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
5,920 KB
testcase_09 AC 6 ms
7,716 KB
testcase_10 AC 7 ms
8,508 KB
testcase_11 AC 5 ms
7,268 KB
testcase_12 AC 5 ms
6,404 KB
testcase_13 AC 3 ms
4,516 KB
testcase_14 AC 7 ms
8,320 KB
testcase_15 AC 8 ms
9,712 KB
testcase_16 AC 8 ms
9,096 KB
testcase_17 AC 8 ms
9,644 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 8 ms
9,940 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 7 ms
8,828 KB
testcase_23 AC 9 ms
9,916 KB
testcase_24 AC 8 ms
9,884 KB
testcase_25 AC 8 ms
9,580 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 3 ms
4,708 KB
testcase_28 AC 8 ms
9,028 KB
testcase_29 AC 5 ms
7,332 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 6 ms
7,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int count(int n) {
    int ret = 0;
    while (n) {
        if (n & 1) ret++;
        n >>= 1;
    }
    return ret;
}

int main() {
    int n;
    std::cin >> n;
    std::vector<std::deque<int>> g(n + 1);
    for (int i = 1; i <= n; i++) {
        int k = count(i);
        if (i - k > 0) g[i].push_back(i - k);
        if (i + k <= n) g[i].push_back(i + k);
    }
    std::vector<int> d(n + 1, 100000000);
    d[1] = 1;
    std::deque<int> q;
    q.push_back(1);
    while (!q.empty()) {
        auto curr = q.front();
        for (auto to : g[curr]) {
            if (d[to] <= d[curr] + 1) continue;
            d[to] = std::min(d[to], d[curr] + 1);
            q.push_back(to);
        }
        q.pop_front();
    }
    std::cout << (d[n] == 100000000 ? -1 : d[n]) << std::endl;
    return 0;
}
0