結果

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

ソースコード

diff #

#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){
               
                dp[P{m,1}]+=dp[P{m/2,1}]*N%MOD;
                dp[P{m,1}]%=MOD;
                dp[P{m,1}]+=dp[P{m/2,1}];
                dp[P{m,1}]%=MOD;
            }else{
                //mが偶数
                
                dp[P{m,1}]+=dp[P{m/2,0}]*N%MOD;
                dp[P{m,1}]%=MOD;
                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){
                
                dp[P{m,0}]+=dp[P{m/2,0}]*N%MOD;
                dp[P{m,0}]%=MOD;

                dp[P{m,0}]+=dp[P{m/2,1}];
                dp[P{m,0}]%=MOD;
            }else{
                //mが偶数
                dp[P{m,0}]+=dp[P{m/2,0}]*N%MOD;
                dp[P{m,0}]%=MOD;
                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