結果

問題 No.3 ビットすごろく
ユーザー tadanoningentadanoningen
提出日時 2020-10-12 15:14:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 537 ms / 5,000 ms
コード長 883 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 76,112 KB
実行使用メモリ 6,008 KB
最終ジャッジ日時 2023-09-14 01:52:19
合計ジャッジ時間 6,243 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 14 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 77 ms
4,380 KB
testcase_10 AC 207 ms
4,380 KB
testcase_11 AC 48 ms
4,376 KB
testcase_12 AC 12 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 174 ms
4,380 KB
testcase_15 AC 461 ms
5,112 KB
testcase_16 AC 266 ms
4,376 KB
testcase_17 AC 413 ms
5,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 534 ms
5,560 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 184 ms
4,380 KB
testcase_23 AC 536 ms
6,008 KB
testcase_24 AC 537 ms
5,992 KB
testcase_25 AC 465 ms
5,172 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 250 ms
4,376 KB
testcase_29 AC 52 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 31 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<queue>
#define ll long long

using namespace std;
typedef pair<int,int> P;

int bitcnt(int n){
  int bit = 0;
  while(n > 0){
    if(n & 1){
      bit++;
    }
    n >>= 1;
  }
  return bit;
}

int main(){
    int n;
    cin >> n;
    queue<P> que;
    que.push(P(1,1));
    vector<bool> used(n+1,false);
    
    while(!que.empty()){
      int now = que.front().first;
      int cost = que.front().second;      
      que.pop();
      
      used[now] = true;
      
      if(now == n){
        cout << cost << endl;
        return 0;
      }
      
      int next = now + bitcnt(now);
      int prev = now - bitcnt(now);
      
      if(next > 0 && next <= n && !used[next]){
        que.push(P(next,cost+1));
      }
      if(prev > 0 && prev <= n && !used[prev]){
        que.push(P(prev,cost+1));
      }
    }
    cout << -1 << endl;
    return 0;
}
0