#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto &i : a) #define SZ(i) ((int)(i).size()) #define X first #define Y second #define PR std::pair #define PII std::pair<int, int> #define MP std::make_pair #define ll long long #define VI std::vector<int> int n, k, mod, ans; int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> k >> mod; if(k == 1){ if(n == 1) std::cout << 0; else{ ans = 1; FOR(i, n-1) ans = (2ll*ans)%mod; std::cout << ans; } return 0; } VI A; FOR(i, n) A.push_back(i); do{ int odp = 0; VI vec = A; while(SZ(vec) > 1){ VI next; FOR(i, SZ(vec)){ if((i+1 < SZ(vec) && vec[i] < vec[i+1]) || (i-1 >= 0 && vec[i] < vec[i-1])) continue; next.push_back(vec[i]); } vec = next; odp++; } if(odp == k) ans++; }while(std::next_permutation(A.begin(), A.end())); std::cout << ans%mod; 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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto &i : a) #define SZ(i) ((int)(i).size()) #define X first #define Y second #define PR std::pair #define PII std::pair<int, int> #define MP std::make_pair #define ll long long #define VI std::vector<int> int n, k, mod, ans; int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> k >> mod; if(k == 1){ if(n == 1) std::cout << 0; else{ ans = 1; FOR(i, n-1) ans = (2ll*ans)%mod; std::cout << ans; } return 0; } VI A; FOR(i, n) A.push_back(i); do{ int odp = 0; VI vec = A; while(SZ(vec) > 1){ VI next; FOR(i, SZ(vec)){ if((i+1 < SZ(vec) && vec[i] < vec[i+1]) || (i-1 >= 0 && vec[i] < vec[i-1])) continue; next.push_back(vec[i]); } vec = next; odp++; } if(odp == k) ans++; }while(std::next_permutation(A.begin(), A.end())); std::cout << ans%mod; return 0; } |