結果

問題 No.3 ビットすごろく
コンテスト
ユーザー alpha_virginis
提出日時 2015-06-18 22:18:16
言語 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
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 759 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,270 ms
コンパイル使用メモリ 177,712 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2026-03-22 03:31:09
合計ジャッジ時間 1,921 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

int ans[16384];
int dist[16384];
int N;

void solve(int p, int depth) {
  if( not ( 1 <= p and p <= N ) ) {
    return;
  }
  if( not ( ans[p] > depth ) ) {
    return;
  }
  
  ans[p] = depth;
  
  solve(p+dist[p], depth+1);
  solve(p-dist[p], depth+1);

  return;
}



int main() {

  int count;
  int temp;

  std::cin >> N;

  for(int i = 1; i <= N; ++i) {
    ans[i] = 10000;
  }
  
  for(int i = 1; i <= N; ++i) {
    temp = i;
    count = 0;
    while( temp != 0 ) {
      if( (temp & 0x01) != 0 ) {
	count += 1;
      }
      temp >>= 1;
    }
    dist[i] = count;
  }
    
  solve(1, 1);

  if( ans[N] != 10000 ) {  
    std::cout << ans[N] << std::endl;
  }
  else {
    std::cout << "-1" << std::endl;
  }

  return 0;
}
	
0