#include <bits/stdc++.h> #define vi vector<int> #define pb push_back #define sz(x) int(x.size()) using namespace std; int sp(vi tab) { int ans = 0; while(sz(tab) != 1) { vi nowy; if(tab[1] < tab[0])nowy.pb(tab[0]); for(int i = 1; i < sz(tab) - 1; ++i) { if(tab[i - 1] > tab[i] || tab[i + 1] > tab[i]) { } else { nowy.pb(tab[i]); } } if(tab[sz(tab) - 1] > tab[sz(tab) - 2])nowy.pb(tab[sz(tab) - 1]); swap(tab, nowy); ++ans; } return ans; } int main() { int N, K; long long P, ans = 0; cin >> N >> K >> P; vi tab; for(int i = 1; i <= N; ++i)tab.pb(i); do { if(sp(tab) == K)++ans; }while(next_permutation(tab.begin(), tab.end())); cout << ans % P; 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 | #include <bits/stdc++.h> #define vi vector<int> #define pb push_back #define sz(x) int(x.size()) using namespace std; int sp(vi tab) { int ans = 0; while(sz(tab) != 1) { vi nowy; if(tab[1] < tab[0])nowy.pb(tab[0]); for(int i = 1; i < sz(tab) - 1; ++i) { if(tab[i - 1] > tab[i] || tab[i + 1] > tab[i]) { } else { nowy.pb(tab[i]); } } if(tab[sz(tab) - 1] > tab[sz(tab) - 2])nowy.pb(tab[sz(tab) - 1]); swap(tab, nowy); ++ans; } return ans; } int main() { int N, K; long long P, ans = 0; cin >> N >> K >> P; vi tab; for(int i = 1; i <= N; ++i)tab.pb(i); do { if(sp(tab) == K)++ans; }while(next_permutation(tab.begin(), tab.end())); cout << ans % P; return 0; } |