QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#217288#6323. Range NEQpengpeng_fudanAC ✓692ms58428kbC++145.2kb2023-10-16 18:16:182023-10-16 18:16:20

Judging History

你现在查看的是最新测评结果

  • [2023-10-16 18:16:20]
  • 评测
  • 测评结果:AC
  • 用时:692ms
  • 内存:58428kb
  • [2023-10-16 18:16:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
using ll=long long;
using poly=vector<ll>;
const ll mod=998244353;
inline ll qpow(ll a,ll b,ll mod){
    ll ans=1;a%=mod;b%=(mod-1);
    while(b){
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

inline poly operator+(poly f,poly g){
    int sz=max(f.size(),g.size());
    f.resize(sz),g.resize(sz);
    poly q(sz);
    for(int i=0;i<sz;i++)   q[i]=(f[i]+g[i])%mod;
    return q;
}
inline poly operator-(poly f,poly g){
    int sz=max(f.size(),g.size());
    f.resize(sz),g.resize(sz);
    poly q(sz);
    for(int i=0;i<sz;i++)   q[i]=(f[i]-g[i]+mod)%mod;
    return q;
}

vector<int> rev;
void NTT(poly& f,int ope){
    for(int i = 0; i < f.size(); ++i) if(i < rev[i]) swap(f[i], f[rev[i]]);
    for(int mid = 1; mid < f.size(); mid <<= 1){
        ll gn = qpow(ope==1?3:332748118, (mod-1)/(mid<<1), mod);
        for(int j = 0; j < f.size(); j += (mid<<1)){
            ll w = 1;
            for(int k = 0; k < mid; ++k, w = w * gn % mod){
                ll x = f[j+k], y = w * f[j+k+mid] % mod;
                f[j+k] = (x+y) % mod;
                f[j+k+mid] = (x-y+mod) % mod;
            }
        }
    }
    if(ope == -1){
        ll deginv = qpow(f.size(), mod-2, mod);
        for(int i = 0; i < f.size(); ++i) f[i] = f[i] * deginv % mod;
    }
}
inline poly poly_linear(poly f,poly g,int len,function<ll(ll,ll)> func){
    int c = 0; for(int t = 1; t < len; t <<= 1) ++c; 
    int deg = (1 << c); rev.resize(deg); rev[0] = 0;
    for(int i = 1; i < deg; ++i) rev[i] = (rev[i>>1]>>1)+((i&1)<<(c-1));
    f.resize(deg); g.resize(deg); NTT(f, 1); NTT(g, 1);
    for(int i = 0; i < deg; ++i) f[i] = func(f[i], g[i]);
    NTT(f, -1); f.resize(len);
    return f;
}
inline poly operator*(poly f,poly g){
    auto func=[](ll a,ll b){return a*b%mod;};
    int len=f.size()+g.size()-1;
    return poly_linear(f,g,len,func);
}
inline poly subpoly(poly& f, int pos, int count){
    poly ret(f.begin()+min(pos,(int)f.size()),f.begin()+min(pos+count,(int)f.size()));
    ret.resize(count); 
    return ret;
}

inline poly poly_inv(poly& f){//
    poly g(1); g[0] = qpow(f[0], mod-2, mod);
    auto func = [](ll a, ll b){return (2ll-a*b%mod+mod)*b%mod;};
    int n = f.size();
    for(int deg = 1; deg < n; deg <<= 1)
    {
        g = poly_linear(subpoly(f, 0, deg*2), g, deg*4, func);
        g.resize(deg*2);
    }
    g.resize(n);
    return g;
}

poly swap(poly f){
    int sz=f.size();
    poly p(sz);
    for(int i=0;i<sz;i++)   p[sz-i-1]=f[i];
    return p;
}
inline void poly_mod(poly f,poly g,poly& q,poly& md){
    int n=f.size(),m=g.size();
    if(m>n){q.clear();md=f;return ;}
    poly f1=swap(f);poly g1=swap(g);
    g1.resize(n-m+1),f1.resize(n-m+1);
    q=f1*poly_inv(g1);q.resize(n-m+1);
    q=swap(q);
    md=f-g*q;
    md.resize(m-1);
}
inline poly poly_qpow(poly& f,int k){//较慢
    int sz=f.size();
    poly ans(sz);ans[0]=1;
    poly p=f;
    while(k){
        if(k&1) ans=ans*p;
        p=p*p;
        k>>=1;
    }
    return ans;
}
inline poly d(poly& a,int ope){
    int sz=a.size();
    poly ans(sz);
    if(ope==-1){
        for(int i=0;i<sz-1;i++)   ans[i]=a[i+1]*(i+1)%mod;
        ans[sz-1]=0;
    }
    else{
        for(int i=1;i<=sz-1;i++)   ans[i]=a[i-1]*qpow(i,mod-2,mod)%mod;
        ans[0]=0;
    }
    return ans;
}
inline poly ln(poly& a){//a[0]=1;
    int sz=a.size();
    poly p=d(a,-1);
    poly f=poly_inv(a);
    p=p*f;p.resize(sz);
    p=d(p,1);
    return p;
}
inline poly exp(poly& a){//a[0]=0;
    int sz=a.size();
    poly g(1);
    g[0]=1;
    poly f,q;
    for(int i=1;i<sz;i<<=1){
        g.resize(i<<1);
        f=poly_inv(g);
        q=ln(g);
        for(int j=0;j<(i<<1);j++)   q[j]=((-q[j]+a[j])%mod+mod)%mod;
        q[0]=(q[0]+1)%mod;
        g=g*q;g.resize(i<<1);
    }
    g.resize(sz);
    return g;
}
inline poly poly_qpow_fst(poly& f,ll k){
    int l=0,sz=f.size();
    while(l<sz&&f[l]==0)    l++;
    if(l==sz)   return poly(sz);
    ll q=f[l];
    poly p=subpoly(f,l,sz-l);
    for(int i=0;i<p.size();i++)   p[i]=p[i]*qpow(q,mod-2,mod)%mod;
    p=ln(p);
    for(int i=0;i<p.size();i++)   p[i]=p[i]*k%mod;
    p=exp(p);
    if(1ll*l*k>=sz)  return poly(sz);
    q=qpow(q,k,mod);
    poly ans(sz);
    for(int i=l*k;i<sz;i++) ans[i]=p[i-l*k]*q%mod;
    return ans;
}
ll pz[1010];
ll invp[1010];
ll get_c(int x,int y){
    return pz[x]*invp[y]%mod*invp[x-y]%mod;
}
poly f;
void solve() {
    int n,m;
    cin>>n>>m;
    f.resize(m+1);
    for(int i=0;i<=m;i++)   f[i]=get_c(m,i)*get_c(m,i)%mod*pz[i]%mod*(i&1?-1:1);
    poly g=poly_qpow(f,n);
    ll ans=0;
    ll zp=1;
    for(int i=n*m;i>=0;i--){
        ans=(ans+g[i]*zp%mod)%mod;
        zp=zp*(n*m-i+1)%mod;
    }
    ans=(ans%mod+mod)%mod;
    cout<<ans<<'\n';
}
int main() {
    ios::sync_with_stdio(0),cin.tie(0);
    int _ = 1;
    //cin >> _;
    pz[0]=1;
    for(int i=1;i<=1000;i++) pz[i]=pz[i-1]*i%mod;
    invp[1000]=qpow(pz[1000],mod-2,mod);invp[0]=1;
    for(int i=1000-1;i>=1;i--)  invp[i]=invp[i+1]*(i+1)%mod;  
    while(_--) solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3500kb

input:

2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3560kb

input:

5 1

output:

44

result:

ok 1 number(s): "44"

Test #3:

score: 0
Accepted
time: 11ms
memory: 4520kb

input:

167 91

output:

284830080

result:

ok 1 number(s): "284830080"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3496kb

input:

2 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3508kb

input:

2 3

output:

36

result:

ok 1 number(s): "36"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3448kb

input:

2 4

output:

576

result:

ok 1 number(s): "576"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3492kb

input:

3 1

output:

2

result:

ok 1 number(s): "2"

Test #8:

score: 0
Accepted
time: 1ms
memory: 3440kb

input:

3 2

output:

80

result:

ok 1 number(s): "80"

Test #9:

score: 0
Accepted
time: 1ms
memory: 3488kb

input:

3 3

output:

12096

result:

ok 1 number(s): "12096"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

3 4

output:

4783104

result:

ok 1 number(s): "4783104"

Test #11:

score: 0
Accepted
time: 0ms
memory: 3492kb

input:

4 1

output:

9

result:

ok 1 number(s): "9"

Test #12:

score: 0
Accepted
time: 0ms
memory: 3528kb

input:

4 2

output:

4752

result:

ok 1 number(s): "4752"

Test #13:

score: 0
Accepted
time: 1ms
memory: 3448kb

input:

4 3

output:

17927568

result:

ok 1 number(s): "17927568"

Test #14:

score: 0
Accepted
time: 1ms
memory: 3492kb

input:

4 4

output:

776703752

result:

ok 1 number(s): "776703752"

Test #15:

score: 0
Accepted
time: 0ms
memory: 3456kb

input:

5 2

output:

440192

result:

ok 1 number(s): "440192"

Test #16:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

5 3

output:

189125068

result:

ok 1 number(s): "189125068"

Test #17:

score: 0
Accepted
time: 0ms
memory: 3452kb

input:

5 4

output:

975434093

result:

ok 1 number(s): "975434093"

Test #18:

score: 0
Accepted
time: 672ms
memory: 56620kb

input:

1000 1000

output:

720037464

result:

ok 1 number(s): "720037464"

Test #19:

score: 0
Accepted
time: 3ms
memory: 3564kb

input:

72 42

output:

638177567

result:

ok 1 number(s): "638177567"

Test #20:

score: 0
Accepted
time: 1ms
memory: 3492kb

input:

15 19

output:

663050288

result:

ok 1 number(s): "663050288"

Test #21:

score: 0
Accepted
time: 5ms
memory: 3860kb

input:

68 89

output:

94365047

result:

ok 1 number(s): "94365047"

Test #22:

score: 0
Accepted
time: 3ms
memory: 3644kb

input:

92 37

output:

652605307

result:

ok 1 number(s): "652605307"

Test #23:

score: 0
Accepted
time: 4ms
memory: 3708kb

input:

61 87

output:

498277867

result:

ok 1 number(s): "498277867"

Test #24:

score: 0
Accepted
time: 3ms
memory: 3616kb

input:

81 40

output:

133095344

result:

ok 1 number(s): "133095344"

Test #25:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

7 91

output:

524164813

result:

ok 1 number(s): "524164813"

Test #26:

score: 0
Accepted
time: 1ms
memory: 3576kb

input:

31 18

output:

361233485

result:

ok 1 number(s): "361233485"

Test #27:

score: 0
Accepted
time: 3ms
memory: 3632kb

input:

74 54

output:

500686087

result:

ok 1 number(s): "500686087"

Test #28:

score: 0
Accepted
time: 1ms
memory: 3472kb

input:

32 2

output:

586504335

result:

ok 1 number(s): "586504335"

Test #29:

score: 0
Accepted
time: 444ms
memory: 46572kb

input:

656 718

output:

346764298

result:

ok 1 number(s): "346764298"

Test #30:

score: 0
Accepted
time: 145ms
memory: 14700kb

input:

254 689

output:

358078813

result:

ok 1 number(s): "358078813"

Test #31:

score: 0
Accepted
time: 464ms
memory: 46024kb

input:

713 674

output:

914437613

result:

ok 1 number(s): "914437613"

Test #32:

score: 0
Accepted
time: 96ms
memory: 14120kb

input:

136 698

output:

56687290

result:

ok 1 number(s): "56687290"

Test #33:

score: 0
Accepted
time: 135ms
memory: 15268kb

input:

369 401

output:

312325811

result:

ok 1 number(s): "312325811"

Test #34:

score: 0
Accepted
time: 49ms
memory: 8732kb

input:

280 204

output:

280012063

result:

ok 1 number(s): "280012063"

Test #35:

score: 0
Accepted
time: 141ms
memory: 15028kb

input:

904 225

output:

162909174

result:

ok 1 number(s): "162909174"

Test #36:

score: 0
Accepted
time: 659ms
memory: 53276kb

input:

855 928

output:

39885159

result:

ok 1 number(s): "39885159"

Test #37:

score: 0
Accepted
time: 154ms
memory: 15364kb

input:

503 365

output:

745115888

result:

ok 1 number(s): "745115888"

Test #38:

score: 0
Accepted
time: 575ms
memory: 55472kb

input:

646 996

output:

610925577

result:

ok 1 number(s): "610925577"

Test #39:

score: 0
Accepted
time: 685ms
memory: 56944kb

input:

990 918

output:

203469632

result:

ok 1 number(s): "203469632"

Test #40:

score: 0
Accepted
time: 676ms
memory: 57304kb

input:

961 949

output:

169566857

result:

ok 1 number(s): "169566857"

Test #41:

score: 0
Accepted
time: 675ms
memory: 57480kb

input:

946 932

output:

352423195

result:

ok 1 number(s): "352423195"

Test #42:

score: 0
Accepted
time: 661ms
memory: 54276kb

input:

903 981

output:

196309824

result:

ok 1 number(s): "196309824"

Test #43:

score: 0
Accepted
time: 676ms
memory: 54876kb

input:

916 988

output:

487208972

result:

ok 1 number(s): "487208972"

Test #44:

score: 0
Accepted
time: 686ms
memory: 58428kb

input:

982 982

output:

387421488

result:

ok 1 number(s): "387421488"

Test #45:

score: 0
Accepted
time: 689ms
memory: 53432kb

input:

955 911

output:

955637031

result:

ok 1 number(s): "955637031"

Test #46:

score: 0
Accepted
time: 638ms
memory: 56988kb

input:

906 999

output:

798469943

result:

ok 1 number(s): "798469943"

Test #47:

score: 0
Accepted
time: 692ms
memory: 57984kb

input:

982 975

output:

193506289

result:

ok 1 number(s): "193506289"

Test #48:

score: 0
Accepted
time: 657ms
memory: 56548kb

input:

921 991

output:

431202149

result:

ok 1 number(s): "431202149"