結果

問題 No.3 ビットすごろく
ユーザー _yoshih__yoshih_
提出日時 2018-09-30 23:18:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 800 bytes
コンパイル時間 548 ms
コンパイル使用メモリ 69,104 KB
実行使用メモリ 8,704 KB
最終ジャッジ日時 2024-04-20 13:48:39
合計ジャッジ時間 7,030 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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, int n, int pos, int count)
{
    if (n == pos) return count;
    if (count > 1 && pos <= 1) return -1;

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

    return move(bits, 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);
    }

    const auto count = move(bits, n, 1, 1);
    cout << count << endl;
}

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