#include<bits/stdc++.h> using namespace std; #define sz(x) (int)(x).size() int n,k,mod; int calc(vector<int> v) { vector<int> pom; int ret = 0; while(sz(v) > 1) { for(int i = 0; i < sz(v); ++i) { if((i > 0 && v[i] < v[i-1]) || (i <= sz(v)-2 && v[i] < v[i+1])) continue; pom.push_back(v[i]); } ret++; v = pom; pom.clear(); } return ret; } int main() { scanf("%d%d%d",&n,&k,&mod); vector<int> t; for(int i = 1; i <= n; ++i) t.push_back(i); int res = 0; for(;;) { if(calc(t) == k) res++; if(!next_permutation(t.begin(), t.end())) break; } printf("%d\n",res%mod); }
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 | #include<bits/stdc++.h> using namespace std; #define sz(x) (int)(x).size() int n,k,mod; int calc(vector<int> v) { vector<int> pom; int ret = 0; while(sz(v) > 1) { for(int i = 0; i < sz(v); ++i) { if((i > 0 && v[i] < v[i-1]) || (i <= sz(v)-2 && v[i] < v[i+1])) continue; pom.push_back(v[i]); } ret++; v = pom; pom.clear(); } return ret; } int main() { scanf("%d%d%d",&n,&k,&mod); vector<int> t; for(int i = 1; i <= n; ++i) t.push_back(i); int res = 0; for(;;) { if(calc(t) == k) res++; if(!next_permutation(t.begin(), t.end())) break; } printf("%d\n",res%mod); } |