#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const ll NMAX = 10001;
const ll VMAX = 10001;
const ll INF = (1LL << 55);
const ll MOD = 1000007;
const ll BLOCK = 1000000;
const ll base = 1000000001;
const ll nr_of_bits = 18;
ll dp[2][NMAX][3];
ll v[NMAX];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, i, j;
cin >> n;
for(i = 1; i <= n; i++){
cin >> v[i];
}
dp[0][0][1] = 1;
ll sum = 0;
for(i = 1; i <= n; i++){
ll act = i&1;
ll last = 1 - act;
for(j = 1; j <= i; j++){
/// Nu deschid culoare noua
dp[act][j][0] = 1LL * (dp[last][j][0] * j) % MOD;
dp[act][j][0] %= MOD;
dp[act][j][0] += 1LL * (dp[last][j][1] * min(j, v[i] - 1)) % MOD;
dp[act][j][0] %= MOD;
if(j >= v[i])
dp[act][j][1] = dp[last][j][1];
dp[act][j][2] = 1LL * (dp[last][j][2] * j) % MOD;
dp[act][j][2] %= MOD;
dp[act][j][2] += 1LL * (dp[last][j][1] * max(0LL, j - v[i])) % MOD;
dp[act][j][2] %= MOD;
/// Deschid o culoare noua
dp[act][j][0] += dp[last][j - 1][0];
dp[act][j][0] %= MOD;
if(j < v[j]){
dp[act][j][0] += dp[last][j - 1][1];
dp[act][j][0] %= MOD;
}
if(j == v[i]){
dp[act][j][1] += dp[last][j - 1][1];
dp[act][j][1] %= MOD;
}
dp[act][j][2] += dp[last][j - 1][2];
dp[act][j][2] %= MOD;
if(j > v[i]){
dp[act][j][2] += dp[last][j - 1][1] % MOD;
dp[act][j][2] %= MOD;
}
if(i == n){
sum += dp[act][j][0];
sum %= MOD;
}
}
for(j = 0; j <= i; j++)
dp[last][j][0] = dp[last][j][1] = dp[last][j][2] = 0;
}
cout << (sum + 1) % MOD;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
372 KB |
Output is correct |
2 |
Correct |
9 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
728 ms |
892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
560 KB |
Output is correct |
2 |
Correct |
229 ms |
768 KB |
Output is correct |
3 |
Correct |
184 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
882 ms |
1068 KB |
Output is correct |
2 |
Correct |
870 ms |
844 KB |
Output is correct |
3 |
Correct |
711 ms |
908 KB |
Output is correct |