#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e4+4, M = 1e9+7;
int n, a[maxn];
void madd(int& a, int b) {
a = (a+b) % M;
}
int mult(int a, int b) {
return (1LL*a*b) % M;
}
/*
* f(i,j) = # combinations, w/ i spaces left, that can be up to j
* f(i,j) = j*f(i-1,j) + f(i-1,j+1)
* f(1,x) = x
* f(2,x) = j*x+x+1 = (j+1)*x+1
*
* */
int dp[1003][1003];
int sum[maxn];
int br = 0;
void DFS(vector<int> v) {
if (v.size() == n) {
bool poss = true;
int biggestSeen = 0;
for (int i: v) {
if (i > biggestSeen+1) {
poss = false;
break;
}
biggestSeen = max(biggestSeen,i);
}
if (poss) {
br++;
/*
for (int i: v) {
cout << i << ' ';
}
cout << '\n';
*/
}
}
else {
for (int j = 1; j <= n; j++) {
v.push_back(j);
DFS(v);
v.pop_back();
}
}
}
int brute() {
vector<int> v;
DFS(v);
return br;
}
int main()
{
//ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i <= n; i++) {
dp[1][i] = i;
//cout << dp[1][i] << ' ';
}
//cout << '\n';
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n+1-i; j++) {
dp[i][j] = (j-1)*dp[i-1][j] + dp[i-1][j+1];
//printf("[%d][%d]: %d\n",i,j,dp[i][j]);
//cout << dp[i][j] << ' ';
madd(sum[i],dp[i][j]);
}
//cout << '\n';
}
int ans = 0;
for (int i = 1; i <= n; i++) {
madd(ans,dp[n+1-i][a[i]-1]);
//cout << ans << '\n';
//curr = dp[n+1-i][a[i]];
}
madd(ans,1);
cout << dp[n][1] << '\n';
//cout << "brute: " << ' ' << brute() << '\n';
}
Compilation message
teams.cpp: In function 'void DFS(std::vector<int>)':
teams.cpp:28:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (v.size() == n) {
~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
640 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
4224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
52 ms |
8468 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
30 ms |
8500 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
52 ms |
8536 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |