1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } using Pir=std::pair<ll,ll>; #define fir first #define sec second #define Mkr std::make_pair #define popcount(x) __builtin_popcountll(x) #define ctz(x) __builtin_ctzll(x) using Node=std::pair<ll,Pir>; const int MAXN=70,MAXM=1e5+10; const int Mod=998244353; template<class T1,class T2> inline void add(T1 &x,T2 y){ x=x+y>=Mod?x+y-Mod:x+y; } const int S=64; int Test,M,K; ll res[MAXM],N,ans; ll f[MAXN],g[MAXN],Dp[MAXN]; Node dpTree(ll x) { if(!x) return Mkr(1,Mkr(0,0)); if(x==1) return Mkr(M,Mkr(K,K)); ++x; ll cx=ctz(x); if(popcount(x)==1&&Dp[cx]) return Mkr(Dp[cx],Mkr(f[cx],g[cx])); --x; ll ri=1ll<<(S-__builtin_clzll(x));--ri; ll lx=(ri-1)>>1,rx=lx,ls=ri-x,mid=(ri+1)>>2; if(ls<=mid) rx-=ls; else rx-=mid,lx-=ls-mid; Node lp=dpTree(lx),rp=dpTree(rx),res; res.fir=lp.fir*rp.fir%Mod; res.sec.sec=(lp.sec.sec*rp.fir%Mod+rp.sec.sec*lp.fir%Mod+res.fir)%Mod*K%Mod; res.fir=res.fir*M%Mod; res.sec.fir=(lp.sec.sec*rp.sec.sec%Mod*K%Mod+res.sec.sec+ (lp.sec.fir*rp.fir%Mod+rp.sec.fir*lp.fir%Mod)%Mod*M%Mod)%Mod; ++x; if(popcount(x)==1) { f[ctz(x)]=res.sec.fir,g[ctz(x)]=res.sec.sec; Dp[ctz(x)]=res.fir; } --x; return res; } int main() { read(Test); while(Test--) { read(N,M); ll ans=0; for(K=1;K<=M;++K) { std::memset(f,0,sizeof(f)), std::memset(g,0,sizeof(g)); std::memset(Dp,0,sizeof(Dp)); res[K]=dpTree(N).sec.fir; } for(K=M;K>=1;--K) { add(res[K],Mod-res[K-1]); add(ans,res[K]*K%Mod); } write(ans,'\n'); } return 0; }
|