結果

問題 No.3 ビットすごろく
ユーザー munromunro
提出日時 2018-06-24 02:00:12
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,386 bytes
コンパイル時間 1,349 ms
コンパイル使用メモリ 148,064 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-13 08:39:08
合計ジャッジ時間 3,350 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1 ms
4,380 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
4,376 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<bitset>
#include<bits/stdc++.h>
using namespace std;
const int INTMAX = numeric_limits<int>::max();

unsigned long bitcount(int bits){
  bits = (bits & 0x55555555) + ((bits >> 1) & 0x55555555);
  //cout << "[bits]" << bits << endl;
  bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
  //cout << "[bits]" << bits << endl;
  bits = (bits & 0x0f0f0f0f) + ((bits >> 4) & 0x0f0f0f0f);
  //cout << "[bits]" << bits << endl;
  bits = (bits & 0x00ff00ff) + ((bits >> 4) & 0x00ff00ff);
  //cout << "[bits]" << bits << endl;
  bits = (bits & 0x0000ffff) + ((bits >> 4) & 0x0000ffff);
  //cout << "[bits]" << bits << endl;
  return bits;
}

int main() {

  int N;
  cin >> N;
  queue<int> q;
  q.push(1);
  vector<int> dist( N + 1, INTMAX );
  dist[1] = 1;
  //cout << "[1]" << dist[1] << endl;

  while(!q.empty()){
    int now = q.front();
    int bit = bitcount(now);
    q.pop();
    if(now - bit > 0 && dist[now - bit] == INTMAX){
      dist[now - bit] = dist[now] + 1;
      q.emplace(now - bit);
      //cout << "[" << dist[now] + 1 << "]" <<now - bit << endl;
    }
    if(now + bit <= N && dist[now + bit] == INTMAX){
      dist[now + bit] = dist[now] + 1;
      q.emplace(now + bit);
      //cout << "[" << dist[now] + 1 << "]" <<now + bit << endl;
    }
  }

  if(dist[N] != INTMAX){
    cout << dist[N] <<endl;
  }else{
    cout << -1 << endl;
  }
}
0