結果

問題 No.262 面白くないビットすごろく
ユーザー Min_25Min_25
提出日時 2015-08-01 10:04:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,748 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 75,212 KB
実行使用メモリ 50,952 KB
最終ジャッジ日時 2023-09-25 00:04:32
合計ジャッジ時間 1,162 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
50,820 KB
testcase_01 AC 36 ms
50,820 KB
testcase_02 AC 37 ms
50,884 KB
testcase_03 AC 36 ms
50,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cassert>

#include <iostream>
#include <utility>
#include <algorithm>
#include <queue>
#include <functional>
#include <vector>
#include <map>
#include <set>

#define getchar getchar_unlocked
#define putchar putchar_unlocked

using namespace std;

typedef long long int64;
typedef long long unsigned uint64;
typedef long double float80;
typedef unsigned short uint16;
typedef unsigned uint;
typedef unsigned char uint8;

const uint pre = 18;
const uint pre_total = 1u << pre;

uint nsteps[41 - pre][pre_total];
uint nexts[41 - pre][pre_total];
uint pops[1u << 16];

inline uint pop_count(uint n) {
  return pops[n & 65535] + pops[n >> 16];
}

void solve() {
  // @FF256grhy さんの解法

  for (uint i = 0; i < 16; ++i) {
    uint beg = 1 << i;
    uint end = beg << 1;
    for (uint j = beg; j < end; ++j) {
      pops[j] = pops[j - beg] + 1;
    }
  }

  for (int i = 40 - pre; i >= 0; --i) {
    for (int j = pre_total - 1; j >= 0; --j) {
      uint k = j + i + pop_count(j);
      if (k >= pre_total) {
        nsteps[i][j] = 1;
        nexts[i][j] = k - pre_total;
      } else {
        nsteps[i][j] = nsteps[i][k] + 1;
        nexts[i][j] = nexts[i][k];
      }
    }
  }

  uint64 n;
  while (~scanf("%llu", &n)) {
    uint hi = n >> pre;
    uint lo = n & (pre_total - 1);

    uint l = 1;
    uint64 nstep = 1;
    for (uint i = 0; i < hi; ++i) {
      uint pc = pop_count(i);
      nstep += nsteps[pc][l];
      l = nexts[pc][l];
    }
    uint pc_hi = pop_count(hi);
    while (l < lo) {
      l += pc_hi + pop_count(l);
      nstep += 1;
    }
    printf("%lld\n", l == lo ? nstep : -1);
  }
}

int main() {
  solve();
  return 0;
}
0