#include <iostream>
#include <queue>
#include <stack>
struct Segment {
Segment(int pozycja, int dlugosc, int depth)
: pozycja(pozycja), dlugosc(dlugosc), depth(depth) {}
int pozycja;
int dlugosc;
int depth;
};
int N = 0;
int M = 0;
int P = 0;
int SUM = 0;
int main() {
std::cin >> N;
std::cin >> M;
std::cin >> P;
//
std::queue<Segment> segmenty;
for (int i = 0; i < M; ++i) {
for (int j = 1; j < M - i + 1; ++j) {
segmenty.push(Segment(i, j, 1));
}
}
if (N == 1) {
std::cout << segmenty.size() << std::endl;
return 0;
}
while (segmenty.empty() == false) {
Segment s = segmenty.front();
segmenty.pop();
for (int i = 0; i < s.pozycja + s.dlugosc; ++i) {
for (int j = std::max(s.pozycja + 1 - i, 1); j < M - i + 1; ++j) {
if (s.depth + 1 == N) {
SUM = (SUM + 1) % P;
} else {
segmenty.push(Segment(i, j, s.depth + 1));
}
}
}
}
std::cout << SUM << 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 | #include <iostream> #include <queue> #include <stack> struct Segment { Segment(int pozycja, int dlugosc, int depth) : pozycja(pozycja), dlugosc(dlugosc), depth(depth) {} int pozycja; int dlugosc; int depth; }; int N = 0; int M = 0; int P = 0; int SUM = 0; int main() { std::cin >> N; std::cin >> M; std::cin >> P; // std::queue<Segment> segmenty; for (int i = 0; i < M; ++i) { for (int j = 1; j < M - i + 1; ++j) { segmenty.push(Segment(i, j, 1)); } } if (N == 1) { std::cout << segmenty.size() << std::endl; return 0; } while (segmenty.empty() == false) { Segment s = segmenty.front(); segmenty.pop(); for (int i = 0; i < s.pozycja + s.dlugosc; ++i) { for (int j = std::max(s.pozycja + 1 - i, 1); j < M - i + 1; ++j) { if (s.depth + 1 == N) { SUM = (SUM + 1) % P; } else { segmenty.push(Segment(i, j, s.depth + 1)); } } } } std::cout << SUM << std::endl; return 0; } |
English