結果

問題 No.3432 popcount & sum (Hard)
コンテスト
ユーザー meowjiao
提出日時 2026-09-30 18:16:34
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
+ 6µs
コード長 1,492 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,112 ms
コンパイル使用メモリ 158,952 KB
実行使用メモリ 9,904 KB
最終ジャッジ日時 2026-09-30 18:16:49
合計ジャッジ時間 5,704 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <cstring>
using namespace std;

const int mod = 998244353, inv2 = 499122177;

int pow2[70], dp[70][70][2];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    long long n;
    cin >> n;
    int m = 64 - __builtin_clzll((unsigned long long)n);
    pow2[0] = 1;
    for (int i = 1; i <= m + 1; i++) {
        pow2[i] = (pow2[i - 1] << 1) % mod;
    }
    int ans = 0;
    for (int i = 1; i <= m; i++) {
        memset(dp, 0, sizeof(dp));
        dp[m][0][0] = 1;
        for (int pos = m; pos >= 1; pos--) {
            for (int cnt = 0; cnt <= m; cnt++) {
                for (int flag = 0; flag < 2; flag++) {
                    if (!dp[pos][cnt][flag]) {
                        continue;
                    }
                    int g = flag ? 1 : (n >> (pos - 1)) & 1;
                    for (int j = 0; j <= g; j++) {
                        if (pos == i && (!j)) {
                            continue;
                        }
                        int nflag = flag || (j < g);
                        dp[pos - 1][cnt + j][nflag] = ((long long)dp[pos - 1][cnt + j][nflag] + dp[pos][cnt][flag]) % mod;
                    }
                }
            }
        }
        for (int t = 1; t <= m; t++) {
            long long c = (dp[0][t][0] + dp[0][t][1]) % mod, k = c * ((c + 1) % mod) % mod * inv2 % mod;
            ans = (ans + pow2[i - 1] * k % mod) % mod;
        }
    }
    cout << ans;
    cout.flush();
    return 0;
}
0