結果

問題 No.2060 AND Sequence
ユーザー HIcoder
提出日時 2023-07-19 00:06:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 3,122 bytes
コンパイル時間 1,066 ms
コンパイル使用メモリ 107,092 KB
最終ジャッジ日時 2025-02-15 15:41:08
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

//AC
/*

*/
#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=998244353;
const double PI=acos(-1);


ll mod_pow(ll x,ll y,ll mod){
    ll res=1;
    while(y>0){
        if(y&1){
            res*=x;
            res%=mod;
        }
        x*=x;
        x%=mod;
        y/=2;
    }
    return res;
}

int main(){
    int N,M;
    cin>>N>>M;

    /*
    ll ans=0;
    for(int x=0;x<=M;x++){//Mが大きいのでこれでは無理
        int c=__builtin_popcount(x);
        ans+=mod_pow(N,c,MOD);
        ans%=MOD;
    }
    cout<<ans<<endl;
    */

    map<P,ll> dp;

    auto dfs=[&](auto f,int m,int i)->void{

        
        if(dp.count(P{m,i})) return;
        if(m==0){
            dp[P{m,i}]=i;
            return;            
        }



        f(f,m/2,0);
        f(f,m/2,1);

        if(i==1){
            
            if(m%2==1){
                //A[0]~A[N-1]のうち,すべてを2倍, A[0]~A[N-1]でどこかのタイミングA[i]以降,2^0の位に1をつける(i=0~N-1のNとおり?)
                dp[P{m,1}]+=dp[P{m/2,1}]*N%MOD;
                dp[P{m,1}]%=MOD;

                //A[0]~A[N-1]のうち,すべてを2倍, すべてに1を足す?  i=0をダブってカウント?
                dp[P{m,1}]+=dp[P{m/2,1}];
                dp[P{m,1}]%=MOD;
            }else{
                //mが偶数
                //A[0]~A[N-1]のうち,すべてを2倍, A[0]~A[N-1]でどこかのタイミングA[i]以降,2^0の位に1をつける
                dp[P{m,1}]+=dp[P{m/2,0}]*N%MOD;
                dp[P{m,1}]%=MOD;

                //A[0]~A[N-1]のうち,すべてを2倍するだけ
                dp[P{m,1}]+=dp[P{m/2,1}];
                dp[P{m,1}]%=MOD;
            }
        }else{
            f(f,m/2,1);
            f(f,m/2,0);

            if(m%2==1){
                //A[0]~A[N-1]のうち,すべてを2倍, A[0]~A[N-1]でどこかのタイミングA[i]以降,2^0の位に1をつける(i=0~N-1のNとおり?)
                
                //i=N-1のときにダブってカウント?
                dp[P{m,0}]+=dp[P{m/2,0}]*N%MOD;
                dp[P{m,0}]%=MOD;

                //m/2 A[0]~A[N-1]のうち,すべてを2倍する.そのあとですべてのA[i]に1を足す
                dp[P{m,0}]+=dp[P{m/2,1}];
                dp[P{m,0}]%=MOD;
            }else{
                //mが偶数

                //m/2 A[0]~A[N-1]のうち,すべてを2倍する.そのあとであるiを選び,j=[0,i] i<=N-2を満たすA[j]に1を足す

                //i=0のときにダブってカウントする?
                dp[P{m,0}]+=dp[P{m/2,0}]*N%MOD;
                dp[P{m,0}]%=MOD;

                //m/2 A[0]~A[N-1]のうち,すべてを2倍する.
                dp[P{m,0}]+=dp[P{m/2,0}];
                dp[P{m,0}]%=MOD;
            }
        }

        return;
    };

    dfs(dfs,M,1);
    ll ans=dp[P{M,1}];
    cout<<ans<<endl;

}
0