結果

問題 No.3 ビットすごろく
ユーザー fushime2fushime2
提出日時 2016-09-23 17:08:28
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 998 bytes
コンパイル時間 1,396 ms
コンパイル使用メモリ 162,944 KB
実行使用メモリ 6,084 KB
最終ジャッジ日時 2024-04-28 22:21:22
合計ジャッジ時間 5,482 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define F first
#define S second

int N;
bool visited[10010];

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

int main() {
    memset(visited,0,sizeof(visited));
    cin >> N;
    queue<pair<int,int>> Q;
    Q.push(mp(1,1));

    while(Q.size()) {
        auto tmp = Q.front();
        if(tmp.F == N) {
            cout << tmp.S << "\n";
            return 0;
        }

        visited[tmp.F] = true;
        int mv = calc(tmp.F);
        int nxt = tmp.F + mv;
        if(!visited[nxt] and 1<=nxt and nxt < N) Q.push(mp(nxt, tmp.S+1));
        nxt = tmp.F - mv;
        if(!visited[nxt] and 1<=nxt and nxt < N) Q.push(mp(nxt, tmp.S+1));
        Q.pop();
    }

    cout << -1 << "\n";

    return 0;
}
0