結果

問題 No.3 ビットすごろく
ユーザー _yoshih__yoshih_
提出日時 2018-09-30 23:35:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 943 bytes
コンパイル時間 946 ms
コンパイル使用メモリ 75,876 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-20 13:48:50
合計ジャッジ時間 1,940 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
6,940 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
6,944 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 2 ms
6,940 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>

using namespace std;

static int count_bits(int i)
{
    auto b = 0;
    while (i > 0) {
        if (i & 0x01) ++b;
        i >>= 1;
    }

    return b;
}

static int move(const vector<int>& bits, vector<bool>& done, int n, int pos, int count)
{
    if (n == pos) return count;
    if (count > 1 && pos <= 1) return -1;

    if (done[pos]) return -1;
    done[pos] = true;

    if (pos + bits[pos] > n) return move(bits, done, n, pos - bits[pos], count + 1);
    return move(bits, done, n, pos + bits[pos], count + 1);
}

static void run()
{
    int n;
    cin >> n;

    vector<int> bits;
    bits.resize(n + 1);

    for (auto i = 1; i <= n; ++i) {
        bits[i] = count_bits(i);
    }

    vector<bool> done;
    done.resize(bits.size());
    const auto count = move(bits, done, n, 1, 1);
    cout << count << endl;
}

int main(int argc, char **argv)
{
    run();
    return 0;
}
0