#include <bits/stdc++.h> using namespace std; int findLog(int n){ int it = 0; n--; while(n > 0){ n = (n >> 1); it++; } return it; } int findPow(int n, int p){ int it = 1; for(int i=0; i<n; i++){ it = (it << 1); it %= p; } return it; } vector<int> createVector(vector<int>& V){ vector<int> A; A.push_back(0); for(int i=0; i<V.size(); i++) A.push_back(V[i]); A.push_back(0); return A; } int main(){ int n, k, p; scanf("%d%d%d", &n, &k, &p); int log = findLog(n); if(log < k){ printf("0\n"); return 0; } if(k == 1){ int pow = findPow(n, p); printf("%d\n", pow); return 0; } vector<int> V; for(int i=1; i<=n; i++) V.push_back(i); int res = 0; do{ vector<int> A; A = createVector(V); int cnt = 0; while(A.size() > 3){ vector<int> B; for(int i=1; i<A.size()-1; i++){ if(A[i-1] < A[i] && A[i] > A[i+1]) B.push_back(A[i]); } A.clear(); A = createVector(B); cnt++; } if(cnt == k){ res++; res %= p; } }while(next_permutation(V.begin(), V.end())); printf("%d\n", res); return 0; }
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 findLog(int n){ int it = 0; n--; while(n > 0){ n = (n >> 1); it++; } return it; } int findPow(int n, int p){ int it = 1; for(int i=0; i<n; i++){ it = (it << 1); it %= p; } return it; } vector<int> createVector(vector<int>& V){ vector<int> A; A.push_back(0); for(int i=0; i<V.size(); i++) A.push_back(V[i]); A.push_back(0); return A; } int main(){ int n, k, p; scanf("%d%d%d", &n, &k, &p); int log = findLog(n); if(log < k){ printf("0\n"); return 0; } if(k == 1){ int pow = findPow(n, p); printf("%d\n", pow); return 0; } vector<int> V; for(int i=1; i<=n; i++) V.push_back(i); int res = 0; do{ vector<int> A; A = createVector(V); int cnt = 0; while(A.size() > 3){ vector<int> B; for(int i=1; i<A.size()-1; i++){ if(A[i-1] < A[i] && A[i] > A[i+1]) B.push_back(A[i]); } A.clear(); A = createVector(B); cnt++; } if(cnt == k){ res++; res %= p; } }while(next_permutation(V.begin(), V.end())); printf("%d\n", res); return 0; } |