#include<bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = a; i <= b; i++) #define REP(i, a) FOR(i, 0, a - 1) #define ST first #define ND second #define V vector #define RS resize #define EB emplace_back #define ALL(a) a.begin(), a.end() #define S(a) (int)a.size() template<class T> void db(T a) { cerr << a; } template<class L, class R> void db(pair<L, L> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; } template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; } template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; } template<class T, class... TS> void dump(const char *s, T a, TS... x) { while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); } #ifdef DEBUG #define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); #else #define DB(...) #endif using LL = long long; using PII = pair<int, int>; using VI = V<int>; using VLL = V<LL>; using VVI = V<VI>; using VPII = V<PII>; int ans = 0; VI p; V<bool> used; int complexity() { int c = 0; VI copy = p; while(S(p) != 1) { c++; V<bool> to_del(S(p)); REP(i, S(p)) { int l = (i != 0 ? p[i - 1] : -1); int r = (i != S(p) - 1 ? p[i + 1] : -1); if(l > p[i] || p[i] < r) to_del[i] = 1; } auto pos = p.begin(); REP(i, S(to_del)) { if(to_del[i]) pos = p.erase(pos); else pos = next(pos); } } p = copy; return c; } void check_all(int n, int k, int l = 0) { if(l == n) { if(complexity() == k) ans++; return; } REP(i, n) { if(!used[i]) { p.EB(i); used[i] = 1; check_all(n, k, l + 1); p.pop_back(); used[i] = 0; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; used.RS(n); check_all(n, k); cout << ans % m << "\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 | #include<bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = a; i <= b; i++) #define REP(i, a) FOR(i, 0, a - 1) #define ST first #define ND second #define V vector #define RS resize #define EB emplace_back #define ALL(a) a.begin(), a.end() #define S(a) (int)a.size() template<class T> void db(T a) { cerr << a; } template<class L, class R> void db(pair<L, L> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; } template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; } template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; } template<class T, class... TS> void dump(const char *s, T a, TS... x) { while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); } #ifdef DEBUG #define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); #else #define DB(...) #endif using LL = long long; using PII = pair<int, int>; using VI = V<int>; using VLL = V<LL>; using VVI = V<VI>; using VPII = V<PII>; int ans = 0; VI p; V<bool> used; int complexity() { int c = 0; VI copy = p; while(S(p) != 1) { c++; V<bool> to_del(S(p)); REP(i, S(p)) { int l = (i != 0 ? p[i - 1] : -1); int r = (i != S(p) - 1 ? p[i + 1] : -1); if(l > p[i] || p[i] < r) to_del[i] = 1; } auto pos = p.begin(); REP(i, S(to_del)) { if(to_del[i]) pos = p.erase(pos); else pos = next(pos); } } p = copy; return c; } void check_all(int n, int k, int l = 0) { if(l == n) { if(complexity() == k) ans++; return; } REP(i, n) { if(!used[i]) { p.EB(i); used[i] = 1; check_all(n, k, l + 1); p.pop_back(); used[i] = 0; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; used.RS(n); check_all(n, k); cout << ans % m << "\n"; } |