結果

問題 No.3432 popcount & sum (Hard)
コンテスト
ユーザー harel
提出日時 2026-01-07 14:19:19
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,528 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,104 ms
コンパイル使用メモリ 335,928 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-01-11 13:09:49
合計ジャッジ時間 3,645 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int mod = 998244353;

void solve() {
    ll n;
    cin >> n;

    ll ans = 0;

    ll temp = n;
    int l = 0;
    while (temp > 0) {
        temp /= 2;
        l++;
    }
    
    ll pow2[64];
    pow2[0] = 1;
    for (int i = 0; i < 63; i++) {
        pow2[i+1] = (pow2[i]*2)%mod;
    }


    ll dp[l+1][l+1][2];
    
    for (int i = 0; i < l; i++) {
        memset(dp,0,sizeof(dp));
        dp[l][0][0] = 1;

        for (int pos = l; pos > 0; pos--) {
            int bt = (n >> (pos - 1)) & 1;
            for (int one = 0; one <= l; one++) {
                for (int is_small = 0; is_small < 2; is_small++) {
                    int can;
                    if (is_small == 0) can = bt;
                    else can = 1;

                    for (int b = 0; b <= can; b++) {
                        if (pos == i+1 && b == 0) continue;
                        int next_small = (is_small || b < can);
                        dp[pos-1][one + b][next_small] += dp[pos][one][is_small];
                        dp[pos-1][one + b][next_small] %= mod;
                    }
                }
            }
        }


        for (int t = 0; t <= l; t++) {
            ll c_ti = (dp[0][t][0] + dp[0][t][1])%mod;
            ll x = (c_ti*(c_ti+1)/2)%mod;
            ans += (pow2[i]*x)%mod;

            ans %= mod;
        }
    }

    cout << ans << ' ';

}



int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    solve();
	return 0;
}
0