#include <bits/stdc++.h> using namespace std; int t[100]; int size,K,P; long long result = 0; void check(){ queue <int> out; int currSize = size; int cnt = 0; int help[100] = {}; for(int a = 0; a < size; a++) help[a] = t[a]; while(currSize > 1){ cnt++; if(cnt == 6){ break; } int temp[100] = {}; int i = 0; for(int a = 0; a < currSize; a++){ int last = max(a-1,0); int next = min(currSize-1,a+1); if(help[a] < help[last] || help[a] < help[next]){ //out.push(a); //cout << "out: " << help[a] << endl; } else{ temp[i] = help[a]; i++; } } currSize = i; for(int a = 0; a < i; a++){ help[a] = temp[a]; } } if(cnt == K){ result++; } } int main(){ ios_base::sync_with_stdio(0); cin >> size >> K >> P; if(K == 1 && size > 1){ result = 2; for(int a = 2; a < size; a++){ result *= 2; result %= P; } cout << result << endl; } else{ if(K < 11){ for(int a = 0; a < size; a++){ t[a] = a+1; } do{ check(); }while(next_permutation(t,t+size)); cout << result%P << endl; } else{ cout << 0 << 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 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 | #include <bits/stdc++.h> using namespace std; int t[100]; int size,K,P; long long result = 0; void check(){ queue <int> out; int currSize = size; int cnt = 0; int help[100] = {}; for(int a = 0; a < size; a++) help[a] = t[a]; while(currSize > 1){ cnt++; if(cnt == 6){ break; } int temp[100] = {}; int i = 0; for(int a = 0; a < currSize; a++){ int last = max(a-1,0); int next = min(currSize-1,a+1); if(help[a] < help[last] || help[a] < help[next]){ //out.push(a); //cout << "out: " << help[a] << endl; } else{ temp[i] = help[a]; i++; } } currSize = i; for(int a = 0; a < i; a++){ help[a] = temp[a]; } } if(cnt == K){ result++; } } int main(){ ios_base::sync_with_stdio(0); cin >> size >> K >> P; if(K == 1 && size > 1){ result = 2; for(int a = 2; a < size; a++){ result *= 2; result %= P; } cout << result << endl; } else{ if(K < 11){ for(int a = 0; a < size; a++){ t[a] = a+1; } do{ check(); }while(next_permutation(t,t+size)); cout << result%P << endl; } else{ cout << 0 << endl; } } } |