QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#646442 | #6410. Classical DP Problem | wsc2008 | Compile Error | / | / | C++17 | 5.3kb | 2024-10-16 23:07:10 | 2024-10-16 23:07:10 |
Judging History
answer
/**************************************************************
Problem: 5054
User: 2024sfls01
****************************************************************/
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pii pair<ll,ll>
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
using namespace std;
bool Mbe;
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void write(ll x){
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
const ll N=(1<<21)+9,Mod=998244353,G=3,iG=332748118,inv2=(Mod+1)>>1;
ll n,m,ans,a[N],b[N],fac[N],ifac[N],dp[N],cur,w[N],len;
ll pw(ll x,ll p){
ll res=1;
while(p){
if(p&1)res=res*x%Mod;
x=x*x%Mod,p>>=1;
}
return res;
}
void Init(ll n){
fac[0]=1;
rep(i,1,n)fac[i]=fac[i-1]*i%Mod;
ifac[n]=pw(fac[n],Mod-2);
per(i,n-1,0)ifac[i]=ifac[i+1]*(i+1)%Mod;
}
ll C(ll x,ll y){
if(x<y||y<0)return 0;
return fac[x]*ifac[y]%Mod*ifac[x-y]%Mod;
}
struct poly{
vector<ll>a;
ll size()const{return a.size();}
void resize(ll n){a.resize(n);}
ll operator[](ll n)const{return a[n];}
ll &operator[](ll n){return a[n];}
};
ll r[N];
ll InitConv(ll n){
ll p=1,c=0;
while(p<=n)p<<=1,c++;
r[0]=0;
rep(i,1,p-1)r[i]=(r[i>>1]>>1)|((i&1)<<(c-1));
return p;
}
void NTT(poly&a,ll n,ll sgn){
rep(i,0,n-1){
if(i<r[i])swap(a[i],a[r[i]]);
}
for(ll i=1;i<n;i<<=1){
ll Wn=pw((sgn==1?G:iG),(Mod-1)/(i<<1));
for(ll j=0;j<n;j+=(i<<1)){
ll w=1;
rep(k,0,i-1){
ll x=a[j+k],y=a[j+k+i]*w%Mod;
a[j+k]=(x+y)%Mod,a[j+k+i]=(x-y+Mod)%Mod;
w=w*Wn%Mod;
}
}
}
if(~sgn)return ;
ll iv=pw(n,Mod-2);
rep(i,0,n-1)a[i]=a[i]*iv%Mod;
}
poly Brutemul(const poly&a,const poly&b){
poly c;c.resize(a.size()+b.size()-1);
rep(i,0,(ll)a.size()-1){
rep(j,0,(ll)b.size()-1)c[i+j]=(c[i+j]+a[i]*b[j])%Mod;
}
return c;
}
poly operator*(const poly&a,const poly&b){
ll p=(ll)a.size()+(ll)b.size();
if(p<=100)return Brutemul(a,b);
p=InitConv(p);
poly ta=a,tb=b;
ta.resize(p),tb.resize(p);
NTT(ta,p,1),NTT(tb,p,1);
rep(i,0,p-1)ta[i]=ta[i]*tb[i]%Mod;
NTT(ta,p,-1);
ta.resize((ll)a.size()+(ll)b.size()-1);
return ta;
}
poly Inv(ll n,const poly&a){
poly b;
if(n==1){
b.resize(1),b[0]=pw(a[0],Mod-2);
return b;
}
b=Inv((n+1)>>1,a);
ll p=InitConv(n<<1);
b.resize(p);
poly t;t.resize(p);
rep(i,0,n-1)t[i]=a[i];
NTT(b,p,1),NTT(t,p,1);
rep(i,0,p-1)t[i]=(2-t[i]*b[i]%Mod+Mod)%Mod*b[i]%Mod;
NTT(t,p,-1);
t.resize(n);
return t;
}
poly work(ll l,ll r){
if(l==r){
poly f;f.resize(2);
f[0]=1,f[1]=w[l];
return f;
}
ll mid=(l+r)>>1;
poly lf=work(l,mid),rg=work(mid+1,r);
lf=lf*rg,vector<ll>().swap(rg.a);
return lf;
}
poly work2(ll l,ll r){
if(l==r){
poly f;f.resize(2);
f[0]=1,f[1]=Mod-l;
return f;
}
ll mid=(l+r)>>1;
poly lf=work2(l,mid),rg=work2(mid+1,r);
lf=lf*rg,vector<ll>().swap(rg.a);
return lf;
}
ll solve(ll*a,ll*b,ll n,ll m){
ll lim=0;
rep(i,1,n){
if(a[i]>cur)lim++;
}
// assert(cur-lim+1>=1);
len=0;
rep(i,1,cur){
if(b[m-i+1]>lim)w[++len]=b[m-i+1]-lim;
}
poly f;
if(!len)f.resize(1),f[0]=1;
else f=work(1,len);
poly g;
if(lim)g=work2(1,lim);
else g.resize(1),g[0]=1;
g.resize(cur-lim+1);
g=Inv(g.size(),g);
// g.resize(max(cur-lim+1,lim+1));
ll res=0;
rep(i,lim,cur){
if(cur-i>=(ll)f.size())continue;
res=(res+f[cur-i]*g[i-lim]%Mod*fac[lim])%Mod;
}
return res;
}
bool Med;
int main(){
cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
n=read();
rep(i,1,n)a[i]=read();
reverse(a+1,a+n+1);
Init(1<<20);
rep(i,1,n){
if(a[i]>cur)cur++;
else break;
}
write(cur),putchar(' ');
reverse(a+1,a+n+1);
ans=Mod-1;
rep(i,1,cur)ans=ans*i%Mod;
per(i,n,1){
ll j=i;
while(a[j]==a[i])j--;
ll c=n-j,t=a[i]-a[j];
while(t--)b[++m]=c;
i=j+1;
}
ans=(ans+solve(a,b,n,m)+solve(b,a,m,n))%Mod;
write(ans);
cerr<<"\n"<<clock()*1.0/CLOCKS_PER_SEC*1000<<"ms\n";
return 0;
}
Details
answer.code:5:1: error: extended character is not valid in an identifier 5 | | ^ answer.code:6:1: error: extended character is not valid in an identifier 6 | | ^ answer.code:17:1: error: extended character is not valid in an identifier 17 | ll x=0,f=1;char ch=getchar(); | ^ answer.code:17:1: error: extended character is not valid in an identifier answer.code:17:1: error: extended character is not valid in an identifier answer.code:17:1: error: extended character is not valid in an identifier answer.code:18:1: error: extended character is not valid in an identifier 18 | while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} | ^ answer.code:18:1: error: extended character is not valid in an identifier answer.code:18:1: error: extended character is not valid in an identifier answer.code:18:1: error: extended character is not valid in an identifier answer.code:19:1: error: extended character is not valid in an identifier 19 | while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} | ^ answer.code:19:1: error: extended character is not valid in an identifier answer.code:19:1: error: extended character is not valid in an identifier answer.code:19:1: error: extended character is not valid in an identifier answer.code:20:1: error: extended character is not valid in an identifier 20 | return x*f; | ^ answer.code:20:1: error: extended character is not valid in an identifier answer.code:20:1: error: extended character is not valid in an identifier answer.code:20:1: error: extended character is not valid in an identifier answer.code:23:1: error: extended character is not valid in an identifier 23 | if(x<0)putchar('-'),x=-x; | ^ answer.code:23:1: error: extended character is not valid in an identifier answer.code:23:1: error: extended character is not valid in an identifier answer.code:23:1: error: extended character is not valid in an identifier answer.code:24:1: error: extended character is not valid in an identifier 24 | if(x>9)write(x/10); | ^ answer.code:24:1: error: extended character is not valid in an identifier answer.code:24:1: error: extended character is not valid in an identifier answer.code:24:1: error: extended character is not valid in an identifier answer.code:25:1: error: extended character is not valid in an identifier 25 | putchar(x%10+'0'); | ^ answer.code:25:1: error: extended character is not valid in an identifier answer.code:25:1: error: extended character is not valid in an identifier answer.code:25:1: error: extended character is not valid in an identifier answer.code:30:1: error: extended character is not valid in an identifier 30 | ll res=1; | ^ answer.code:30:1: error: extended character is not valid in an identifier answer.code:30:1: error: extended character is not valid in an identifier answer.code:30:1: error: extended character is not valid in an identifier answer.code:31:1: error: extended character is not valid in an identifier 31 | while(p){ | ^ answer.code:31:1: error: extended character is not valid in an identifier answer.code:31:1: error: extended character is not valid in an identifier answer.code:31:1: error: extended character is not valid in an identifier answer.code:32:1: error: extended character is not valid in an identifier 32 | if(p&1)res=res*x%Mod; | ^ answer.code:32:1: error: extended character is not valid in an identifier answer.code:32:1: error: extended character is not valid in an identifier answer.code:32:1: error: extended character is not valid in an identifier answer.code:32:1: error: extended character is not valid in an identifier answer.code:32:1: error: extended character is not valid in an identifier answer.code:32:1: error: extended character is not valid in an identifier answer.code:32:1: error: extended character is not valid in an identifier answer.code:33:1: error: extended character is not valid in an identifier 33 | x=x*x%Mod,p>>=1; | ^ answer.code:33:1: error: extended character is not valid in an identifier answer.code:33:1: error: extended character is not valid in an identifier answer.code:33:1: error: extended character is not valid in an identifier answer.code:33:1: error: extended character is not valid in an identifier answer.code:33:1: error: extended character is not valid in an identifier answer.code:33:1: error: extended character is not valid in an identifier answer.code:33:1: error: extended character is not valid in an identifier answer.code:34:1: error: extended character is not valid in an identifier 34 | } | ^ answer.code:34:1: error: extended character is not valid in an identifier answer.code:34:1: error: extended character is not valid in an identifier answer.code:34:1: error: extended character is not valid in an identifier answer.c...