#include <bits/stdc++.h> #define ll long long using namespace std; int check(vector <int> v) { int n = v.size(); int k = 0; while(n > 1) { k++; vector <int> v_next; if(v[0] > v[1]) v_next.push_back(v[0]); for(int i = 1; i < (int)v.size() - 1; i++) { if(v[i] > v[i - 1] && v[i] > v[i + 1]) { v_next.push_back(v[i]); } } if(v[v.size() - 1] > v[v.size() - 2]) v_next.push_back(v[v.size() - 1]); v = v_next; n = v.size(); } return k; } int main(){ ios_base::sync_with_stdio(false); ll n, k, p; cin >> n >> k >> p; vector <int> v; for(int i = 1; i <= n; i++) { v.push_back(i); } ll res = 0; do { int ans = check(v); if(ans == k) { res = (res + 1) % 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 <bits/stdc++.h> #define ll long long using namespace std; int check(vector <int> v) { int n = v.size(); int k = 0; while(n > 1) { k++; vector <int> v_next; if(v[0] > v[1]) v_next.push_back(v[0]); for(int i = 1; i < (int)v.size() - 1; i++) { if(v[i] > v[i - 1] && v[i] > v[i + 1]) { v_next.push_back(v[i]); } } if(v[v.size() - 1] > v[v.size() - 2]) v_next.push_back(v[v.size() - 1]); v = v_next; n = v.size(); } return k; } int main(){ ios_base::sync_with_stdio(false); ll n, k, p; cin >> n >> k >> p; vector <int> v; for(int i = 1; i <= n; i++) { v.push_back(i); } ll res = 0; do { int ans = check(v); if(ans == k) { res = (res + 1) % p; } } while(next_permutation(v.begin(), v.end())); cout << res << endl; return 0; } |