結果

問題 No.3 ビットすごろく
ユーザー mamekinmamekin
提出日時 2014-12-09 22:37:26
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,144 bytes
コンパイル時間 926 ms
コンパイル使用メモリ 96,704 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 07:07:48
合計ジャッジ時間 1,732 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

int countBit(int a)
{
    int cnt = 0;
    while(a > 0){
        if(a & 1)
            ++ cnt;
        a >>= 1;
    }
    return cnt;
}

int main()
{
    int n;
    cin >> n;

    vector<int> dp(n+1, INT_MAX);
    queue<int> q;
    dp[1] = 1;
    q.push(1);
    while(!q.empty()){
        int a = q.front();
        q.pop();
        int x = countBit(a);
        if(a - x >= 1 && dp[a-x] == INT_MAX){
            dp[a-x] = dp[a] + 1;
            q.push(a-x);
        }
        if(a + x <= n && dp[a+x] == INT_MAX){
            dp[a+x] = dp[a] + 1;
            q.push(a+x);
        }
    }

    if(dp[n] < INT_MAX)
        cout << dp[n] << endl;
    else
        cout << -1 << endl;

    return 0;
}
0