#include<cstdio> #include<algorithm> #include<vector> using namespace std; const int SIZE = 11; char tab[SIZE]; int power(int a, int n, int p) { long long wyn = 1; long tmp = a; for (int i = n; i > 0; i /= 2) { if (i % 2 == 1) { wyn = (wyn * tmp) % p; } tmp = (tmp*tmp) % p; } return wyn; } int main() { for (int i = 0; i < SIZE; ++i) { tab[i] = i; } int N, K, P; scanf("%i %i %i", &N, &K, &P); int prog = 0; int n = N; for(; n > 1; n -= n/2) ++prog; if (K > prog) { printf("0\n"); return 0; } //todo: K == 1 if (K == 1) { printf("%i\n", power(2, N-1, P)); return 0; } int wyn = 0; do { vector<char> v(tab, tab+N); int k = 0; vector<char> tmp; tmp.reserve(SIZE); while (v.size() > 1) { for (int i = 0; i < v.size(); ++i) { bool g = true; if ( (i != 0) && v[i] < v[i-1]) g = false; if ( (i != v.size()-1) && v[i] < v[i+1] ) g = false; if (g) tmp.push_back(v[i]); } v = tmp; tmp.clear(); tmp.reserve(SIZE); k += 1; } //k = kwarkuj(v); if (k == K) wyn = (wyn + 1)%P; } while (next_permutation(tab, tab+N)); printf("%i\n", wyn); 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 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 | #include<cstdio> #include<algorithm> #include<vector> using namespace std; const int SIZE = 11; char tab[SIZE]; int power(int a, int n, int p) { long long wyn = 1; long tmp = a; for (int i = n; i > 0; i /= 2) { if (i % 2 == 1) { wyn = (wyn * tmp) % p; } tmp = (tmp*tmp) % p; } return wyn; } int main() { for (int i = 0; i < SIZE; ++i) { tab[i] = i; } int N, K, P; scanf("%i %i %i", &N, &K, &P); int prog = 0; int n = N; for(; n > 1; n -= n/2) ++prog; if (K > prog) { printf("0\n"); return 0; } //todo: K == 1 if (K == 1) { printf("%i\n", power(2, N-1, P)); return 0; } int wyn = 0; do { vector<char> v(tab, tab+N); int k = 0; vector<char> tmp; tmp.reserve(SIZE); while (v.size() > 1) { for (int i = 0; i < v.size(); ++i) { bool g = true; if ( (i != 0) && v[i] < v[i-1]) g = false; if ( (i != v.size()-1) && v[i] < v[i+1] ) g = false; if (g) tmp.push_back(v[i]); } v = tmp; tmp.clear(); tmp.reserve(SIZE); k += 1; } //k = kwarkuj(v); if (k == K) wyn = (wyn + 1)%P; } while (next_permutation(tab, tab+N)); printf("%i\n", wyn); return 0; } |