結果

問題 No.262 面白くないビットすごろく
ユーザー OIer1048576
提出日時 2024-06-22 18:16:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 448 ms / 2,000 ms
コード長 1,098 bytes
コンパイル時間 833 ms
コンパイル使用メモリ 73,052 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-22 18:16:36
合計ジャッジ時間 3,375 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bitset>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int64_t pcnt(int64_t x) { return __builtin_popcountll(x); }

const int64_t trunc = 20, lt = 1 << trunc;

int main() {
    int64_t n;
    cin >> n;
    auto nxt = vector<vector<int64_t>>(trunc + 1, vector<int64_t>(3 * trunc)),
         step = vector<vector<int64_t>>(trunc + 1, vector<int64_t>(3 * trunc));
    // (offset, num)
    for (int64_t i = 0; i <= trunc; i++) {
        for (int64_t j = !i; j < 3 * trunc; j++) {
            int64_t j_ans = j, c = 0;
            for (; j_ans < lt; c++, j_ans += pcnt(j_ans) + i)
                ;
            nxt[i][j] = j_ans;
            step[i][j] = c;
        }
    }
    int64_t ans = 1, i;
    for (i = 1; nxt[pcnt(i >> trunc)][i & (lt - 1)] + lt * (i >> trunc) <= n;
         i = nxt[pcnt(i >> trunc)][i & (lt - 1)] + lt * ((i >> trunc))) {
        ans += step[pcnt(i >> trunc)][i & (lt - 1)];
    }
    for (; i < n; i += pcnt(i)) {
        ans++;
    }
    if (i == n)
        cout << ans << endl;
    else
        cout << -1 << endl;
    return 0;
}
0