#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef pair<int, int> PII; typedef vector<pair<int, int> > VPII; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; //813437586 #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define VAR(v, n) auto v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " /*************************** END OF TEMPLATE ***************************/ const int MAXN = 1005; int n, k, p; int Tab[MAXN+2]; int Zlicz[MAXN+1]; void Sprawdz() { int Wyn = 1; int Temp[2][MAXN+2] = {0}; int Wsk = 1; FOR (i, 1, n) if (Tab[i-1] < Tab[i] && Tab[i] > Tab[i+1]) Temp[0][Wsk] = Tab[i], ++Wsk; while (Wsk != 2) { FOR (i, 1, n) Temp[Wyn%2][i] = 0; int Last = Wsk-1; Wsk = 1; int x = (Wyn-1) % 2; FOR (i, 1, Last) if (Temp[x][i-1] < Temp[x][i] && Temp[x][i] > Temp[x][i+1]) Temp[Wyn%2][Wsk] = Temp[x][i], ++Wsk; ++Wyn; } ++Zlicz[Wyn]; Zlicz[Wyn] %= p; // if (Wyn == 2) /// { // FOR (i, 1, MAXN) // printf ("%d ", Tab[i]); // printf ("\n"); // } // printf ("Zlicz[%d]++\n", Wyn); } int main() { scanf ("%d%d%d", &n, &k, &p); FOR (i, 1, n) Tab[i] = i; Sprawdz(); while (next_permutation(Tab+1, Tab+n+1)) Sprawdz(); printf ("%d", Zlicz[k]); // FOR (i, 1, MAXN) printf ("Zlicz[%d] = %d\n", i, Zlicz[i]); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef pair<int, int> PII; typedef vector<pair<int, int> > VPII; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; //813437586 #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define VAR(v, n) auto v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " /*************************** END OF TEMPLATE ***************************/ const int MAXN = 1005; int n, k, p; int Tab[MAXN+2]; int Zlicz[MAXN+1]; void Sprawdz() { int Wyn = 1; int Temp[2][MAXN+2] = {0}; int Wsk = 1; FOR (i, 1, n) if (Tab[i-1] < Tab[i] && Tab[i] > Tab[i+1]) Temp[0][Wsk] = Tab[i], ++Wsk; while (Wsk != 2) { FOR (i, 1, n) Temp[Wyn%2][i] = 0; int Last = Wsk-1; Wsk = 1; int x = (Wyn-1) % 2; FOR (i, 1, Last) if (Temp[x][i-1] < Temp[x][i] && Temp[x][i] > Temp[x][i+1]) Temp[Wyn%2][Wsk] = Temp[x][i], ++Wsk; ++Wyn; } ++Zlicz[Wyn]; Zlicz[Wyn] %= p; // if (Wyn == 2) /// { // FOR (i, 1, MAXN) // printf ("%d ", Tab[i]); // printf ("\n"); // } // printf ("Zlicz[%d]++\n", Wyn); } int main() { scanf ("%d%d%d", &n, &k, &p); FOR (i, 1, n) Tab[i] = i; Sprawdz(); while (next_permutation(Tab+1, Tab+n+1)) Sprawdz(); printf ("%d", Zlicz[k]); // FOR (i, 1, MAXN) printf ("Zlicz[%d] = %d\n", i, Zlicz[i]); } |