#include #include using namespace std; int index(int max, int a, int b) { if (a >= max) { return 0; } return a * (a + 1) / 2 + b; } vector get_missing(int max) { vector result; vector present(max * (max + 1) / 2); present[index(max, 1, 0)] = true; for (int a = 1; a < max; ++a) { bool any_present = false; for (int b = 0; b <= a; ++b) { if (!present[index(max, a, b)]) { continue; } any_present = true; { /* add single edge */ int na = a + b; int nb = a; present[index(max, na, nb)] = true; } for (int a1 = 1; a1 <= a && a1 * a < max; a1++) { for (int b1 = 0; b1 <= a1 && (a != a1 || b1 <= b); ++b1) { if (!present[index(max, a1, b1)]) { continue; } int na = (a + b) * (a1 + b1) - b * b1; int nb = b * b1; if (na >= max) { break; } /* add glued trees */ present[index(max, na, nb)] = true; } } } if (!any_present) { result.push_back(a); } } return result; } int main(int , const char** ) { auto res = get_missing(1000); for (const auto& a : res) { cout << a << ", "; } cout << endl; }