#include <iostream> #include <vector> #include <algorithm> using namespace std; bool dobry(vector <int> czek, int k){ int n = czek.size(); int t1[n], t2[n]; for(int i = 0; i < n; i++){ t1[i] = czek[i]; } int m1 = n, m2 = 0, l = 0; while(k > 0){ k--; if(!(l & 1)){ for(int i = 0; i < m1; i++){ if(i == 0){ if(t1[i] > t1[i + 1]){ t2[m2++] = t1[i]; } continue; } if(i == m1 - 1){ if(t1[i] > t1[i - 1]){ t2[m2++] = t1[i]; } continue; } if(t1[i] > t1[i - 1] && t1[i] > t1[i + 1]){ t2[m2++] = t1[i]; } } m1 = 0; if(m2 == 1){ if(k == 0) return true; return false; } } else{ for(int i = 0; i < m2; i++){ if(i == 0){ if(t2[i] > t2[i + 1]){ t1[m1++] = t2[i]; } continue; } if(i == m2 - 1){ if(t2[i] > t2[i - 1]){ t1[m1++] = t2[i]; } continue; } if(t2[i] > t2[i - 1] && t2[i] > t2[i + 1]){ t1[m1++] = t2[i]; } } m2 = 0; if(m1 == 1){ if(k == 0) return true; return false; } } l++; } return false; } vector <int> ciag; int main(){ ios_base::sync_with_stdio(0); long long n, k, p; long long wynik = 0; cin >> n >> k >> p; if(k == 1){ wynik++; for(int i = 0; i < n - 1; i++){ wynik = (wynik << 1) % p; } if(n == 1) cout << 0 << '\n'; else cout << wynik << '\n'; return 0; } if(n & 1){ if(k > n / 2 + 1){ cout << 0 << '\n'; return 0; } } else{ if(k > n / 2){ cout << 0 << '\n'; return 0; } } for(int i = 1; i <= n; i++) ciag.push_back(i); do{ if(dobry(ciag, k)){ wynik = (wynik + 1)% p; } }while(next_permutation(ciag.begin(), ciag.end())); cout << wynik << '\n'; }
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <iostream> #include <vector> #include <algorithm> using namespace std; bool dobry(vector <int> czek, int k){ int n = czek.size(); int t1[n], t2[n]; for(int i = 0; i < n; i++){ t1[i] = czek[i]; } int m1 = n, m2 = 0, l = 0; while(k > 0){ k--; if(!(l & 1)){ for(int i = 0; i < m1; i++){ if(i == 0){ if(t1[i] > t1[i + 1]){ t2[m2++] = t1[i]; } continue; } if(i == m1 - 1){ if(t1[i] > t1[i - 1]){ t2[m2++] = t1[i]; } continue; } if(t1[i] > t1[i - 1] && t1[i] > t1[i + 1]){ t2[m2++] = t1[i]; } } m1 = 0; if(m2 == 1){ if(k == 0) return true; return false; } } else{ for(int i = 0; i < m2; i++){ if(i == 0){ if(t2[i] > t2[i + 1]){ t1[m1++] = t2[i]; } continue; } if(i == m2 - 1){ if(t2[i] > t2[i - 1]){ t1[m1++] = t2[i]; } continue; } if(t2[i] > t2[i - 1] && t2[i] > t2[i + 1]){ t1[m1++] = t2[i]; } } m2 = 0; if(m1 == 1){ if(k == 0) return true; return false; } } l++; } return false; } vector <int> ciag; int main(){ ios_base::sync_with_stdio(0); long long n, k, p; long long wynik = 0; cin >> n >> k >> p; if(k == 1){ wynik++; for(int i = 0; i < n - 1; i++){ wynik = (wynik << 1) % p; } if(n == 1) cout << 0 << '\n'; else cout << wynik << '\n'; return 0; } if(n & 1){ if(k > n / 2 + 1){ cout << 0 << '\n'; return 0; } } else{ if(k > n / 2){ cout << 0 << '\n'; return 0; } } for(int i = 1; i <= n; i++) ciag.push_back(i); do{ if(dobry(ciag, k)){ wynik = (wynik + 1)% p; } }while(next_permutation(ciag.begin(), ciag.end())); cout << wynik << '\n'; } |