結果

問題 No.2060 AND Sequence
ユーザー srjywrdnprkt
提出日時 2025-03-29 07:57:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,392 bytes
コンパイル時間 3,397 ms
コンパイル使用メモリ 278,008 KB
実行使用メモリ 7,328 KB
最終ジャッジ日時 2025-03-29 07:57:18
合計ジャッジ時間 4,883 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>

using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    /*
       A_Nを固定する。
       k=N, N-1, ..., 1と見ていった時、0->1となっていてはいけない。
       つまり、A_Nのpopcountをkとすると、
       A_0=0, A_1, ..., A_N-1のN箇所のどこかでそのbitが1になる。
       よって、(M以下でpopcountがkになるもの)*N^kの和が答え。
       bit DPでやる。
    */

    ll N, M;
    cin >> N >> M;
    ll popsame=0;
    vector<mint> dp(31);
    for (ll i=30; i>=0; i--){
        ll c = (M>>i) & 1;
        popsame += c;
        vector<mint> pd(31);
        if (c){
            //same->small
            pd[popsame-1] += 1;
            //small->small
            for (int j=0; j<=30; j++){
                pd[j] += dp[j];
                if (j) pd[j] += dp[j-1];
            }
        }
        else{
            //small->small
            for (int j=0; j<=30; j++){
                pd[j] += dp[j];
                if (j) pd[j] += dp[j-1];
            }
        }
        swap(dp, pd);
    }
    dp[popsame]++;

    mint ans=0, pw=1;
    for (int i=0; i<=30; i++){
        ans += dp[i] * pw;
        pw *= N;
    }
    cout << ans.val() << endl;

    return 0;
}
0