結果

問題 No.3 ビットすごろく
ユーザー tamaron
提出日時 2019-12-18 14:13:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 956 bytes
コンパイル時間 1,648 ms
コンパイル使用メモリ 174,552 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 09:33:43
合計ジャッジ時間 2,623 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
const long long INF = 1e18;

vector<ll> dp(10100,INF);
vector<bool> visited(10100,false);
queue<ll> que;

ll stands(ll num){
    ll cnt=0;
    while(num>0){
        if(num%2==1) ++cnt;
        num/=2;
    }
    return cnt;
}

int main() {
    ll N;
    cin>>N;
    que.push(1);
    dp[1]=1;
    while(!que.empty()){
        ll node=que.front();que.pop();
        if(visited[node]) continue;
        visited[node]=true;
        ll st=stands(node);
        if (0<node-st && node-st<=N){
            dp[node-st]=dp[node-st]<dp[node]+1 ? dp[node-st] : dp[node]+1;
            que.push(node-st);
        }
        if (0<node+st && node+st<=N){
            dp[node+st]=dp[node+st]<dp[node]+1 ? dp[node+st] : dp[node]+1;
            que.push(node+st);
        }
    }
    if (dp[N]==INF) cout<<-1<<endl;
    else cout<<dp[N]<<endl;

    return 0;
}
0