#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int score(const vector<int>& v) {
vector<int> t = v;
int res = 0, prev = -1, curr, next, size = t.size(), next_size;
while(size > 1) {
prev = -1;
next_size = 0;
for(int i = 0; i < size; ++i) {
curr = t[i];
next = -1;
if(i + 1 < size) {
next = t[i + 1];
}
if(prev < curr && curr > next) {
t[next_size] = curr;
++next_size;
}
prev = curr;
}
size = next_size;
++res;
}
return res;
}
int main(int argc, char const *argv[]) {
int n, k, p, res = 0;
cin >> n >> k >> p;
vector<int> v;
v.resize(n);
for(int i = 0; i < n; ++i) {
v[i] = i + 1;
}
do {
res += (score(v) == k);
res %= p;
} while(next_permutation(v.begin(), v.end()));
cout << res << endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int score(const vector<int>& v) { vector<int> t = v; int res = 0, prev = -1, curr, next, size = t.size(), next_size; while(size > 1) { prev = -1; next_size = 0; for(int i = 0; i < size; ++i) { curr = t[i]; next = -1; if(i + 1 < size) { next = t[i + 1]; } if(prev < curr && curr > next) { t[next_size] = curr; ++next_size; } prev = curr; } size = next_size; ++res; } return res; } int main(int argc, char const *argv[]) { int n, k, p, res = 0; cin >> n >> k >> p; vector<int> v; v.resize(n); for(int i = 0; i < n; ++i) { v[i] = i + 1; } do { res += (score(v) == k); res %= p; } while(next_permutation(v.begin(), v.end())); cout << res << endl; return 0; } |
English