#include <bits/stdc++.h>
using namespace std;
#define REP(i, j, k) for (int i = j; i < k; i++)
#define RREP(i, j, k) for (int i = j; i >= k; i--)
template<class T>
inline bool mxto(T &a, T b) {return a < b ? a = b, 1 : 0;}
template<class T>
inline bool mnto(T &a, T b) {return a > b ? a = b, 1 : 0;}
typedef long long ll;
#define FI first
#define SE second
#define MP make_pair
typedef pair<int, int> ii;
#define pb push_back
#define ALL(x) x.begin(), x.end()
typedef vector<int> vi;
typedef vector<ii> vii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
#define INF 1000000005
#define LINF 1000000000000000005ll
#define MAXN 10005
#define MOD 1000007
int n;
int a[MAXN];
int memo[2][MAXN][2];
int dp(int i, int j, bool same) {
if (i >= n) {
return 1;
}
int res = 0;
if (same) {
res += (ll) memo[i & 1 ^ 1][j][0] * min(j, a[i] - 1) % MOD;
if (res >= MOD) {
res -= MOD;
}
if (a[i] <= j + 1) {
res += memo[i & 1 ^ 1][max(j, a[i])][1];
if (res >= MOD) {
res -= MOD;
}
}
} else {
res += (ll) memo[i & 1 ^ 1][j][0] * j % MOD;
if (res >= MOD) {
res -= MOD;
}
res += memo[i & 1 ^ 1][j + 1][0];
if (res >= MOD) {
res -= MOD;
}
}
return memo[i & 1][j][same] = res;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n;
REP (i, 0, n) {
cin >> a[i];
}
REP (j, 0, n + 1) {
REP (k, 0, 2) {
memo[n & 1][j][k] = 1;
}
}
RREP (i, n - 1, 0) {
REP (j, 0, i + 1) {
REP (k, 0, 2) {
dp(i, j, k);
}
}
}
cout << memo[0][0][1] << '\n';
return 0;
}
Compilation message
teams.cpp: In function 'int dp(int, int, bool)':
teams.cpp:41:22: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
41 | res += (ll) memo[i & 1 ^ 1][j][0] * min(j, a[i] - 1) % MOD;
| ~~^~~
teams.cpp:46:18: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
46 | res += memo[i & 1 ^ 1][max(j, a[i])][1];
| ~~^~~
teams.cpp:52:22: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
52 | res += (ll) memo[i & 1 ^ 1][j][0] * j % MOD;
| ~~^~~
teams.cpp:56:17: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
56 | res += memo[i & 1 ^ 1][j + 1][0];
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
332 KB |
Output is correct |
2 |
Correct |
9 ms |
332 KB |
Output is correct |
3 |
Correct |
7 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
585 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
452 KB |
Output is correct |
2 |
Correct |
170 ms |
412 KB |
Output is correct |
3 |
Correct |
126 ms |
412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
688 ms |
492 KB |
Output is correct |
2 |
Correct |
683 ms |
460 KB |
Output is correct |
3 |
Correct |
613 ms |
540 KB |
Output is correct |