#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
long long putere(long long a,long long b)
{
if(b==0)
return 1;
long long p=putere(a,b/2);
p*=p;
p%=MOD;
if(b%2)
{
p*=a;
p%=MOD;
}
return p;
}
long long fact[610];
void factorial()
{
fact[0]=1;
for(long long i=1;i<=600;++i)
fact[i]=fact[i-1]*i%MOD;
}
long long combinari(long long n,long long m)
{
long long ans=1;
ans=fact[n];
ans*=putere(fact[m],MOD-2);
ans%=MOD;
ans*=putere(fact[n-m],MOD-2);
ans%=MOD;
return ans;
}
long long n,v[610];
long long raspunde(long long step,long long nrstack,long long ppoz,bool exista)
{
if(step==n)
return 1;
long long ans=0;
long long ramas=n-step-nrstack;
long long posibileInceput=v[ppoz]-1-(2*step-step+ppoz-1-nrstack);
if(exista==false)
{
if(ramas>=3)
ans=(ans+combinari(ramas-1,2)*raspunde(step+1,nrstack+1,ppoz,false))%MOD;
if(ramas>=2)
ans=(ans+(ramas-1)*raspunde(step+1,nrstack+1,ppoz,true))%MOD;
if(posibileInceput>=2&&nrstack>=1)
ans=(ans+combinari(posibileInceput,2)*raspunde(step+1,nrstack-1,ppoz,false))%MOD;
if(posibileInceput&&nrstack>=1&&ramas>=1)
{
if(ramas-1)
ans=(ans+posibileInceput*(ramas-1)%MOD*raspunde(step+1,nrstack,ppoz,false))%MOD;
ans=(ans+posibileInceput*raspunde(step+1,nrstack,ppoz,true))%MOD;
}
else if(posibileInceput&&ramas>=1)
{
if(step==n-1)
return 1;
if(ramas-1)
ans=(ans+posibileInceput*(ramas-1)%MOD*raspunde(step+1,nrstack,ppoz,false))%MOD;
long long q=putere(combinari(n-ppoz,step+1-ppoz),MOD-2);
for(int newppoz=ppoz+1;newppoz<=step+2;++newppoz)
ans=(ans+combinari(n-newppoz,step+2-newppoz)*q%MOD*posibileInceput%MOD*raspunde(step+1,nrstack,newppoz,false))%MOD;
}
}
else
{
if(ramas>=2)
ans=(ans+combinari(ramas,2)*raspunde(step+1,nrstack+1,ppoz,true))%MOD;
if(posibileInceput>=2)
{
if(nrstack>=2)
ans=(ans+combinari(posibileInceput,2)*raspunde(step+1,nrstack-1,ppoz,true))%MOD;
else
{
if(step==n-1)
return 1;
long long q=putere(combinari(n-ppoz,step+1-ppoz),MOD-2);
for(int newppoz=ppoz+1;newppoz<=step+2;++newppoz)
ans=(ans+combinari(n-newppoz,step+2-newppoz)*q%MOD*combinari(posibileInceput,2)%MOD*raspunde(step+1,nrstack-1,newppoz,false))%MOD;
}
}
if(posibileInceput&&ramas>=1)
ans=(ans+posibileInceput*ramas%MOD*raspunde(step+1,nrstack,ppoz,true))%MOD;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
factorial();
cin>>n;
for(int i=1;i<=n;++i)
cin>>v[i];
cout<<raspunde(0,0,1,false);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5642 ms |
320 KB |
Output is correct |
3 |
Correct |
24 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Execution timed out |
6043 ms |
212 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5642 ms |
320 KB |
Output is correct |
3 |
Correct |
24 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Execution timed out |
6043 ms |
212 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5642 ms |
320 KB |
Output is correct |
3 |
Correct |
24 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Execution timed out |
6043 ms |
212 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |