#include <stdio.h> #include <string.h> #include <iostream> namespace { const int MAX = 1000; long long gCount = 0; void process(const int K, const int P, int const* const p, int size) { int A[MAX]; memcpy(A, p, static_cast<size_t>(size) * sizeof(int)); int counter = 0; while (size > 1) { if (counter > K) { return; } int new_size = 0; for (int i = 0; i < size; ++i) { if (i == 0) { if (A[i] > A[i + 1]) { new_size++; } } else if (i == size - 1) { if (A[i - 1] < A[i]) { A[new_size] = A[i]; new_size++; } } else { if (A[i - 1] < A[i] && A[i] > A[i + 1]) { A[new_size] = A[i]; new_size++; } } } counter++; size = new_size; } if (counter == K) { gCount++; if (gCount >= P) { gCount = 0; } } } void gen_permutations(const int N, const int K, const int P) { int A[MAX]; int C[MAX] = {}; for (int i = 0; i < N; ++i) { A[i] = i + 1; } process(K, P, A, N); int i = 0; while (i < N) { if (C[i] < i) { if (i % 2 == 0) { std::swap(A[0], A[i]); } else { std::swap(A[C[i]], A[i]); } process(K, P, A, N); C[i]++; i = 0; } else { C[i] = 0; i++; } } } } // namespace anonymous int main() { int N = 0; int K = 0; int P = 0; std::cin >> N; std::cin >> K; std::cin >> P; fprintf(stderr, "N: [%d], K: [%d], P: [%d]\n", N, K, P); if (N == 1 || K > (N + 1) / 2) { std::cout << 0 << std::endl; return 0; } gen_permutations(N, K, P); std::cout << gCount << std::endl; 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 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 | #include <stdio.h> #include <string.h> #include <iostream> namespace { const int MAX = 1000; long long gCount = 0; void process(const int K, const int P, int const* const p, int size) { int A[MAX]; memcpy(A, p, static_cast<size_t>(size) * sizeof(int)); int counter = 0; while (size > 1) { if (counter > K) { return; } int new_size = 0; for (int i = 0; i < size; ++i) { if (i == 0) { if (A[i] > A[i + 1]) { new_size++; } } else if (i == size - 1) { if (A[i - 1] < A[i]) { A[new_size] = A[i]; new_size++; } } else { if (A[i - 1] < A[i] && A[i] > A[i + 1]) { A[new_size] = A[i]; new_size++; } } } counter++; size = new_size; } if (counter == K) { gCount++; if (gCount >= P) { gCount = 0; } } } void gen_permutations(const int N, const int K, const int P) { int A[MAX]; int C[MAX] = {}; for (int i = 0; i < N; ++i) { A[i] = i + 1; } process(K, P, A, N); int i = 0; while (i < N) { if (C[i] < i) { if (i % 2 == 0) { std::swap(A[0], A[i]); } else { std::swap(A[C[i]], A[i]); } process(K, P, A, N); C[i]++; i = 0; } else { C[i] = 0; i++; } } } } // namespace anonymous int main() { int N = 0; int K = 0; int P = 0; std::cin >> N; std::cin >> K; std::cin >> P; fprintf(stderr, "N: [%d], K: [%d], P: [%d]\n", N, K, P); if (N == 1 || K > (N + 1) / 2) { std::cout << 0 << std::endl; return 0; } gen_permutations(N, K, P); std::cout << gCount << std::endl; return 0; } |