結果

問題 No.3 ビットすごろく
ユーザー RTnF
提出日時 2019-07-25 00:31:04
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,980 bytes
コンパイル時間 2,371 ms
コンパイル使用メモリ 198,472 KB
最終ジャッジ日時 2025-01-07 07:28:04
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define repr(i, a, b) for(ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b){ return b?GCD(b, a % b):a; }
inline ll LCM(ll a, ll b){ return a/GCD(a, b)*b; }
inline ll powint(ll x, ll y){ ll r=1; while(y){ if(y&1) r*=x; x*=x; y>>=1; } return r; }
inline ll powmod(ll x, ll y, ll m = MOD){ ll r=1; while(y){ if(y&1) r*=x; x*=x; r%=m; x%=m; y>>=1; } return r; }
template<class T>bool chmax(T &a, const T &b){ if(a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a) { a=b; return 1; } return 0; }
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

ll n;
vi visited(10010, 0);

// グラフに対する幅優先探索
int bfs(ll start, ll goal){
    queue<pll> q;
    visited[start] = 1;
    ll depth = 0;
    ll now = start;
    q.push(make_pair(now, depth));
    while(!q.empty()){
        now = q.front().first;
        depth = q.front().second;
        q.pop();
        dump(now, depth);
        if(now == goal){
            return depth+1;
        }
        rep(i, 2){
            int pc = __builtin_popcountll(now);
            ll next = (i == 0 ? now+pc : now-pc);
            dump(next);
            if(next >= 1 && next <= n && !visited[next]){
                visited[next] = 1;
                q.push(make_pair(next, depth+1));
            }
        }
    }
    return -1;
}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    cin >> n;
    cout << bfs(1, n) << endl;
    return 0;
}
0