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>
using namespace std;
const int mod=1000000007;
long long lgput (long long a, long long exp)
{
long long rz=1;
while(exp)
{
if(exp&1)
{
exp^=1;
rz=(rz*1LL*a)%mod;
}
else
{
exp>>=1;
a=(a*1LL*a)%mod;
}
}
return rz;
}
int inv2;
long long cmb2 (int n)
{
if(n<2)
return 0;
return n*(n-1LL)%mod*1LL*inv2%mod;
}
int v[605];
int dp[605][605][2][3][2]; /// 0=NU ; 1=URMEAZA ; 2=POATE
int main()
{
ios_base::sync_with_stdio(false);
dp[0][0][0][0][0]=1;
inv2=lgput(2,mod-2);
int n;
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>v[i];
v[i]-=i;
}
for(int prefX=0;prefX<=n;++prefX)
{
int ci=prefX%2;
int ni=1-ci;
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
for(int b=0;b<=2;++b)
for(int c=0;c<=1;++c)
dp[i][j][ni][b][c]=0;
for(int _=0;_<=n;++_)
{
if(_ > prefX)
dp[_][0][ni][2][0] = ( dp[_][0][ni][2][0] + dp[_][0][ci][2][0]*1LL*(_ - prefX)%mod )%mod;
dp[_][0][ci][0][0] = (dp[_][0][ci][0][0] + dp[_][0][ci][2][0])%mod;
}
for(int _=0;_<=n;++_)
for(int x=0;x<=n;++x)
{
++x;
dp[_][x-1][ci][0][0] = (dp[_][x-1][ci][0][0] + dp[_][x][ci][0][1])%mod;
if(x>=2)
dp[_][x-1][ci][1][0] = (dp[_][x-1][ci][1][0] + dp[_][x][ci][1][1])%mod;
else
dp[_][x-1][ni][2][0] = (dp[_][x-1][ni][2][0] + dp[_][x][ci][1][1])%mod;
--x;
int dpc = dp[_][x][ci][0][0];
int cate=v[prefX+1] - _;
dp[_+2][x+0][ci][0][1] = (dp[_+2][x+0][ci][0][1] + dpc*1LL*cmb2(cate)%mod)%mod;
dp[_+1][x+1][ci][0][1] = (dp[_+1][x+1][ci][0][1] + dpc*1LL*cate%mod)%mod;
dp[_+1][x+1][ci][1][1] = (dp[_+1][x+1][ci][1][1] + dpc*1LL*cate%mod)%mod;
dp[_+0][x+2][ci][0][1] = (dp[_+0][x+2][ci][0][1] + dpc*1LL*inv2%mod)%mod;
dp[_+0][x+2][ci][1][1] = (dp[_+0][x+2][ci][1][1] + dpc)%mod;
dpc = dp[_][x][ci][1][0];
dp[_+2][x+0][ci][1][1] = (dp[_+2][x+0][ci][1][1] + dpc*1LL*cmb2(cate)%mod)%mod;
dp[_+1][x+1][ci][1][1] = (dp[_+1][x+1][ci][1][1] + dpc*1LL*cate%mod)%mod;
dp[_+0][x+2][ci][1][1] = (dp[_+0][x+2][ci][1][1] + dpc*1LL*inv2%mod)%mod;
}
}
cout<<dp[n][0][n&1][0][0];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |