QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#578381#6323. Range NEQAKALemon#AC ✓196ms87072kbC++234.2kb2024-09-20 18:45:082024-09-20 18:45:09

Judging History

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

  • [2024-09-20 18:45:09]
  • 评测
  • 测评结果:AC
  • 用时:196ms
  • 内存:87072kb
  • [2024-09-20 18:45:08]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using db = double;
constexpr int N = 4e6 + 50, LOGN = 30;
constexpr i64 P = 998244353;
typedef long long ll;
ll pos[N];
const ll mod = 998244353, root = 3;
inline ll fastpow(ll a, ll b) {
    ll ans = 1;
    for (;b;a=a*a%mod,b>>=1) if(b&1)ans=ans*a%mod;
    return ans;
}
inline void exgcd(ll a,ll b,ll &g,ll &x,ll &y) {
    if (!b) g=a,x=1,y=0;
    else exgcd(b,a%b,g,y,x),y-=x*(a/b);
}
inline ll inv(ll a) {
   ll g,x,y;
    exgcd(a,mod,g,x,y);
    return (x%mod+mod)%mod;
}
void init(const int &n) {
    for (int i = 0,j=0; i < n; ++i) {
        pos[i]=j;for (int l = n >> 1; (j ^= l) < l; l >>= 1);
    }
}
void transform(ll *a, const int &n, bool inverse) {
    for (int i=0; i<n;++i) if(i>pos[i]) swap(a[i],a[pos[i]]);
    for (int l=2; l<=n;l<<=1) {
        int m=l/2;ll omega=fastpow(inverse?inv(root):root,(mod-1)/l);
        for (ll *p=a;p!=a+n;p+=l) {
            ll x=1;
            for (int i=0;i<m;++i,x=x*omega%mod) {
                ll t=x*p[m+i]%mod;
                p[m+i]=(p[i]-t+mod)%mod;(p[i]+=t)%=mod;
            }
        }
    }
}
void dft(ll *a, const int &n) {
    transform(a,n,0); 
}
void idft(ll *a, const int &n) {
    const ll INV=inv(n);
    transform(a,n,1);
    for (int i=0;i<n;i++) a[i]=a[i]*INV % mod;
}
const int maxn=4e6+6;
ll a[maxn],b[maxn],c[maxn],pic[maxn],pec[maxn],plc[maxn],ppc[maxn];
int INV[maxn];
void poly_inv(int n,ll *a,ll *b) {
    if(n==1) {b[0]=inv(a[0]);return;}
    poly_inv((n+1)/2,a,b);
    int cnt=1;while(cnt<=n*2) cnt<<=1;init(cnt);
    copy(a,a+n,pic);fill(pic+n,pic+cnt,0);fill(b+n,b+cnt,0);dft(pic,cnt);dft(b,cnt);
    for(int i=0;i<cnt;i++) (b[i]*=(2ll-pic[i]*b[i])%mod)%=mod;
    for(int i=0;i<cnt;i++) b[i]=(b[i]+mod)%mod;
    idft(b,cnt);fill(b+n,b+cnt,0);
}
void poly_ln(int n,ll *a,ll *b) { //G'=F'/F
    poly_inv(n,a,b);
    int cnt=1;while(cnt<=n*2-3) cnt<<=1;init(cnt);
    for(int i=0;i<n-1;i++) plc[i]=a[i+1]*(i+1)%mod;
    fill(plc+n-1,plc+cnt,0);fill(b+n,b+cnt,0);dft(plc,cnt);dft(b,cnt);
    for(int i=0;i<cnt;i++) b[i]=plc[i]*b[i]%mod;
    idft(b,cnt);for(int i=n-1;i>=1;i--) b[i]=b[i-1]*INV[i]%mod;b[0]=0;
    fill(b+n,b+cnt,0);
}
void poly_exp(int n,ll *a,ll *b) {
    if(n==1) {b[0]=1;return;}
    poly_exp((n+1)/2,a,b);poly_ln(n,b,pec);
    int cnt=1;while(cnt<=n*2-2) cnt<<=1;init(cnt);
    for(int i=0;i<n;i++) pec[i]=(-pec[i]+a[i])%mod;pec[0]+=1;
    dft(pec,cnt);dft(b,cnt);
    for(int i=0;i<cnt;i++) b[i]=(b[i]*pec[i])%mod;
    idft(b,cnt);fill(b+n,b+cnt,0);
}
void poly_pow(int n,ll *a,ll *b,ll k) {
    poly_ln(n,a,ppc);
    for(int i=0;i<n;i++) ppc[i]*=k;
    poly_exp(n,ppc,b);
}

i64 fac[N], invfac[N];
i64 C(int n, int m) {
    if (n < m) return 0;
    if (m < 0) return 0;
    return fac[n] * invfac[m] % P * invfac[n - m] % P;
}
void init() {
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = fac[i - 1] * i % P;
    invfac[N - 1] = fastpow(fac[N - 1], P - 2);
    for (int i = N - 1; i > 0; i--) invfac[i - 1] = invfac[i] * i % P;
}

void solve(){
    int n, m; cin >> n >> m;
    init();
    int limit = 1;
    while (limit < n * m + 1) limit *= 2; 
    init(limit);
    for (int i = 0; i <= m; i++) {
        a[i] = C(m, i) * C(m, i) % P * fac[i] % P;
    }
    dft(a, limit);
    for (int i = 0; i <= limit; i++) {
        a[i] = fastpow(a[i], n);
    }
    idft(a, limit);
    // poly_pow(limit, a, b, n);
    // for (int i = 0; i <= n * m; i++) cout << a[i] << ' ';
    // auto g = Poly(f).pow(n,)
    // int sz = 1;
    // while (n * m + 1 > sz) sz *= 2;
    // f.resize(sz);
    // dft(f);
    // for (int i = 0; i <= n * m + 1; i++) f[i] = power(f[i], n);
    // idft(f);
    // for (int i = 0; i <= n * m + 1; i++) cout << f[i] << ' ';

    i64 ans = 0;
    for (int i = 0; i <= n * m; i++) {
        i64 sign = (i % 2 ? -1 : 1);
        ans += sign * fac[n * m - i] % P * a[i] % P;
        ans = (ans + P) % P;
    } 
    cout << ans << "\n";
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cout << setprecision(15) << fixed;
    int t = 1;
    //cin >> t;
    while (t--) solve();
    return 0;
}


详细

Test #1:

score: 100
Accepted
time: 30ms
memory: 70448kb

input:

2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 37ms
memory: 71204kb

input:

5 1

output:

44

result:

ok 1 number(s): "44"

Test #3:

score: 0
Accepted
time: 39ms
memory: 71200kb

input:

167 91

output:

284830080

result:

ok 1 number(s): "284830080"

Test #4:

score: 0
Accepted
time: 37ms
memory: 72268kb

input:

2 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 30ms
memory: 70940kb

input:

2 3

output:

36

result:

ok 1 number(s): "36"

Test #6:

score: 0
Accepted
time: 37ms
memory: 70916kb

input:

2 4

output:

576

result:

ok 1 number(s): "576"

Test #7:

score: 0
Accepted
time: 29ms
memory: 70652kb

input:

3 1

output:

2

result:

ok 1 number(s): "2"

Test #8:

score: 0
Accepted
time: 30ms
memory: 70492kb

input:

3 2

output:

80

result:

ok 1 number(s): "80"

Test #9:

score: 0
Accepted
time: 34ms
memory: 71808kb

input:

3 3

output:

12096

result:

ok 1 number(s): "12096"

Test #10:

score: 0
Accepted
time: 29ms
memory: 71328kb

input:

3 4

output:

4783104

result:

ok 1 number(s): "4783104"

Test #11:

score: 0
Accepted
time: 29ms
memory: 71348kb

input:

4 1

output:

9

result:

ok 1 number(s): "9"

Test #12:

score: 0
Accepted
time: 34ms
memory: 71480kb

input:

4 2

output:

4752

result:

ok 1 number(s): "4752"

Test #13:

score: 0
Accepted
time: 37ms
memory: 70832kb

input:

4 3

output:

17927568

result:

ok 1 number(s): "17927568"

Test #14:

score: 0
Accepted
time: 30ms
memory: 71516kb

input:

4 4

output:

776703752

result:

ok 1 number(s): "776703752"

Test #15:

score: 0
Accepted
time: 41ms
memory: 70396kb

input:

5 2

output:

440192

result:

ok 1 number(s): "440192"

Test #16:

score: 0
Accepted
time: 33ms
memory: 71452kb

input:

5 3

output:

189125068

result:

ok 1 number(s): "189125068"

Test #17:

score: 0
Accepted
time: 38ms
memory: 71856kb

input:

5 4

output:

975434093

result:

ok 1 number(s): "975434093"

Test #18:

score: 0
Accepted
time: 193ms
memory: 87072kb

input:

1000 1000

output:

720037464

result:

ok 1 number(s): "720037464"

Test #19:

score: 0
Accepted
time: 35ms
memory: 71564kb

input:

72 42

output:

638177567

result:

ok 1 number(s): "638177567"

Test #20:

score: 0
Accepted
time: 33ms
memory: 70812kb

input:

15 19

output:

663050288

result:

ok 1 number(s): "663050288"

Test #21:

score: 0
Accepted
time: 31ms
memory: 71480kb

input:

68 89

output:

94365047

result:

ok 1 number(s): "94365047"

Test #22:

score: 0
Accepted
time: 37ms
memory: 70748kb

input:

92 37

output:

652605307

result:

ok 1 number(s): "652605307"

Test #23:

score: 0
Accepted
time: 30ms
memory: 71576kb

input:

61 87

output:

498277867

result:

ok 1 number(s): "498277867"

Test #24:

score: 0
Accepted
time: 37ms
memory: 70856kb

input:

81 40

output:

133095344

result:

ok 1 number(s): "133095344"

Test #25:

score: 0
Accepted
time: 33ms
memory: 70856kb

input:

7 91

output:

524164813

result:

ok 1 number(s): "524164813"

Test #26:

score: 0
Accepted
time: 26ms
memory: 70396kb

input:

31 18

output:

361233485

result:

ok 1 number(s): "361233485"

Test #27:

score: 0
Accepted
time: 37ms
memory: 70912kb

input:

74 54

output:

500686087

result:

ok 1 number(s): "500686087"

Test #28:

score: 0
Accepted
time: 34ms
memory: 70440kb

input:

32 2

output:

586504335

result:

ok 1 number(s): "586504335"

Test #29:

score: 0
Accepted
time: 111ms
memory: 78804kb

input:

656 718

output:

346764298

result:

ok 1 number(s): "346764298"

Test #30:

score: 0
Accepted
time: 75ms
memory: 74712kb

input:

254 689

output:

358078813

result:

ok 1 number(s): "358078813"

Test #31:

score: 0
Accepted
time: 112ms
memory: 78816kb

input:

713 674

output:

914437613

result:

ok 1 number(s): "914437613"

Test #32:

score: 0
Accepted
time: 53ms
memory: 73936kb

input:

136 698

output:

56687290

result:

ok 1 number(s): "56687290"

Test #33:

score: 0
Accepted
time: 68ms
memory: 74712kb

input:

369 401

output:

312325811

result:

ok 1 number(s): "312325811"

Test #34:

score: 0
Accepted
time: 45ms
memory: 71076kb

input:

280 204

output:

280012063

result:

ok 1 number(s): "280012063"

Test #35:

score: 0
Accepted
time: 68ms
memory: 74716kb

input:

904 225

output:

162909174

result:

ok 1 number(s): "162909174"

Test #36:

score: 0
Accepted
time: 180ms
memory: 87068kb

input:

855 928

output:

39885159

result:

ok 1 number(s): "39885159"

Test #37:

score: 0
Accepted
time: 67ms
memory: 72600kb

input:

503 365

output:

745115888

result:

ok 1 number(s): "745115888"

Test #38:

score: 0
Accepted
time: 175ms
memory: 86996kb

input:

646 996

output:

610925577

result:

ok 1 number(s): "610925577"

Test #39:

score: 0
Accepted
time: 188ms
memory: 86944kb

input:

990 918

output:

203469632

result:

ok 1 number(s): "203469632"

Test #40:

score: 0
Accepted
time: 186ms
memory: 87064kb

input:

961 949

output:

169566857

result:

ok 1 number(s): "169566857"

Test #41:

score: 0
Accepted
time: 186ms
memory: 87064kb

input:

946 932

output:

352423195

result:

ok 1 number(s): "352423195"

Test #42:

score: 0
Accepted
time: 184ms
memory: 87028kb

input:

903 981

output:

196309824

result:

ok 1 number(s): "196309824"

Test #43:

score: 0
Accepted
time: 180ms
memory: 86996kb

input:

916 988

output:

487208972

result:

ok 1 number(s): "487208972"

Test #44:

score: 0
Accepted
time: 180ms
memory: 87020kb

input:

982 982

output:

387421488

result:

ok 1 number(s): "387421488"

Test #45:

score: 0
Accepted
time: 192ms
memory: 86944kb

input:

955 911

output:

955637031

result:

ok 1 number(s): "955637031"

Test #46:

score: 0
Accepted
time: 188ms
memory: 87056kb

input:

906 999

output:

798469943

result:

ok 1 number(s): "798469943"

Test #47:

score: 0
Accepted
time: 196ms
memory: 87068kb

input:

982 975

output:

193506289

result:

ok 1 number(s): "193506289"

Test #48:

score: 0
Accepted
time: 179ms
memory: 84900kb

input:

921 991

output:

431202149

result:

ok 1 number(s): "431202149"