結果

問題 No.262 面白くないビットすごろく
ユーザー koba-e964koba-e964
提出日時 2016-12-06 11:18:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,263 ms / 2,000 ms
コード長 1,367 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 100,020 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 18:40:06
合計ジャッジ時間 6,622 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1,254 ms
4,376 KB
testcase_02 AC 1,263 ms
4,376 KB
testcase_03 AC 1,256 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
const ll mod = 1e9 + 7;

const ll B = 1 << 25;

unordered_map<ll, PL> memo;

PL big_step(int bias, ll c) {
  assert (c < 41);
  ll code = bias * 64 + c;
  if (memo.count(code)) {
    return memo[code];
  }
  int cnt = 0;
  while (c < B) {
    c += __builtin_popcountll(c) + bias;
    cnt++;
  }
  memo[code] = PL(cnt, c);
  return PL(cnt, c);
}

int main(void){
  ll n;
  cin >> n;
  ll c = 1;
  ll cnt = 0;
  while (c < n) {
    PL result = big_step(__builtin_popcountll(c / B), c % B);
    ll newc = c / B * B + result.second;
    if (newc > n) {
      break;
    }
    cnt += result.first;
    c = newc;
  }
  while (c < n) {
    c += __builtin_popcountll(c);
    cnt++;
  }
  cout << (c == n ? cnt + 1 : -1) << endl;
}
0