#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <set> #include <map> #include <bits/stdc++.h> using namespace std; int f(vector<int> const& v) { if(v.size() == 1) return 0; if(v.size() == 2) return 1; vector<int> v2; if(v[1] < v[0]) v2.push_back(v[0]); for(int i=1; i<v.size()-1; i++) { if(v[i] > v[i-1] && v[i] > v[i+1]) v2.push_back(v[i]); } if(v[v.size()-1] > v[v.size()-2]) v2.push_back(v.back()); return f(v2) + 1; } int main() { ios::sync_with_stdio(0); int n,k,p; cin >> n >> k >> p; vector<int> v; int result = 0; for(int i=1; i<=n; i++) v.push_back(i); do{ if(f(v) == k) ++result; }while(next_permutation(v.begin(), v.end())); cout << result%p << endl; }
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <set> #include <map> #include <bits/stdc++.h> using namespace std; int f(vector<int> const& v) { if(v.size() == 1) return 0; if(v.size() == 2) return 1; vector<int> v2; if(v[1] < v[0]) v2.push_back(v[0]); for(int i=1; i<v.size()-1; i++) { if(v[i] > v[i-1] && v[i] > v[i+1]) v2.push_back(v[i]); } if(v[v.size()-1] > v[v.size()-2]) v2.push_back(v.back()); return f(v2) + 1; } int main() { ios::sync_with_stdio(0); int n,k,p; cin >> n >> k >> p; vector<int> v; int result = 0; for(int i=1; i<=n; i++) v.push_back(i); do{ if(f(v) == k) ++result; }while(next_permutation(v.begin(), v.end())); cout << result%p << endl; } |