結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define Rep(i,N) for(int i = 0;i < N;i++)
int main(){
  int N;
  queue<int>que;
  que.push(1);
  cin >> N;
  int now;
  int mc[10001];
  memset(mc,-1,sizeof(mc));
  mc[1] = 1;
  while(!que.empty()){
    now = que.front();que.pop();
    if(now == N)break;
    int hosu = 0;
    for(int i = 0;1 << i <= now;i++)hosu += (now >> i) & 1;
    if(!(~mc[now - hosu])){
      mc[now - hosu] = mc[now] + 1;
      que.push(now - hosu);
    }
    if(!(~mc[now + hosu]) && now + hosu <= N){
      mc[now + hosu] = mc[now] + 1;
      que.push(now + hosu);
    }
  }
  cout << mc[N] << endl;
  return 0;
}
0