#include <bits/stdc++.h> using namespace std; typedef long long LL; vector<int> t; int odp[10][10]{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} , {2 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0}, {4, 2, 0, 0, 0, 0, 0, 0, 0, 0} , {8, 16, 0, 0, 0, 0, 0, 0, 0, 0} , {16, 100, 4, 0,0, 0, 0, 0, 0, 0} , {32, 616, 72, 0, 0, 0, 0, 0, 0, 0}, {64, 4024, 952, 0, 0, 0, 0, 0, 0, 0}, {128, 28512, 11680, 0, 0, 0, 0, 0, 0, 0}, {256, 219664, 142800, 160, 0, 0, 0, 0, 0, 0}, {512, 1831712, 1788896, 7680, 0, 0, 0, 0, 0, 0} }; bool czy[20]; int sprawdz(vector<int> v) { int ind = 0; while(v.size() > 1) { ind++; if(v[0] < v[1]) czy[0] = 1; if(v[v.size() - 1] < v[v.size() - 2]) czy[v.size() - 1] = 1; for(int i = 1; i < v.size() - 1; i++) if(v[i] < v[i - 1] || v[i] < v[i + 1]) czy[i] = 1; for(int i = v.size() - 1; i >= 0; i--) { if(czy[i]) v.erase(v.begin() + i); czy[i] = 0; } } return ind; } int hm(int N) { int licz = 0; while(N > 1) { licz++; if(N % 2) N = N / 2 + 1; else N /= 2; } return licz++; } int main() { int N, K; LL P; cin >> N >> K >> P; if(N <= 10 && K <= 10) cout << odp[N - 1][K - 1]; else if(hm(N) < K) cout << 0; else if(N!= 1 && K == 1) { LL w = 1; for(int i = 1; i < N; i++) w = (w * 2) % P; cout << w; } else{ int w = 0; for(int i = 0; i < N; i++) t.push_back(i + 1); do{ w+=(sprawdz(t) == K ? 1:0); }while(next_permutation(t.begin(), t.begin()+N)); cout << w; } }
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include <bits/stdc++.h> using namespace std; typedef long long LL; vector<int> t; int odp[10][10]{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} , {2 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0}, {4, 2, 0, 0, 0, 0, 0, 0, 0, 0} , {8, 16, 0, 0, 0, 0, 0, 0, 0, 0} , {16, 100, 4, 0,0, 0, 0, 0, 0, 0} , {32, 616, 72, 0, 0, 0, 0, 0, 0, 0}, {64, 4024, 952, 0, 0, 0, 0, 0, 0, 0}, {128, 28512, 11680, 0, 0, 0, 0, 0, 0, 0}, {256, 219664, 142800, 160, 0, 0, 0, 0, 0, 0}, {512, 1831712, 1788896, 7680, 0, 0, 0, 0, 0, 0} }; bool czy[20]; int sprawdz(vector<int> v) { int ind = 0; while(v.size() > 1) { ind++; if(v[0] < v[1]) czy[0] = 1; if(v[v.size() - 1] < v[v.size() - 2]) czy[v.size() - 1] = 1; for(int i = 1; i < v.size() - 1; i++) if(v[i] < v[i - 1] || v[i] < v[i + 1]) czy[i] = 1; for(int i = v.size() - 1; i >= 0; i--) { if(czy[i]) v.erase(v.begin() + i); czy[i] = 0; } } return ind; } int hm(int N) { int licz = 0; while(N > 1) { licz++; if(N % 2) N = N / 2 + 1; else N /= 2; } return licz++; } int main() { int N, K; LL P; cin >> N >> K >> P; if(N <= 10 && K <= 10) cout << odp[N - 1][K - 1]; else if(hm(N) < K) cout << 0; else if(N!= 1 && K == 1) { LL w = 1; for(int i = 1; i < N; i++) w = (w * 2) % P; cout << w; } else{ int w = 0; for(int i = 0; i < N; i++) t.push_back(i + 1); do{ w+=(sprawdz(t) == K ? 1:0); }while(next_permutation(t.begin(), t.begin()+N)); cout << w; } } |