#include <bits/stdc++.h> using namespace std; long long res, n, k, M; int get(vector<int> v) { vector<int> wek; if(v.size() < 2)return 0; for(int i = 0; i < v.size(); i++) { int git = 1; if(i != 0 && v[i] < v[i - 1])git = 0; if(i != v.size() - 1 && v[i] < v[i + 1])git = 0; if(git)wek.push_back(v[i]); } return get(wek) + 1; } int main() { vector<int> v; cin >> n >> k >> M; for(int i = 1; i <= n; i++) v.push_back(i); do{ if(get(v) == k)res++; }while(next_permutation(v.begin(), v.end())); cout << res % M; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <bits/stdc++.h> using namespace std; long long res, n, k, M; int get(vector<int> v) { vector<int> wek; if(v.size() < 2)return 0; for(int i = 0; i < v.size(); i++) { int git = 1; if(i != 0 && v[i] < v[i - 1])git = 0; if(i != v.size() - 1 && v[i] < v[i + 1])git = 0; if(git)wek.push_back(v[i]); } return get(wek) + 1; } int main() { vector<int> v; cin >> n >> k >> M; for(int i = 1; i <= n; i++) v.push_back(i); do{ if(get(v) == k)res++; }while(next_permutation(v.begin(), v.end())); cout << res % M; } |