#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#pragma GCC target ("avx,tune=native")
//Use above if bruteforcing with lots of small operations. Or just use it anytime, there's no downside. AVX is better slightly
/*
TASK: hidden
LANG: C++11
*/
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pair2;
typedef pair<int, pair<int, int> > pair3;
typedef pair<int, pair<int, pair<int, int> > > pair4;
#define MAXN 10013
#define INF 1000000000000000000LL
#define mp make_pair
#define add push_back
#define remove pop
#define MOD 1000007
int n;
int values[MAXN];
//int dp[MAXN][MAXN]; //dp[i][j]: num ways from point i IF the highest number that appears before us is j (implying value of i is at most j + 1)
//dp[i][j] = dp[i + 1][j] * j + dp[i + 1][j + 1]
cc_hash_table<int, cc_hash_table<int, int> > dp;
int solve(int index, int first) {
if (first < 0 || first > n) return 0;
if (first == 0) return dp[index][first] = solve(index + 1, first + 1);
if (index == n) return 1;
if (dp[index].find(first) != dp[index].end()) {
return dp[index][first];
}
ll answer = solve(index + 1, first) * first * 1LL + solve(index + 1, first + 1);
answer %= MOD * 1LL;
return dp[index][first] = (int) answer;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> values[i];
}
ll answer = 0;
int highestNum = 0;
for (int i = 0; i < n; i++) {
//highestNum = max(highestNum, values[i]);
//if (highestNum == 1) continue;
answer += (1LL * (values[i] - 1) * solve(i + 1, highestNum)) % MOD;
//cout << "solve " << i << " " << min(values[i] - 2, highestNum - 1) << " is " << solve(i, min(values[i] - 2, highestNum - 1)) << endl;
answer %= MOD * 1LL;
highestNum = max(highestNum, values[i]);
}
cout << answer + 1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2232 KB |
Output is correct |
2 |
Correct |
0 ms |
2232 KB |
Output is correct |
3 |
Correct |
0 ms |
2232 KB |
Output is correct |
4 |
Correct |
0 ms |
2232 KB |
Output is correct |
5 |
Correct |
0 ms |
2232 KB |
Output is correct |
6 |
Memory limit exceeded |
76 ms |
65536 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2232 KB |
Output is correct |
2 |
Correct |
0 ms |
2232 KB |
Output is correct |
3 |
Correct |
0 ms |
2232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2232 KB |
Output is correct |
2 |
Correct |
0 ms |
2232 KB |
Output is correct |
3 |
Correct |
0 ms |
2232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2496 KB |
Output is correct |
2 |
Correct |
0 ms |
2496 KB |
Output is correct |
3 |
Correct |
0 ms |
2496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
8912 KB |
Output is correct |
2 |
Correct |
29 ms |
8912 KB |
Output is correct |
3 |
Correct |
16 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
28816 KB |
Output is correct |
2 |
Correct |
206 ms |
28816 KB |
Output is correct |
3 |
Correct |
176 ms |
28824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
466 ms |
65536 KB |
Memory limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
486 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
433 ms |
65536 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |