結果

問題 No.3 ビットすごろく
ユーザー hanaokaunderbarhanaokaunderbar
提出日時 2019-04-17 16:30:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,959 bytes
コンパイル時間 685 ms
コンパイル使用メモリ 92,260 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 01:16:18
合計ジャッジ時間 2,060 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<algorithm>
#include<tuple>
#include<utility>
#include<cctype>
#include<climits>
#include<map>
#include<queue>
#include<functional>
#include<stack>
#include<iomanip>
#include<sstream>
#include<bitset>

using namespace std;

#define REP(i,a,n) for(int i=a;i<n;++i)
#define REPL(i,a,n) for(ll i=a;i<n;++i)
#define RUP(a,b) ((a+b-1)/(b))
#define ENT "\n"
#define SORTVG(v) sort(v.begin(),v.end(),greater<>())
#define SORTV(v) sort(v.begin(),v.end())
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define INF LLONG_MAX/2

typedef long long ll;
typedef tuple<int,int,bool> Tb;
typedef pair<int,int> Pii;
typedef vector<int> Vi;

template<class T> void chmax(T& a, T b) {if(a < b){a=b;}}
template<class T> void chmin(T& a, T b) {if(a > b){a=b;}}
template<class T> void YesNo(T& a) {if(a){cout << "Yes" << ENT;}else{cout << "No" << ENT;}}
template<class T> void YESNO(T& a) {if(a){cout << "YES" << ENT;}else{cout << "NO" << ENT;}}

int bits(int num)
{
    int dig;
    int sum = 0;
    while(num){
        dig = num % 2;
        sum = sum + dig;
        num = num / 2;
    }
    return sum;
}


int atcoder(){
    int n;
    cin>>n;
    Vi visited = Vi(n+1,0);
    visited[1] = 1;
    queue< pair<int,int> > q;
    q.push(make_pair(1,1));
    while(q.size()>0){
        int now= q.front().first;
        int depth= q.front().second;
        q.pop();
        int bitcount=bits(now);
        if(now+bitcount<=n && visited[now+bitcount]==0){
            visited[now+bitcount]=depth+1;
            q.push(make_pair(now+bitcount,depth+1));
        }
        if(1<=now-bitcount && visited[now-bitcount]==0){
            visited[now-bitcount]=depth+1;
            q.push(make_pair(now-bitcount,depth+1));
        }
    }
    cout<<(visited[n]!=0 ? visited[n] : -1)<<ENT;
    return 0;
}


signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    atcoder();
    return 0;
}
0