結果

問題 No.3 ビットすごろく
ユーザー ferin
提出日時 2017-02-28 23:23:56
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,069 bytes
コンパイル時間 1,550 ms
コンパイル使用メモリ 162,324 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 08:33:56
合計ジャッジ時間 2,562 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MOD 1000000007
#define INF (1LL<<25)     //33554432
#define PI 3.14159265359
#define EPS 1e-12
//#define int ll

signed main(void)
{
  int n;
  cin >> n;

  int d[10010];
  REP(i, n+1) d[i] = INF;

  queue<int> que;
  que.push(1);
  d[1] = 1;

  while(que.size()) {
    int p = que.front(); que.pop();
    //cout << p << endl;
    if(p == n) break;
    int n1 = p + __builtin_popcount(p), n2 = p - __builtin_popcount(p);
    if(0 < n1 && n1 <= n && d[n1] == INF) {
      que.push(n1);
      d[n1] = d[p] + 1;
    }
    if(0 < n2 && n2 <= n && d[n2] == INF) {
      que.push(n2);
      d[n2] = d[p] + 1;
    }
  }

  //REP(i, n+1) cout << d[i] << " "; cout << endl;
  if(d[n] == INF) cout << -1 << endl;
  else cout << d[n] << endl;

  return 0;
}
0