QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417499 | #8715. 放苹果 | light_ink_dots# | AC ✓ | 211ms | 72768kb | C++20 | 2.9kb | 2024-05-22 19:19:19 | 2024-05-22 19:19:20 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxk=21,maxn=1<<(maxk-1),mod=998244353,G=3,invG=(mod+1)/3;
int n,m,ans;
int fac[maxn],nfac[maxn],inv[maxn],btf[maxn],w[maxk][maxn][2],fu[2]={1,mod-1};
typedef vector<int>poly;
inline int C(int a,int b){
return a<b? 0:1ll*fac[a]*nfac[b]%mod*nfac[a-b]%mod;
}
int ksm(int a,int b){
int res=1;
while(b){
if(b&1)
res=1ll*res*a%mod;
a=1ll*a*a%mod,b>>=1;
}
return res;
}
void init(){
for(int len=2,i=1;i<maxk;len<<=1,i++){
int o0=ksm(G,(mod-1)/len),o1=ksm(invG,(mod-1)/len);
w[i][0][0]=w[i][0][1]=1;
for(int j=1;j<len;j++)
w[i][j][0]=1ll*w[i][j-1][0]*o0%mod,w[i][j][1]=1ll*w[i][j-1][1]*o1%mod;
}
}
int getlen(int n){
int r=0;
while((1<<r)<n)
r++;
for(int i=0;i<(1<<r);i++)
btf[i]=(btf[i>>1]>>1)|((i&1)<<(r-1));
return 1<<r;
}
inline int inc(int x){
return x>=mod? x-mod:x;
}
void NTT(poly &x,int lim,int opt){
x.resize(lim);
for(int i=0;i<lim;i++)
if(i<btf[i])
swap(x[i],x[btf[i]]);
for(int l=2,p=1;l<=lim;l<<=1,p++)
for(int i=0,h=l>>1;i<lim;i+=l)
for(int j=0;j<h;j++){
int a=x[i+j],b=1ll*x[i+j+h]*w[p][j][opt]%mod;
x[i+j]=inc(a+b),x[i+j+h]=inc(a-b+mod);
}
if(opt==1){
int iv=ksm(lim,mod-2);
for(int i=0;i<lim;i++)
x[i]=1ll*x[i]*iv%mod;
}
}
poly operator *(poly a,poly b){
int deg=a.size()+b.size()-1,lim=getlen(deg);
poly res(lim);
NTT(a,lim,0),NTT(b,lim,0);
for(int i=0;i<lim;i++)
res[i]=1ll*a[i]*b[i]%mod;
NTT(res,lim,1),res.resize(deg);
return res;
}
void polyinv(poly &x,int deg){
if(deg==1){
x.resize(1),x[0]=ksm(x[0],mod-2);
return ;
}
poly a=x,b=x;
a.resize(deg),polyinv(b,(deg+1)>>1);
int lim=getlen(deg<<1);
NTT(a,lim,0),NTT(b,lim,0);
x.resize(lim);
for(int i=0;i<lim;i++)
x[i]=1ll*b[i]*(2+1ll*(mod-a[i])*b[i]%mod)%mod;
NTT(x,lim,1),x.resize(deg);
}
int main(){
init();
fac[0]=fac[1]=nfac[0]=nfac[1]=inv[1]=1;
for(int i=2;i<maxn;i++)
fac[i]=1ll*fac[i-1]*i%mod,inv[i]=mod-1ll*(mod/i)*inv[mod%i]%mod,nfac[i]=1ll*nfac[i-1]*inv[i]%mod;
scanf("%d%d",&n,&m);
poly f(n+1),g(n+1),p(n+1),q(n+1);
for(int i=0;i<=n;i++)
f[n-i]=1ll*C(n,i)*min(i,n-i)%mod*fac[n-i]%mod,g[i]=1ll*fu[i&1]*nfac[i]%mod;
reverse(g.begin(),g.end());
f=f*g;
for(int i=0;i<=n;i++)
p[i]=q[i]=nfac[i+1],p[i]=1ll*p[i]*ksm(m+1,i+1)%mod;
polyinv(q,n+1),p=p*q;
for(int i=0;i<=n;i++){
p[i]=1ll*p[i]*fac[i]%mod;
if(i==0)
p[i]=(p[i]-1+mod)%mod;
// printf("p[i]=%d\n",p[i]);
}
for(int i=0;i<=n;i++){
int val=1ll*f[n+i]*nfac[i]%mod*ksm(m,i)%mod;
// printf("i=%d val=%d\n",i,val);
ans=(ans+1ll*val*p[n-i])%mod;
/*int res=0;
for(int j=0;j<=n;j++)
res=(res+1ll*C(n,j)*C(n-j,i)%mod*ksm(m,i)%mod*min(j,n-j))%mod;
printf("res=%d\n",res);*/
}
/*for(int i=1;i<m;i++)
for(int j=1;j<=n;j++)
ans=(ans+1ll*C(n,j)*ksm(i,j)%mod*ksm(m-i,n-j)%mod*min(j,n-j))%mod;*/
printf("%d\n",ans);
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 19ms
memory: 38840kb
input:
2 3
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 11ms
memory: 38620kb
input:
3 3
output:
36
result:
ok 1 number(s): "36"
Test #3:
score: 0
Accepted
time: 30ms
memory: 38624kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 17ms
memory: 38612kb
input:
1 2
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 17ms
memory: 38560kb
input:
1 3
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 11ms
memory: 38656kb
input:
2 1
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 25ms
memory: 38604kb
input:
3 1
output:
0
result:
ok 1 number(s): "0"
Test #8:
score: 0
Accepted
time: 14ms
memory: 38828kb
input:
3719 101
output:
78994090
result:
ok 1 number(s): "78994090"
Test #9:
score: 0
Accepted
time: 23ms
memory: 38788kb
input:
2189 1022
output:
149789741
result:
ok 1 number(s): "149789741"
Test #10:
score: 0
Accepted
time: 15ms
memory: 38800kb
input:
2910 382012013
output:
926541722
result:
ok 1 number(s): "926541722"
Test #11:
score: 0
Accepted
time: 189ms
memory: 62040kb
input:
131072 3837829
output:
487765455
result:
ok 1 number(s): "487765455"
Test #12:
score: 0
Accepted
time: 189ms
memory: 69944kb
input:
183092 100000000
output:
231786691
result:
ok 1 number(s): "231786691"
Test #13:
score: 0
Accepted
time: 188ms
memory: 72412kb
input:
197291 937201572
output:
337054675
result:
ok 1 number(s): "337054675"
Test #14:
score: 0
Accepted
time: 204ms
memory: 72700kb
input:
200000 328194672
output:
420979346
result:
ok 1 number(s): "420979346"
Test #15:
score: 0
Accepted
time: 211ms
memory: 72768kb
input:
200000 1000000000
output:
961552572
result:
ok 1 number(s): "961552572"
Extra Test:
score: 0
Extra Test Passed