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;
#define f first
#define s second
typedef long long ll;
const int N = 1e4+5, mod = 1e6+7;
ll n, dp[2][N][2], a[N];
ll sol(int i, ll j, int bit)
{
/*
if(i == n) return 1;
ll &ret = dp[i][j][bit];
if(ret != -1) return ret;
if(bit) ret = j*sol(i+1, j, 1)%mod + sol(i+1, j+1, 1)%mod;
else ret = (a[i]-1)*sol(i+1, max(j, a[i]-1), 1)%mod +
sol(i+1, max(j, a[i]), 0)%mod;
return ret%mod;
*/
for(int i=0; i<=n; i++) dp[n%2][i][0] = dp[n%2][i][1] = 1LL;
for(int i=n-1; i>=0; i--)
{
for(ll j=0; j<=i; j++)
{
dp[i%2][j][1] = j*dp[(i+1)%2][j][1] + dp[(i+1)%2][j+1][1], dp[i%2][j][1] %= mod;
dp[i%2][j][0] = (a[i]-1)*dp[(i+1)%2][max(j, a[i]-1)][1] +
dp[(i+1)%2][max(j, a[i])][0];
dp[i%2][j][0] %= mod;
}
}
return dp[0][0][0]%mod;
}
int main()
{
memset(dp, -1, sizeof dp);
cin >> n;
for(int i=0; i<n; i++) cin >> a[i];
cout << sol(0, 0, 0) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |