#define _USE_MATH_DEFINES/////////////////////////////////////////////////////
#include <bits/stdc++.h>//////////////////////////////////////////////////////
#ifdef LOC////////////////////////////////////////////////////////////////////
#include "debuglib.h"/////////////////////////////////////////////////////////
#else/////////////////////////////////////////////////////////////////////////
#define deb(...)//////////////////////////////////////////////////////////////
#define DBP(...)//////////////////////////////////////////////////////////////
#endif////////////////////////////////////////////////////////////////////////
#define x first///////////////////////////////////////////////////////////////
#define y second//////////////////////////////////////////////////////////////
#define mp make_pair//////////////////////////////////////////////////////////
#define pb push_back//////////////////////////////////////////////////////////
#define sz(x)int((x).size())//////////////////////////////////////////////////
#define each(a,x)for(auto&a:(x))//////////////////////////////////////////////
#define all(x)(x).begin(),(x).end()///////////////////////////////////////////
#define rep(i,b,e)for(int i=(b);i<(e);i++)////////////////////////////////////
using namespace std;using namespace rel_ops;using ll=int64_t;using Pii=pair///
<int,int>;using ull=uint64_t;using Vi=vector<int>;void run();int main(){cin.//
sync_with_stdio(0);cin.tie(0);cout<<fixed<<setprecision(20);run();return 0;}//
//--------------------------------------------------------------------------//
int buf1[100], buf2[100];
int level(const Vi& perm) {
int *cur = buf1, *prev = buf2;
int n = sz(perm);
memcpy(prev, perm.data(), sz(perm)*sizeof(int));
for (int j = 0;; j++) {
if (n <= 1) return j;
int k = 0;
if (prev[0] > prev[1]) {
cur[k++] = prev[0];
}
rep(i, 1, n-1) {
if (prev[i] > prev[i-1] && prev[i] > prev[i+1]) {
cur[k++] = prev[i++];
}
}
if (prev[n-1] > prev[n-2]) {
cur[k++] = prev[n-1];
}
swap(prev, cur);
n = k;
}
}
int solve(int n, int k) {
int ans = 0;
Vi perm(n);
iota(all(perm), 1);
do {
if (level(perm) == k) ans++;
} while (next_permutation(all(perm)));
return ans;
}
void run() {
int n, k, p; cin >> n >> k >> p;
cout << (solve(n, k) % p) << 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 | #define _USE_MATH_DEFINES///////////////////////////////////////////////////// #include <bits/stdc++.h>////////////////////////////////////////////////////// #ifdef LOC//////////////////////////////////////////////////////////////////// #include "debuglib.h"///////////////////////////////////////////////////////// #else///////////////////////////////////////////////////////////////////////// #define deb(...)////////////////////////////////////////////////////////////// #define DBP(...)////////////////////////////////////////////////////////////// #endif//////////////////////////////////////////////////////////////////////// #define x first/////////////////////////////////////////////////////////////// #define y second////////////////////////////////////////////////////////////// #define mp make_pair////////////////////////////////////////////////////////// #define pb push_back////////////////////////////////////////////////////////// #define sz(x)int((x).size())////////////////////////////////////////////////// #define each(a,x)for(auto&a:(x))////////////////////////////////////////////// #define all(x)(x).begin(),(x).end()/////////////////////////////////////////// #define rep(i,b,e)for(int i=(b);i<(e);i++)//////////////////////////////////// using namespace std;using namespace rel_ops;using ll=int64_t;using Pii=pair/// <int,int>;using ull=uint64_t;using Vi=vector<int>;void run();int main(){cin.// sync_with_stdio(0);cin.tie(0);cout<<fixed<<setprecision(20);run();return 0;}// //--------------------------------------------------------------------------// int buf1[100], buf2[100]; int level(const Vi& perm) { int *cur = buf1, *prev = buf2; int n = sz(perm); memcpy(prev, perm.data(), sz(perm)*sizeof(int)); for (int j = 0;; j++) { if (n <= 1) return j; int k = 0; if (prev[0] > prev[1]) { cur[k++] = prev[0]; } rep(i, 1, n-1) { if (prev[i] > prev[i-1] && prev[i] > prev[i+1]) { cur[k++] = prev[i++]; } } if (prev[n-1] > prev[n-2]) { cur[k++] = prev[n-1]; } swap(prev, cur); n = k; } } int solve(int n, int k) { int ans = 0; Vi perm(n); iota(all(perm), 1); do { if (level(perm) == k) ans++; } while (next_permutation(all(perm))); return ans; } void run() { int n, k, p; cin >> n >> k >> p; cout << (solve(n, k) % p) << endl; } |
English