結果

問題 No.3 ビットすごろく
ユーザー yogi_cg
提出日時 2020-03-26 23:29:30
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,272 bytes
コンパイル時間 1,565 ms
コンパイル使用メモリ 173,392 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 09:42:39
合計ジャッジ時間 2,771 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 

#define REP(i, b) for(int i = 0; i < (b); i++)
#define REPS(i, b) for(int i = 1; i <= (b); i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vd = vector<double>;
using vs = vector<string>;
using vb = vector<bool>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvb = vector<vb>;

const int INF = 1e9;
const int MOD = 1e9+7;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

signed main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    int N; cin >> N;
    vi d(N+1, INF); d[1] = 0;
    queue<int> q; q.push(1);
    while(!q.empty())
    {
        int p = q.front(); q.pop();
        int b = __builtin_popcount(p);
        if(p+b <= N && chmin(d[p+b], d[p]+1)) q.push(p+b);
        if(p-b >= 1 && chmin(d[p-b], d[p]+1)) q.push(p-b);
    }
    if(d[N] == INF) cout << -1 << endl;
    else cout << d[N]+1 << endl;
}
0