#include <bits/stdc++.h> using namespace std; // pewnie nie dynamik po k, bo wtedy mozna by bylo zapytac o odpowiedz na kilka pytan // moze cos z ta liczba pierwsza? (w sensie, ze jest pierwsza: dzielenie) typedef long long ll; int n, k; vector<int> perm; ll res = 0ll, p; vector<int> reduction(vector<int> my_perm) { vector<int> new_perm; if (my_perm[0] > my_perm[1]) new_perm.push_back(my_perm[0]); for(int i = 1; i < my_perm.size() - 1; i++) { if (my_perm[i] > my_perm[i-1] && my_perm[i] > my_perm[i+1]) new_perm.push_back(my_perm[i]); } if (my_perm[my_perm.size() - 1] > my_perm[my_perm.size() - 2]) new_perm.push_back(my_perm[my_perm.size() - 1]); return new_perm; } int reduction_number(vector<int> my_perm) { if (my_perm.size() == 1) return 0; return 1 + reduction_number(reduction(my_perm)); } ll counts[11]; int main() { scanf("%d%d%lld", &n, &k, &p); if ((n == 1) || (k > 10)){ printf("0\n"); return 0; } if (k == 1) { res = 1ll; n--; while(n--){ res *= 2ll; res %= p; } printf("%d\n", res); return 0; } for(int i = 0; i < n; i++) perm.push_back(i); do { /*if (reduction_number(perm) == k){ for(int j = 0; j <= perm.size(); j++){ perm.insert(perm.begin() + j, n); bool found = false; if (reduction_number(perm) == k + 1){ found = true; for(int i : perm) { printf("%d" , i); } printf("\n"); } perm.erase(perm.begin() + j); if (found){ for(int i : perm) { printf("%d" , i); } printf("\n\n"); } } }*/ counts[reduction_number(perm)]++; } while(next_permutation(perm.begin(), perm.end())); printf("%lld\n", counts[k]); }
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 | #include <bits/stdc++.h> using namespace std; // pewnie nie dynamik po k, bo wtedy mozna by bylo zapytac o odpowiedz na kilka pytan // moze cos z ta liczba pierwsza? (w sensie, ze jest pierwsza: dzielenie) typedef long long ll; int n, k; vector<int> perm; ll res = 0ll, p; vector<int> reduction(vector<int> my_perm) { vector<int> new_perm; if (my_perm[0] > my_perm[1]) new_perm.push_back(my_perm[0]); for(int i = 1; i < my_perm.size() - 1; i++) { if (my_perm[i] > my_perm[i-1] && my_perm[i] > my_perm[i+1]) new_perm.push_back(my_perm[i]); } if (my_perm[my_perm.size() - 1] > my_perm[my_perm.size() - 2]) new_perm.push_back(my_perm[my_perm.size() - 1]); return new_perm; } int reduction_number(vector<int> my_perm) { if (my_perm.size() == 1) return 0; return 1 + reduction_number(reduction(my_perm)); } ll counts[11]; int main() { scanf("%d%d%lld", &n, &k, &p); if ((n == 1) || (k > 10)){ printf("0\n"); return 0; } if (k == 1) { res = 1ll; n--; while(n--){ res *= 2ll; res %= p; } printf("%d\n", res); return 0; } for(int i = 0; i < n; i++) perm.push_back(i); do { /*if (reduction_number(perm) == k){ for(int j = 0; j <= perm.size(); j++){ perm.insert(perm.begin() + j, n); bool found = false; if (reduction_number(perm) == k + 1){ found = true; for(int i : perm) { printf("%d" , i); } printf("\n"); } perm.erase(perm.begin() + j); if (found){ for(int i : perm) { printf("%d" , i); } printf("\n\n"); } } }*/ counts[reduction_number(perm)]++; } while(next_permutation(perm.begin(), perm.end())); printf("%lld\n", counts[k]); } |