結果

問題 No.3 ビットすごろく
ユーザー dazydazy
提出日時 2017-11-24 14:04:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,531 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 52,700 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 00:50:37
合計ジャッジ時間 1,820 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

#define fastcin {\
    cin.tie(0);\
    ios::sync_with_stdio(false);\
}
#define rep(i, a, b) for(int i = a; i < b; i++)
#define REP(i, a) for(int i = 0; i < a; i++)
#define scan(x) cin >> n;
#define print(x) cout << x << "\n"

int countbit(int n) {
    int count = 0;
    for (int i = 1; i < n + 1; i <<= 1) if (n & i) count++;
    return count;
}

int main() {
    fastcin;
    int n;
    scan(n);

    int masu[10001], tmp[10001];
    REP(i, 10001) {
        masu[i] = 10002;
        tmp[i] = 10002;
    }

    int ans = -1;
    masu[1] = 1;
    tmp[1] = 1;

    int move, max = 1, p ,m, flag;
    if (n != 1) {
        while (1) {
            flag = 0;
            rep(i, 1, max+1) {
                if (masu[i] != 10002 && masu[i] != -1) {
                    flag++;
                    move = countbit(i);
                    p = i + move;
                    m = i - move;
                    if (p == n || m == n) {
                        print(masu[i] + 1);
                        exit(0);
                    }

                    if (p < n) if (masu[p] == 10002) {
                            if (max < p) max = p;
                            tmp[p] = masu[i] + 1;
                        }
                    if (m > 0) if (masu[m] == 10002) tmp[m] = masu[i] + 1;

                    tmp[i] = -1;
                }
            }
            if (flag == 0) break;
            rep(i, 1, max+1) masu[i] = tmp[i];
        }
    } else ans = 1;

    print(ans);
    return 0;
}
0