結果

問題 No.2060 AND Sequence
ユーザー HIcoderHIcoder
提出日時 2023-07-18 23:23:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,059 bytes
コンパイル時間 1,260 ms
コンパイル使用メモリ 111,800 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 02:39:05
合計ジャッジ時間 3,261 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

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