#include <bits/stdc++.h> #include <sys/time.h> #define ST first #define ND second #define pb push_back #define rep(i, a, n) for (int i = a; i <= n; i++) #define per(i, a, n) for (int i = a; i >= n; i--) #define SZ(x) ((int)x.size()) #define ALL(x) (x.begin(), x.end()) #define pii pair <int, int> #define ll long long #define LD long double using namespace std; int n, m, k, nxt[1000005], pocz, it, ans, ded[1000005], ost, zle, zm, wypisz; vector <int> v; ll pot(ll n, ll k) { ll ans = 1; while(k>0) { if (k%2) { ans *= n; ans %= m; } n*=n; n %= m; k/=2; } return ans; } main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>k>>m; if (n==1 && k==1) { cout<<0<<endl; return 0; } //cin>>n; //cin>>n>>k; //k = 4; //m = 1e9+7; if (k==1) { cout<<pot(2, n-1)<<endl; return 0; } //n = 5; //k = 3; //m = 412412; for (int i = 1; i <= n; i++) { v.pb(i); } do { zm = 0; zle = 0; wypisz=0; rep(i, 0, n-1) { nxt[i]=i+1; ded[i]=0; } pocz = 0; rep(i, 1, k) { it = pocz; while(it!=n) { if ((it!=pocz && v[it]<v[ost]) || (nxt[it]!=n && v[it]<v[nxt[it]])) { ded[it]=1; } ost = it; it = nxt[it]; } int zp = 0; zm = 0; rep(j, 0, n-1) { if (ded[j]==1) {zm++; continue;} if (zp==0) { zp=1; pocz = j; ost = j; } else { nxt[ost] = j; ost = j; } } nxt[ost]=n; if (zm==n-1 && k!=i) zle = 1; } if (!zle && zm==n-1) { ans++; ans%=m; } } while (next_permutation(v.begin(), v.end())); cout<<ans%m<<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 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 102 103 104 105 106 107 108 109 110 111 | #include <bits/stdc++.h> #include <sys/time.h> #define ST first #define ND second #define pb push_back #define rep(i, a, n) for (int i = a; i <= n; i++) #define per(i, a, n) for (int i = a; i >= n; i--) #define SZ(x) ((int)x.size()) #define ALL(x) (x.begin(), x.end()) #define pii pair <int, int> #define ll long long #define LD long double using namespace std; int n, m, k, nxt[1000005], pocz, it, ans, ded[1000005], ost, zle, zm, wypisz; vector <int> v; ll pot(ll n, ll k) { ll ans = 1; while(k>0) { if (k%2) { ans *= n; ans %= m; } n*=n; n %= m; k/=2; } return ans; } main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>k>>m; if (n==1 && k==1) { cout<<0<<endl; return 0; } //cin>>n; //cin>>n>>k; //k = 4; //m = 1e9+7; if (k==1) { cout<<pot(2, n-1)<<endl; return 0; } //n = 5; //k = 3; //m = 412412; for (int i = 1; i <= n; i++) { v.pb(i); } do { zm = 0; zle = 0; wypisz=0; rep(i, 0, n-1) { nxt[i]=i+1; ded[i]=0; } pocz = 0; rep(i, 1, k) { it = pocz; while(it!=n) { if ((it!=pocz && v[it]<v[ost]) || (nxt[it]!=n && v[it]<v[nxt[it]])) { ded[it]=1; } ost = it; it = nxt[it]; } int zp = 0; zm = 0; rep(j, 0, n-1) { if (ded[j]==1) {zm++; continue;} if (zp==0) { zp=1; pocz = j; ost = j; } else { nxt[ost] = j; ost = j; } } nxt[ost]=n; if (zm==n-1 && k!=i) zle = 1; } if (!zle && zm==n-1) { ans++; ans%=m; } } while (next_permutation(v.begin(), v.end())); cout<<ans%m<<endl; } |