This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
int fact[610],comb[610][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],ans[610][610][2],cans[610][610][2];
long long raspunde(short step,short nrstack,short ppoz,bool exista)
{
int vans=0;
short ramas=n-step-nrstack;
short posibileInceput=v[ppoz]-1-(2*step-step+ppoz-1-nrstack);
if(posibileInceput<0||ramas<0||ramas==0&&exista==false||nrstack==0&&exista==true)
return 0;
if(exista==false)
{
if(ramas>=3)
vans=(1LL*vans+1LL*comb[ramas-1][2]*cans[nrstack+1][ppoz][false])%MOD;
if(ramas>=2)
vans=(1LL*vans+1LL*(ramas-1)*cans[nrstack+1][ppoz][true])%MOD;
if(posibileInceput>=2&&nrstack>=1)
vans=(1LL*vans+1LL*comb[posibileInceput][2]*cans[nrstack-1][ppoz][false])%MOD;
if(posibileInceput&&nrstack>=1&&ramas>=1)
{
if(ramas-1)
vans=(1LL*vans+1LL*posibileInceput*(ramas-1)%MOD*cans[nrstack][ppoz][false])%MOD;
vans=(1LL*vans+1LL*posibileInceput*cans[nrstack][ppoz][true])%MOD;
}
else if(posibileInceput&&ramas>=1)
{
if(step==n-1)
return 1;
if(ramas-1)
vans=(1LL*vans+1LL*posibileInceput*(ramas-1)%MOD*cans[nrstack][ppoz][false])%MOD;
int q=putere(comb[n-ppoz][step+1-ppoz],MOD-2);
for(int newppoz=ppoz+1;newppoz<=step+2;++newppoz)
vans=(1LL*vans+1LL*comb[n-newppoz][step+2-newppoz]*q%MOD*posibileInceput%MOD*cans[nrstack][newppoz][false])%MOD;
}
}
else
{
if(ramas>=2)
vans=(1LL*vans+1LL*comb[ramas][2]*cans[nrstack+1][ppoz][true])%MOD;
if(posibileInceput>=2)
{
if(nrstack>=2)
vans=(1LL*vans+1LL*comb[posibileInceput][2]*cans[nrstack-1][ppoz][true])%MOD;
else
{
if(step==n-1)
return 1;
int q=putere(comb[n-ppoz][step+1-ppoz],MOD-2);
for(int newppoz=ppoz+1;newppoz<=step+2;++newppoz)
vans=(1LL*vans+1LL*comb[n-newppoz][step+2-newppoz]*q%MOD*comb[posibileInceput][2]%MOD*cans[nrstack-1][newppoz][false])%MOD;
}
}
if(posibileInceput&&ramas>=1)
vans=(1LL*vans+1LL*posibileInceput*ramas%MOD*cans[nrstack][ppoz][true])%MOD;
}
return vans;
}
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];
for(int i=0;i<=n;++i)
for(int j=0;j<=i;++j)
comb[i][j]=combinari(i,j);
for(int step=n-1;step>=0;--step)
{
for(int nrstack=0;nrstack<=step&&nrstack+step<=n;++nrstack)
for(int ppoz=1;ppoz<=step+1;++ppoz)
for(int q=0;q<=1;++q)
ans[nrstack][ppoz][q]=raspunde(step,nrstack,ppoz,q);
for(int nrstack=0;nrstack<=step&&nrstack+step<=n;++nrstack)
for(int ppoz=1;ppoz<=step+1;++ppoz)
for(int q=0;q<=1;++q)
cans[nrstack][ppoz][q]=ans[nrstack][ppoz][q];
}
cout<<ans[0][1][false];
return 0;
}
Compilation message (stderr)
ruins3.cpp: In function 'long long int raspunde(short int, short int, short int, bool)':
ruins3.cpp:41:44: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
41 | if(posibileInceput<0||ramas<0||ramas==0&&exista==false||nrstack==0&&exista==true)
| ~~~~~~~~^~~~~~~~~~~~~~~
ruins3.cpp:41:71: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
41 | if(posibileInceput<0||ramas<0||ramas==0&&exista==false||nrstack==0&&exista==true)
| ~~~~~~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |