QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#217283#6323. Range NEQpengpeng_fudanTL 60ms5508kbC++145.2kb2023-10-16 18:05:362023-10-16 18:05:37

Judging History

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

  • [2023-10-16 18:05:37]
  • 评测
  • 测评结果:TL
  • 用时:60ms
  • 内存:5508kb
  • [2023-10-16 18:05:36]
  • 提交

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;
        ans.resize(sz),p.resize(sz);
        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(n*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_fst(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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3444kb

input:

2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

5 1

output:

44

result:

ok 1 number(s): "44"

Test #3:

score: 0
Accepted
time: 60ms
memory: 5508kb

input:

167 91

output:

284830080

result:

ok 1 number(s): "284830080"

Test #4:

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

input:

2 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

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

input:

2 3

output:

36

result:

ok 1 number(s): "36"

Test #6:

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

input:

2 4

output:

576

result:

ok 1 number(s): "576"

Test #7:

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

input:

3 1

output:

2

result:

ok 1 number(s): "2"

Test #8:

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

input:

3 2

output:

80

result:

ok 1 number(s): "80"

Test #9:

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

input:

3 3

output:

12096

result:

ok 1 number(s): "12096"

Test #10:

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

input:

3 4

output:

4783104

result:

ok 1 number(s): "4783104"

Test #11:

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

input:

4 1

output:

9

result:

ok 1 number(s): "9"

Test #12:

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

input:

4 2

output:

4752

result:

ok 1 number(s): "4752"

Test #13:

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

input:

4 3

output:

17927568

result:

ok 1 number(s): "17927568"

Test #14:

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

input:

4 4

output:

776703752

result:

ok 1 number(s): "776703752"

Test #15:

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

input:

5 2

output:

440192

result:

ok 1 number(s): "440192"

Test #16:

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

input:

5 3

output:

189125068

result:

ok 1 number(s): "189125068"

Test #17:

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

input:

5 4

output:

975434093

result:

ok 1 number(s): "975434093"

Test #18:

score: -100
Time Limit Exceeded

input:

1000 1000

output:


result: