QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#273217 | #7886. Not Another Eulerian Number Problem | _set_ | AC ✓ | 8ms | 16384kb | C++17 | 7.8kb | 2023-12-02 22:06:15 | 2023-12-02 22:06:16 |
Judging History
answer
// what is matter? never mind.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
#define i128 __int128
//#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 998244353
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,int b){return a.x==b;}
friend bool operator !=(modint a,int b){return a.x!=b;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define poly vector<modint>
const modint G=3,Ginv=modint(1)/3;
inline poly one(){poly a;a.push_back(1);return a;}
vector<int>rev;
int rts[2100000];
inline int ext(int n){
int k=0;
while((1<<k)<n)++k;return k;
}
inline void init(int k){
int n=1<<k;
rts[0]=1,rts[1<<k]=qpow(31,1<<(21-k)).x;
Rep(i,k,1)rts[1<<(i-1)]=1ull*rts[1<<i]*rts[1<<i]%mod;
For(i,1,n-1)rts[i]=1ull*rts[i&(i-1)]*rts[i&-i]%mod;
}
void ntt(poly&a,int k,int typ){
int n=1<<k;
static ull tmp[2100000];
for(int i=0;i<n;++i)tmp[i]=a[i].x;
if(typ==1){
for(int l=n>>1;l>=1;l>>=1){
ull*k=tmp;
for(int*g=rts;k<tmp+n;k+=(l<<1),++g){
for(ull*x=k;x<k+l;++x){
int o=x[l]%mod*(*g)%mod;
x[l]=*x+mod-o,*x+=o;
}
}
}
for(int i=0;i<n;++i)a[i].x=tmp[i]%mod;
}else{
for(int l=1;l<n;l<<=1){
ull*k=tmp;
for(int*g=rts;k<tmp+n;k+=(l<<1),++g){
for(ull*x=k;x<k+l;++x){
int o=x[l]%mod;
x[l]=(*x+mod-o)*(*g)%mod,*x+=o;
}
}
}
int iv=qpow(n,mod-2).x;
for(int i=0;i<n;++i)a[i].x=tmp[i]%mod*iv%mod;
reverse(a.begin()+1,a.end());
}
}
poly operator +(poly a,poly b){
int n=max(a.size(),b.size());a.resize(n),b.resize(n);
For(i,0,n-1)a[i]+=b[i];return a;
}
poly operator -(poly a,poly b){
int n=max(a.size(),b.size());a.resize(n),b.resize(n);
For(i,0,n-1)a[i]-=b[i];return a;
}
poly operator *(poly a,modint b){
int n=a.size();
For(i,0,n-1)a[i]*=b;return a;
}
poly operator *(poly a,poly b){
if(!a.size()||!b.size())return {};
if((int)a.size()<=32 || (int)b.size()<=32){
poly c(a.size()+b.size()-1,0);
for(int i=0;i<a.size();++i)for(int j=0;j<b.size();++j)c[i+j]+=a[i]*b[j];
return c;
}
int n=(int)a.size()+(int)b.size()-1,k=ext(n);
a.resize(1<<k),b.resize(1<<k);
ntt(a,k,1),ntt(b,k,1);
For(i,0,(1<<k)-1)a[i]*=b[i];
ntt(a,k,-1),a.resize(n);return a;
}
poly Tmp;
poly pmul(poly a,poly b,int n,bool ok=0)
{
int k=ext(n);
a.resize(1<<k),ntt(a,k,1);
if(!ok) b.resize(1<<k),ntt(b,k,1),Tmp=b;
For(i,0,(1<<k)-1)a[i]*=Tmp[i];
ntt(a,k,-1),a.resize(n);
return a;
}
poly inv(poly a,int n)
{
a.resize(n);
if(n==1){
poly f(1,1/a[0]);
return f;
}
poly f0=inv(a,(n+1)>>1),f=f0;
poly now=pmul(a,f0,n,0);
for(int i=0;i<f0.size();++i)now[i]=0;
now=pmul(now,poly(0),n,1);
f.resize(n);
for(int i=f0.size();i<n;++i)f[i]=-now[i];
return f;
}
poly inv(poly a){return inv(a,a.size());}
poly deriv(poly a){
int n=(int)a.size()-1;
For(i,0,n-1)a[i]=a[i+1]*(i+1);
a.resize(n);return a;
}
poly inter(poly a){
int n=a.size()+1;a.resize(n); initC(n+1);
Rep(i,n-1,1)a[i]=a[i-1]*iv[i];
a[0]=0;return a;
}
poly ln(poly a){
int n=a.size();
a=inter(deriv(a)*inv(a));
a.resize(n);return a;
}
poly exp(poly a,int k){
int n=1<<k;a.resize(n);
if(n==1)return one();
poly f0=exp(a,k-1);f0.resize(n);
return f0*(one()+a-ln(f0));
}
poly exp(poly a){
int n=a.size(); a[0]=0;
a=exp(a,ext(n));a.resize(n);return a;
}
poly div(poly a,poly b){
int n=a.size(),m=b.size(),k=ext(n-m+1);
reverse(a.begin(),a.end()),reverse(b.begin(),b.end());
a.resize(n-m+1),b.resize(n-m+1);
a=a*inv(b),a.resize(n-m+1),reverse(a.begin(),a.end()); return a;
}
poly modulo(poly a,poly b){
if(b.size()>a.size())return a;
int n=b.size()-1;
a=a-div(a,b)*b;a.resize(n);return a;
}
poly mulx(poly f){
int n=f.size(); f.resize(n+1);
Rep(i,n,1)f[i]=f[i-1]; f[0]=0; return f;
}
poly divx(poly f){
int n=f.size();
For(i,0,n-2) f[i]=f[i+1]; f.pop_back(); return f;
}
poly qpow(poly a,int b){
assert(a[0].x==1);
return exp(ln(a)*b);
}
#define maxn 200005
#define inf 0x3f3f3f3f
int a,n,m,m0,n0;
poly newton(int n){
if(n==1)return {0};
if(n==2)return {0,1};
poly f0=newton((n+1)>>1); f0.resize(n+1);
// cout<<"solve "<<n<<"\n";
poly I=qpow(one()-f0,mod-a);
poly a=I*f0*(::a-2)+I; a.resize(n);
poly b=f0*f0; b.resize(n+1); b=b*I*(1-::a+mod); b.resize(n);
b=divx(b); b.resize(n);
For(i,1,n-1)a[i]*=iv[i];
a=exp(a);
b=b*inv(a),b.resize(n);
For(i,1,n-1)b[i]*=iv[i];
b[0]=1;
poly res=mulx(a*b); res.resize(n);
// for(auto x:res)cout<<x.x<<" ";cout<<" res\n";
return res;
}
/*
u'(v) = 1/v'(x)
u(v) = u'(v) * v / ((1-v)^(a-1))
x = 1/v'(x) * v / ((1-v)^(a-1))
1/v'(x) * v / ((1-v)^(a-1)) - x = 0
v / ((1-v)^(a-1)) - x*v' = 0
((a-2)v+1)/((1-v)^a) - x*v''
how??
https://vfleaking.blog.uoj.ac/blog/43
v' = (v / ((1-v)^(a-1))) / x
*/
signed main()
{
init(21),initC(1<<18|5);
a=read(),n=read(),m=read(),n0=read();
int lst=0;
For(i,1,n0*a){
int x=read();
m0+=(lst>x),lst=x;
}
if(m<m0)puts("0"),exit(0);
if(a==1){
modint res=0;
For(i,m0,m){
modint t=qpow(i+1,n-n0)*C(i-m0+n0,n0);
// * (1-x)^{n+1}
int nd=m-i;
t*=sign(nd)*C(n+1,nd);
res+=t;
}
cout<<res.x;
exit(0);
}
// u = u' * x/(1-x)^{a-1}
// calc u^<-1>
poly ui=newton(m-m0+3);
// ui= {0,1,2,499122182,332748135,707089809,798595693,691839573,595780532};
// poly tmp=ui*qpow(one()-ui,mod-(a-1))-mulx(deriv(ui)); tmp.resize(ui.size());
// for(auto x:tmp)cout<<x.x<<" ";cout<<" tmp\n";
// G_{n0} = x^{m0+1} / (1-x)^{n0+1}
ui.resize(m-m0+2);
poly ux=divx(ui); ux.pb(0);
poly F1=ln(ux),F2=ln(one()-ui);
poly h=(F1*(m0+1)-F2*((1ll*a*n0+1)%mod));
h=exp(h);
h.resize(m-m0+1);
// for(auto x:h)cout<<x.x<<" ";
For(i,0,m-m0)h[i]*=qpow(i+m0+1,n-n0);
h=h*exp(F2*((1ll*a*n+1)%mod));
h.resize(m-m0+1);
// for(auto x:h)cout<<x.x<<" ";cout<<"\n";
// need h(u(i))[x^{m+1}]
// lagrange inversion
++m;
modint res=0;
poly g=exp(F1*(mod-m));
For(i,0,m-m0-1) res+=h[i]*(i+m0+1)*g[m-m0-1-i];
res/=m;
cout<<res.x;
return 0;
}
/*
3 8 8 1
2 3 1
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 15632kb
input:
1 4 2 2 2 1
output:
7
result:
ok 1 number(s): "7"
Test #2:
score: 0
Accepted
time: 8ms
memory: 14824kb
input:
2 4 2 2 1 2 2 1
output:
19
result:
ok 1 number(s): "19"
Test #3:
score: 0
Accepted
time: 8ms
memory: 14812kb
input:
1 1 0 1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 7ms
memory: 15600kb
input:
3 1 0 1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 7ms
memory: 15592kb
input:
4 1 0 1 1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 8ms
memory: 14816kb
input:
5 1 0 1 1 1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 0ms
memory: 15616kb
input:
10 1 0 1 1 1 1 1 1 1 1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: 0
Accepted
time: 4ms
memory: 15360kb
input:
1 3 2 1 1
output:
1
result:
ok 1 number(s): "1"
Test #9:
score: 0
Accepted
time: 2ms
memory: 15272kb
input:
3 3 2 3 2 2 2 3 3 3 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #10:
score: 0
Accepted
time: 8ms
memory: 15932kb
input:
3 3 2 2 1 1 2 2 2 1
output:
5
result:
ok 1 number(s): "5"
Test #11:
score: 0
Accepted
time: 8ms
memory: 16184kb
input:
3 3 2 1 1 1 1
output:
15
result:
ok 1 number(s): "15"
Test #12:
score: 0
Accepted
time: 8ms
memory: 16112kb
input:
3 3 1 2 2 2 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #13:
score: 0
Accepted
time: 4ms
memory: 14352kb
input:
1 4 0 4 4 2 1 3
output:
0
result:
ok 1 number(s): "0"
Test #14:
score: 0
Accepted
time: 0ms
memory: 14216kb
input:
2 4 1 4 1 4 4 3 3 2 2 1
output:
0
result:
ok 1 number(s): "0"
Test #15:
score: 0
Accepted
time: 0ms
memory: 15648kb
input:
2 4 2 4 1 3 3 2 2 4 4 1
output:
1
result:
ok 1 number(s): "1"
Test #16:
score: 0
Accepted
time: 0ms
memory: 15596kb
input:
2 4 2 1 1 1
output:
58
result:
ok 1 number(s): "58"
Test #17:
score: 0
Accepted
time: 4ms
memory: 15132kb
input:
2 4 1 2 1 1 2 2
output:
14
result:
ok 1 number(s): "14"
Test #18:
score: 0
Accepted
time: 8ms
memory: 15928kb
input:
1 8 7 3 2 1 3
output:
0
result:
ok 1 number(s): "0"
Test #19:
score: 0
Accepted
time: 8ms
memory: 15620kb
input:
1 8 0 4 1 2 3 4
output:
1
result:
ok 1 number(s): "1"
Test #20:
score: 0
Accepted
time: 7ms
memory: 14868kb
input:
1 8 4 4 4 3 1 2
output:
771
result:
ok 1 number(s): "771"
Test #21:
score: 0
Accepted
time: 7ms
memory: 16144kb
input:
1 8 1 4 4 3 2 1
output:
0
result:
ok 1 number(s): "0"
Test #22:
score: 0
Accepted
time: 8ms
memory: 16384kb
input:
1 8 7 1 1
output:
1
result:
ok 1 number(s): "1"
Test #23:
score: 0
Accepted
time: 7ms
memory: 15060kb
input:
1 9 2 5 4 3 1 5 2
output:
0
result:
ok 1 number(s): "0"
Test #24:
score: 0
Accepted
time: 4ms
memory: 15608kb
input:
1 9 7 8 6 5 8 3 4 7 2 1
output:
0
result:
ok 1 number(s): "0"
Test #25:
score: 0
Accepted
time: 0ms
memory: 14744kb
input:
1 9 7 7 5 3 2 4 7 6 1
output:
0
result:
ok 1 number(s): "0"
Test #26:
score: 0
Accepted
time: 0ms
memory: 15608kb
input:
1 9 7 6 2 1 3 5 6 4
output:
0
result:
ok 1 number(s): "0"
Test #27:
score: 0
Accepted
time: 4ms
memory: 14828kb
input:
1 9 3 3 1 2 3
output:
20420
result:
ok 1 number(s): "20420"
Test #28:
score: 0
Accepted
time: 3ms
memory: 15092kb
input:
1 10 0 10 5 6 10 1 3 2 4 7 8 9
output:
0
result:
ok 1 number(s): "0"
Test #29:
score: 0
Accepted
time: 4ms
memory: 15628kb
input:
1 10 7 2 2 1
output:
30973
result:
ok 1 number(s): "30973"
Test #30:
score: 0
Accepted
time: 8ms
memory: 15532kb
input:
1 10 1 6 6 3 5 2 4 1
output:
0
result:
ok 1 number(s): "0"
Test #31:
score: 0
Accepted
time: 4ms
memory: 15636kb
input:
1 10 2 1 1
output:
47840
result:
ok 1 number(s): "47840"
Test #32:
score: 0
Accepted
time: 7ms
memory: 14332kb
input:
1 10 5 2 2 1
output:
689155
result:
ok 1 number(s): "689155"
Extra Test:
score: 0
Extra Test Passed