#include <tuple> #include <vector> #include <stdio.h> #include <iostream> #include <algorithm> #include <functional> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vvi = vector<vi>; using vpii = vector<pii>; using vpll = vector<pll>; using vll = vector<ll>; using vull = vector<ull>; #define ever (;;) #define f first #define s second #define pb emplace_back #define sz size() #define graph vector<vertex> #define deg(X) G[X].e.size() #define INF 1234567890 #define INFll 2000000000000000000 #define maxe(X, Y) if((Y) > (X)) (X) = (Y) #define mine(X, Y) if((Y) < (X)) (X) = (Y) #define rep(i, begin, end) for(__typeof(end) i = (begin); i < (end); ++i) #define repr(i, begin, end) for(__typeof(end) i = (begin)-1; i >= (end); --i) #define bend(X) X.begin(), X.end() ll mod; int n, k; void in() { scanf("%d %d %lld", &n, &k, &mod); } int getTime(vi T) { int R = 0; while(T.sz > 1) { vb mark(T.sz, 0); ++R; rep(i, 0, T.sz) if((i>0 && T[i-1] > T[i]) || (i+1<T.sz && T[i+1] > T[i])) mark[i] = 1; int erased = 0; rep(i, 0, T.sz) while(i+erased < mark.sz && mark[i+erased]) { T.erase(T.begin()+i); ++erased; } } return R; } int getLog(int x) { int R = 0; while(x > 0) x /= 2, ++R; return R; } ll fastPow(ll x, ll p) { if(p == 0) return 1; if(p&1) return x*fastPow(x,p-1)%mod; ll a = fastPow(x,p/2); return a*a%mod; } int main() { in(); vi T(n); rep(i, 0, n) T[i] = i+1; ll R = 0; if(n > 8 && k > getLog(n)) { printf("0\n"); return 0; } else if(k == 1) { printf("%lld\n", fastPow(2, n-1)%mod); return 0; } do { if(getTime(T) == k) ++R; R %= mod; } while(next_permutation(bend(T))); printf("%lld\n", R); 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 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 | #include <tuple> #include <vector> #include <stdio.h> #include <iostream> #include <algorithm> #include <functional> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vvi = vector<vi>; using vpii = vector<pii>; using vpll = vector<pll>; using vll = vector<ll>; using vull = vector<ull>; #define ever (;;) #define f first #define s second #define pb emplace_back #define sz size() #define graph vector<vertex> #define deg(X) G[X].e.size() #define INF 1234567890 #define INFll 2000000000000000000 #define maxe(X, Y) if((Y) > (X)) (X) = (Y) #define mine(X, Y) if((Y) < (X)) (X) = (Y) #define rep(i, begin, end) for(__typeof(end) i = (begin); i < (end); ++i) #define repr(i, begin, end) for(__typeof(end) i = (begin)-1; i >= (end); --i) #define bend(X) X.begin(), X.end() ll mod; int n, k; void in() { scanf("%d %d %lld", &n, &k, &mod); } int getTime(vi T) { int R = 0; while(T.sz > 1) { vb mark(T.sz, 0); ++R; rep(i, 0, T.sz) if((i>0 && T[i-1] > T[i]) || (i+1<T.sz && T[i+1] > T[i])) mark[i] = 1; int erased = 0; rep(i, 0, T.sz) while(i+erased < mark.sz && mark[i+erased]) { T.erase(T.begin()+i); ++erased; } } return R; } int getLog(int x) { int R = 0; while(x > 0) x /= 2, ++R; return R; } ll fastPow(ll x, ll p) { if(p == 0) return 1; if(p&1) return x*fastPow(x,p-1)%mod; ll a = fastPow(x,p/2); return a*a%mod; } int main() { in(); vi T(n); rep(i, 0, n) T[i] = i+1; ll R = 0; if(n > 8 && k > getLog(n)) { printf("0\n"); return 0; } else if(k == 1) { printf("%lld\n", fastPow(2, n-1)%mod); return 0; } do { if(getTime(T) == k) ++R; R %= mod; } while(next_permutation(bend(T))); printf("%lld\n", R); return 0; } |