結果

問題 No.3 ビットすごろく
コンテスト
ユーザー kongroo
提出日時 2018-01-06 14:35:51
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 822 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,028 ms
コンパイル使用メモリ 175,528 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2026-05-29 09:56:23
合計ジャッジ時間 2,433 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define FI(i,a,b) for(int i=(a);i<=(b);i++)
#define FD(i,b,a) for(int i=(b);i>=(a);i--)
#define len(a) int((a).size())
using namespace std;
using LL = long long;
using PII = pair<int, int>;


const int N = 10005;
int F[N];

int dfs(int x, int target, bitset<N> b) {
    if (F[x] != -1) return F[x];
    if (x == target) return F[x] = 0;
    b.set(x);
    int t = __builtin_popcount(x);
    int ans = 1 << 30;
    if (x > t && !b.test(x-t)) {
        ans = 1 + dfs(x-t, target, b);
    }
    if (x + t <= target && !b.test(x+t)) {
        ans = min(ans, 1 + dfs(x+t, target, b));
    }
    return F[x] = ans;
}

int main() {
    int n;
    scanf("%d", &n);
    memset(F, -1, sizeof F);
    bitset<N> b(0);
    int ans = dfs(1, n, b) + 1;
    if (ans >= 1 << 30) ans = -1;
    printf("%d\n", ans);
}
0