結果

問題 No.3 ビットすごろく
ユーザー 久我山菜々久我山菜々
提出日時 2018-09-29 16:11:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 348 ms / 5,000 ms
コード長 682 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 82,660 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 01:07:46
合計ジャッジ時間 6,168 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 21 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 99 ms
4,380 KB
testcase_06 AC 25 ms
4,376 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 64 ms
4,380 KB
testcase_09 AC 163 ms
4,376 KB
testcase_10 AC 227 ms
4,380 KB
testcase_11 AC 135 ms
4,380 KB
testcase_12 AC 91 ms
4,380 KB
testcase_13 AC 15 ms
4,376 KB
testcase_14 AC 217 ms
4,384 KB
testcase_15 AC 336 ms
4,380 KB
testcase_16 AC 290 ms
4,376 KB
testcase_17 AC 327 ms
4,376 KB
testcase_18 AC 11 ms
4,380 KB
testcase_19 AC 348 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 226 ms
4,376 KB
testcase_23 AC 347 ms
4,380 KB
testcase_24 AC 345 ms
4,376 KB
testcase_25 AC 339 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 18 ms
4,376 KB
testcase_28 AC 277 ms
4,380 KB
testcase_29 AC 142 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 118 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

static int const INF{100000000};

int main(int, char**) {
  int N;
  std::cin >> N;
  std::vector<int> v(N + 1);
  for(unsigned int i{1}; i <= N; ++i) {
    v[i] = __builtin_popcount(i);
  }

  std::vector<int> dp(N + 1, INF);
  dp[1] = 1;

  for(int j{}; j < N + 1; ++j) {
  for(int i{1}; i < N + 1; ++i) {
    if(dp[i] == INF) continue;

    int front = i + v[i];
    if(front <= N) {
      dp[front] = std::min(dp[front], dp[i] + 1);
    }

    int back = i - v[i];
    if(back >= 0) {
      dp[back] = std::min(dp[back], dp[i] + 1);
    }
  }
  }

  std::cout << (dp[N] == INF ? -1 : dp[N]) << std::endl;
}
0