#include <bits/stdc++.h> int res[30]; int cnt(std::vector<int> v){ v.insert(v.begin()+0, -1); v.push_back(-1); int res = 0; std::vector<int> tmp; while (v.size() > 3){ tmp.clear(); tmp.push_back(-1); res++; for (int i = 1; i < v.size()-1; i++){ if (v[i-1] < v[i] && v[i+1] < v[i]){ tmp.push_back(v[i]); } } tmp.push_back(-1); v = tmp; } return res; } int main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n, k, P; std::cin >> n >> k >> P; if (k >= 11){ std::cout << "0\n"; return 0; } std::vector<int> v; for (int i = 1; i <= n; i++) v.push_back(i); do{ int x = cnt(v); res[x] = (res[x] + 1)%P; } while (std::next_permutation(v.begin(), v.end())); std::cout << res[k] << "\n"; }
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 | #include <bits/stdc++.h> int res[30]; int cnt(std::vector<int> v){ v.insert(v.begin()+0, -1); v.push_back(-1); int res = 0; std::vector<int> tmp; while (v.size() > 3){ tmp.clear(); tmp.push_back(-1); res++; for (int i = 1; i < v.size()-1; i++){ if (v[i-1] < v[i] && v[i+1] < v[i]){ tmp.push_back(v[i]); } } tmp.push_back(-1); v = tmp; } return res; } int main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n, k, P; std::cin >> n >> k >> P; if (k >= 11){ std::cout << "0\n"; return 0; } std::vector<int> v; for (int i = 1; i <= n; i++) v.push_back(i); do{ int x = cnt(v); res[x] = (res[x] + 1)%P; } while (std::next_permutation(v.begin(), v.end())); std::cout << res[k] << "\n"; } |