QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#369255 | #6323. Range NEQ | Vengeful_Spirit# | AC ✓ | 581ms | 51120kb | C++17 | 5.1kb | 2024-03-27 22:35:47 | 2024-03-27 22:35:48 |
Judging History
answer
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
// void cmax(int &x, int y) {x = std::max(x, y);}
typedef long long ll;
const int mod=998244353;
inline int inc(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline int dec(int x,int y){return x-y<0?x-y+mod:x-y;}
inline int mul(int x,int y){return (ll)x*y%mod;}
inline int qpow(int x,int y){
int res=1;
for(;y;y>>=1)res=y&1?mul(res,x):res,x=mul(x,x);
return res;
}
inline int inv(int x){return qpow(x,mod-2);}
const int N=1<<21;
namespace NTT{
int re[N],w[2][N];
inline int getre(int n){
int len=1,bit=0;
while(len<n)len<<=1,++bit;
for(int i=1;i<len;++i)re[i]=(re[i>>1]>>1)|((i&1)<<(bit-1));
w[0][0]=w[1][0]=1,w[0][1]=qpow(3,(mod-1)/len),w[1][1]=inv(w[0][1]);
for(int o=0;o<2;++o)for(int i=2;i<len;++i)
w[o][i]=mul(w[o][i-1],w[o][1]);
return len;
}
inline void NTT(int* a,int n,int o=0){
for(int i=1;i<n;++i)if(i<re[i])swap(a[i],a[re[i]]);
int R;
for(int k=1;k<n;k<<=1)
for(int i=0,t=k<<1,st=n/t;i<n;i+=t)
for(int j=0,nw=0;j<k;++j,nw+=st)
R=mul(a[i+j+k],w[o][nw]),a[i+j+k]=dec(a[i+j],R),a[i+j]=inc(a[i+j],R);
if(o){
R=inv(n);
for(int i=0;i<n;++i)a[i]=mul(a[i],R);
}
}
int t0[N],t1[N],t2[N];
inline void multi(const int* a,const int* b,int* c,int n,int m){
if(n+m<=32){
memset(c,0,sizeof(int)*(n+m+1));
for(int i=0;i<=n;++i)
for(int j=0;j<=m;++j)
c[i+j]=inc(c[i+j],mul(a[i],b[j]));
return;
}
int len=getre(n+m+1);
memset(t0,0,sizeof(int)*len),memcpy(t0,a,sizeof(int)*(n+1));
memset(t1,0,sizeof(int)*len),memcpy(t1,b,sizeof(int)*(m+1));
NTT(t0,len),NTT(t1,len);
for(int i=0;i<len;++i)t0[i]=mul(t0[i],t1[i]);
NTT(t0,len,1);
memcpy(c,t0,sizeof(int)*(n+m+1));
}
inline void inver(const int* a,int* b,int n){
int len=1;
while(len<=n)len<<=1;
memset(t0,0,sizeof(int)*len),memcpy(t0,a,sizeof(int)*(n+1));
memset(t1,0,sizeof(int)*(len<<1));
memset(t2,0,sizeof(int)*(len<<1));
t2[0]=inv(t0[0]);
for(int k=1;k<=len;k<<=1){
memcpy(t1,t0,sizeof(int)*k);
getre(k<<1);
NTT(t1,k<<1),NTT(t2,k<<1);
for(int i=0;i<(k<<1);++i)t2[i]=mul(t2[i],dec(2,mul(t1[i],t2[i])));
NTT(t2,k<<1,1);
for(int i=k;i<(k<<1);++i)t2[i]=0;
}
memcpy(b,t2,sizeof(int)*(n+1));
}
} //namespace NTT
struct poly:public vector<int>{
inline int time()const{return size()-1;}
inline poly(int tim=0,int c=0){
resize(tim+1);
if(tim>=0)at(0)=c;
}
inline poly operator%(const int& n)const{
poly r(*this);
r.resize(n);
return r;
}
inline poly operator%=(const int& n){
resize(n);
return *this;
}
inline poly operator+(const poly& p)const{
int n=time(),m=p.time();
poly r(*this);
if(n<m)r.resize(m+1);
for(int i=0;i<=m;++i)r[i]=inc(r[i],p[i]);
return r;
}
inline poly operator-(const poly& p)const{
int n=time(),m=p.time();
poly r(*this);
if(n<m)r.resize(m+1);
for(int i=0;i<=m;++i)r[i]=dec(r[i],p[i]);
return r;
}
inline poly operator*(const poly& p)const{
poly r(time()+p.time());
NTT::multi(&((*this)[0]),&p[0],&r[0],time(),p.time());
return r;
}
inline poly operator*(const int& k)const{
poly r(*this);
int n=time();
for(int i=0;i<=n;++i)r[i]=mul(r[i],k);
return r;
}
inline poly operator~()const{
poly r(*this);
reverse(r.begin(),r.end());
return r;
}
};
inline poly inv(const poly& a){
poly r(a.time());
NTT::inver(&a[0],&r[0],a.time());
return r;
}
inline poly der(const poly& a){
int n=a.time();
poly r(n-1);
for(int i=1;i<=n;++i)r[i-1]=mul(a[i],i);
return r;
}
int _[N];
inline poly itr(const poly& a){
int n=a.time();
poly r(n+1);
_[1]=1;
for(int i=2;i<=n+1;++i)_[i]=mul(_[mod%i],mod-mod/i);
for(int i=0;i<=n;++i)r[i+1]=mul(a[i],_[i+1]);
return r;
}
inline poly ln(const poly& a){
return itr(der(a)*inv(a)%a.time());
}
inline poly exp(const poly& a){
poly r(0,1);
int n=a.time(),k=1;
while(r.time()<n)
r%=k,r=r*(a%k-ln(r)+poly(0,1))%k,k<<=1;
return r%(n+1);
}
inline poly qpow(const poly& a,int k){
return exp(ln(a)*k);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int i, N, M;
cin >> N >> M;
const int mod = 998244353;
#define Mul mul
#define fpow qpow
#define Add inc
vector<int> fac((N*M)+1), inv((N*M)+1);
for(inv[0]=fac[0]=fac[1]=inv[1]=1,i=2;i<=(N*M);++i)
fac[i]=Mul(fac[i-1], i), inv[i]=Mul(inv[mod%i],mod-mod/i);
for(i=2;i<=(N*M);++i) inv[i]=Mul(inv[i-1],inv[i]);
poly a(M);
for(i=0;i<=M;++i) a[i]=Mul(inv[i], Mul(inv[M-i], inv[M-i]));
int __0 = a[0], __1 = fpow(__0, mod-2);
for(i=0;i<=M;++i) a[i]=Mul(a[i], __1);
auto c = poly(0, 1);
for(int nn = N; nn; nn>>=1, a=a*a) {
if(nn&1) c=c*a;
}
// poly c=qpow(a, N);
// ln(a, b, N*M+1);
// for(i=0;i<N*M+1;++i) b[i]=Mul(b[i], N);
// exp(b, c, N*M+1);
int ans=0, fMN=fpow(fac[M], N), ifMN = fpow(fMN, mod-2);
for(i=0;i<N*M+1;++i) c[i]=Mul(c[i], ifMN);
for(int i=0;i<=N*M;++i)
{
int v=Mul(fac[N*M-i], Mul(c[i], fMN));
if(i&1) ans=Add(ans, mod-v);
else ans=Add(ans, v);
}
cout << ans << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3552kb
input:
2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3904kb
input:
5 1
output:
44
result:
ok 1 number(s): "44"
Test #3:
score: 0
Accepted
time: 7ms
memory: 12224kb
input:
167 91
output:
284830080
result:
ok 1 number(s): "284830080"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
2 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
2 3
output:
36
result:
ok 1 number(s): "36"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
2 4
output:
576
result:
ok 1 number(s): "576"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3892kb
input:
3 1
output:
2
result:
ok 1 number(s): "2"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
3 2
output:
80
result:
ok 1 number(s): "80"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
3 3
output:
12096
result:
ok 1 number(s): "12096"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
3 4
output:
4783104
result:
ok 1 number(s): "4783104"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3860kb
input:
4 1
output:
9
result:
ok 1 number(s): "9"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3860kb
input:
4 2
output:
4752
result:
ok 1 number(s): "4752"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3904kb
input:
4 3
output:
17927568
result:
ok 1 number(s): "17927568"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
4 4
output:
776703752
result:
ok 1 number(s): "776703752"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
5 2
output:
440192
result:
ok 1 number(s): "440192"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3836kb
input:
5 3
output:
189125068
result:
ok 1 number(s): "189125068"
Test #17:
score: 0
Accepted
time: 1ms
memory: 3600kb
input:
5 4
output:
975434093
result:
ok 1 number(s): "975434093"
Test #18:
score: 0
Accepted
time: 563ms
memory: 51120kb
input:
1000 1000
output:
720037464
result:
ok 1 number(s): "720037464"
Test #19:
score: 0
Accepted
time: 0ms
memory: 11924kb
input:
72 42
output:
638177567
result:
ok 1 number(s): "638177567"
Test #20:
score: 0
Accepted
time: 0ms
memory: 11820kb
input:
15 19
output:
663050288
result:
ok 1 number(s): "663050288"
Test #21:
score: 0
Accepted
time: 4ms
memory: 12076kb
input:
68 89
output:
94365047
result:
ok 1 number(s): "94365047"
Test #22:
score: 0
Accepted
time: 0ms
memory: 11916kb
input:
92 37
output:
652605307
result:
ok 1 number(s): "652605307"
Test #23:
score: 0
Accepted
time: 3ms
memory: 11948kb
input:
61 87
output:
498277867
result:
ok 1 number(s): "498277867"
Test #24:
score: 0
Accepted
time: 3ms
memory: 11872kb
input:
81 40
output:
133095344
result:
ok 1 number(s): "133095344"
Test #25:
score: 0
Accepted
time: 2ms
memory: 12104kb
input:
7 91
output:
524164813
result:
ok 1 number(s): "524164813"
Test #26:
score: 0
Accepted
time: 2ms
memory: 11856kb
input:
31 18
output:
361233485
result:
ok 1 number(s): "361233485"
Test #27:
score: 0
Accepted
time: 0ms
memory: 11924kb
input:
74 54
output:
500686087
result:
ok 1 number(s): "500686087"
Test #28:
score: 0
Accepted
time: 0ms
memory: 12056kb
input:
32 2
output:
586504335
result:
ok 1 number(s): "586504335"
Test #29:
score: 0
Accepted
time: 344ms
memory: 42904kb
input:
656 718
output:
346764298
result:
ok 1 number(s): "346764298"
Test #30:
score: 0
Accepted
time: 80ms
memory: 15524kb
input:
254 689
output:
358078813
result:
ok 1 number(s): "358078813"
Test #31:
score: 0
Accepted
time: 358ms
memory: 43452kb
input:
713 674
output:
914437613
result:
ok 1 number(s): "914437613"
Test #32:
score: 0
Accepted
time: 52ms
memory: 24856kb
input:
136 698
output:
56687290
result:
ok 1 number(s): "56687290"
Test #33:
score: 0
Accepted
time: 70ms
memory: 24772kb
input:
369 401
output:
312325811
result:
ok 1 number(s): "312325811"
Test #34:
score: 0
Accepted
time: 22ms
memory: 23544kb
input:
280 204
output:
280012063
result:
ok 1 number(s): "280012063"
Test #35:
score: 0
Accepted
time: 80ms
memory: 26376kb
input:
904 225
output:
162909174
result:
ok 1 number(s): "162909174"
Test #36:
score: 0
Accepted
time: 526ms
memory: 48340kb
input:
855 928
output:
39885159
result:
ok 1 number(s): "39885159"
Test #37:
score: 0
Accepted
time: 77ms
memory: 25640kb
input:
503 365
output:
745115888
result:
ok 1 number(s): "745115888"
Test #38:
score: 0
Accepted
time: 489ms
memory: 46960kb
input:
646 996
output:
610925577
result:
ok 1 number(s): "610925577"
Test #39:
score: 0
Accepted
time: 581ms
memory: 49744kb
input:
990 918
output:
203469632
result:
ok 1 number(s): "203469632"
Test #40:
score: 0
Accepted
time: 566ms
memory: 48924kb
input:
961 949
output:
169566857
result:
ok 1 number(s): "169566857"
Test #41:
score: 0
Accepted
time: 567ms
memory: 49752kb
input:
946 932
output:
352423195
result:
ok 1 number(s): "352423195"
Test #42:
score: 0
Accepted
time: 527ms
memory: 49284kb
input:
903 981
output:
196309824
result:
ok 1 number(s): "196309824"
Test #43:
score: 0
Accepted
time: 557ms
memory: 49776kb
input:
916 988
output:
487208972
result:
ok 1 number(s): "487208972"
Test #44:
score: 0
Accepted
time: 579ms
memory: 50180kb
input:
982 982
output:
387421488
result:
ok 1 number(s): "387421488"
Test #45:
score: 0
Accepted
time: 542ms
memory: 48828kb
input:
955 911
output:
955637031
result:
ok 1 number(s): "955637031"
Test #46:
score: 0
Accepted
time: 558ms
memory: 49328kb
input:
906 999
output:
798469943
result:
ok 1 number(s): "798469943"
Test #47:
score: 0
Accepted
time: 565ms
memory: 50592kb
input:
982 975
output:
193506289
result:
ok 1 number(s): "193506289"
Test #48:
score: 0
Accepted
time: 543ms
memory: 48972kb
input:
921 991
output:
431202149
result:
ok 1 number(s): "431202149"