QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#121668 | #6410. Classical DP Problem | SolitaryDream# | WA | 1ms | 3640kb | C++20 | 1.2kb | 2023-07-08 17:07:51 | 2023-07-08 17:07:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1e3+7,P=998244353;
int n,a[N],b[N];
int fac[N],inv[N];
int qpow(int a,int b)
{
int ret=1;
while(b)
{
if(b&1)
ret=1ll*ret*a%P;
b>>=1;
a=1ll*a*a%P;
}
return ret;
}
int k;
int calc(int *a,int x)
{
int ret=1;
for(int i=1;i<=k;i++)
ret=1ll*ret*(a[i]-x)%P;
return ret;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
reverse(a+1,a+n+1);
for(int i=1;i<=n;i++)
b[a[i]]++;
for(int i=n;i>=1;i--)
b[i]+=b[i+1];
k=1;
for(int i=2;i<=n+1;i++)
if(a[i]<i||b[i]<i)
{
k=i-1;
break;
}
int ans=0;
fac[0]=1;
for(int i=1;i<=k;i++)
fac[i]=1ll*fac[i-1]*i%P;
inv[k]=qpow(fac[k],P-2);
for(int i=k-1;i>=0;i--)
inv[i]=1ll*inv[i+1]*(i+1)%P;
int t=b[k+1];
for(int i=0;i<=t;i++)
ans=(ans+1ll*(i&1?P-1:1)*fac[t]%P*inv[i]%P*inv[t-i]%P*calc(a,i))%P;
t=a[k+1];
for(int i=0;i<=t;i++)
ans=(ans+1ll*(i&1?P-1:1)*fac[t]%P*inv[i]%P*inv[t-i]%P*calc(b,i))%P;
ans=(ans-fac[k]+P)%P;
printf("%d %d\n",k,ans);
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3588kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3600kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3640kb
input:
2 1 1
output:
1 1
result:
wrong answer 2nd numbers differ - expected: '2', found: '1'