結果

問題 No.3 ビットすごろく
コンテスト
ユーザー Ricky_pon
提出日時 2020-05-17 20:50:35
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,388 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,329 ms
コンパイル使用メモリ 215,568 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-10 04:37:53
合計ジャッジ時間 2,758 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 1510;

int main(){
    int n;
    scanf("%d", &n);
    int d[n+1];
    fill(d, d+n+1, mod);
    queue<pii> que;
    que.emplace(1, 1);
    d[1] = 1;
    while(!que.empty()){
        auto [td, v] = que.front();
        que.pop();
        int p = __builtin_popcount(v);
        if(v-p >= 1 && chmin(d[v-p], td + 1)){
            que.emplace(d[v-p], v-p);
        }
        if(v+p <= n && chmin(d[v+p], td + 1)){
            que.emplace(d[v+p], v+p);
        }
    }
    printf("%d\n", d[n] == mod ? -1 : d[n]);
}
0