#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 1e6; vector<int> perm; bool check(int k) { vector<int> permt1 = perm; vector<int> permt2; while(k --> 0) { if(permt1.size() == 1) return false; if(permt1[0] > permt1[1]) permt2.push_back(permt1[0]); for(int i = 1;i < permt1.size()-1;++i) { if(permt1[i] > permt1[i-1] && permt1[i] > permt1[i+1]) permt2.push_back(permt1[i]); } if(permt1.back() > permt1[permt1.size()-2]) permt2.push_back(permt1.back()); permt1 = permt2; permt2.clear(); } if(permt1.size() == 1) return true; return false; } int main() { ios::sync_with_stdio(0); int n, k, p; cin >> n >> k >> p; for(int i = 1;i <= n;++i) perm.push_back(i); int res = 0; do { if(check(k)) ++res; res %= p; } while(next_permutation(perm.begin(), perm.end())); cout << res % p << 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 49 50 51 52 53 54 55 56 57 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 1e6; vector<int> perm; bool check(int k) { vector<int> permt1 = perm; vector<int> permt2; while(k --> 0) { if(permt1.size() == 1) return false; if(permt1[0] > permt1[1]) permt2.push_back(permt1[0]); for(int i = 1;i < permt1.size()-1;++i) { if(permt1[i] > permt1[i-1] && permt1[i] > permt1[i+1]) permt2.push_back(permt1[i]); } if(permt1.back() > permt1[permt1.size()-2]) permt2.push_back(permt1.back()); permt1 = permt2; permt2.clear(); } if(permt1.size() == 1) return true; return false; } int main() { ios::sync_with_stdio(0); int n, k, p; cin >> n >> k >> p; for(int i = 1;i <= n;++i) perm.push_back(i); int res = 0; do { if(check(k)) ++res; res %= p; } while(next_permutation(perm.begin(), perm.end())); cout << res % p << endl; return 0; } |