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
| #include <cstdio> #include <vector> #include <algorithm> #define pb push_back const int N = 500; int cnt = 0; struct Way{ int val, num, typ; }; std::vector<Way> way; bool e[N + 100][N + 100]; void lk(int x, int y){ e[x][y] = e[y][x] = true; } void mk_bf(int x) { lk(cnt + 1, cnt + 2); lk(cnt + 2, cnt + 3); lk(cnt + 3, cnt + 1); cnt += 3, --x; while (x--) { ++cnt; lk(cnt, cnt - 1), lk(cnt, cnt - 2); } } void mk_q3(int x) { int y = x / 3; for (int i = 1; i <= y; ++i) for (int j = 1; j <= y; ++j) lk(cnt + j, cnt + i + y); for (int i = y + y + 1; i <= x; ++i) for (int j = y + y; j; --j) lk(cnt + j, cnt + i); cnt += x; } void mk_all(int x) { for (int i = 1; i <= x; ++i) for (int j = 1; j < i; ++j) lk(cnt + i, cnt + j); cnt += x; } Way find(int x) { int l = 0, r = way.size() - 1, mid, res = -1; while (l <= r) { mid = (l + r) >> 1; if (way[mid].val <= x) res = mid, l = mid + 1; else r = mid - 1; } return way[res]; } void calc(int x) { if (!x) return ; if (x + 2 < N - cnt) return mk_bf(x); Way t = find(x); if (t.typ == 1) mk_q3(t.num); else mk_all(t.num); calc(x - t.val); } int main() { for (int i = 2; i * 3 <= N; ++i) way.pb({i * i * i, i * 3, 1}); for (int i = 4; i <= N; ++i) way.pb({i * (i - 1) * (i - 2) / 6, i, 2}); std::sort(way.begin(), way.end(), [&](Way x, Way y){ return x.val == y.val ? x.num > y.num : x.val < y.val; }); int n; scanf("%d", &n); calc(n); printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) { for (int j = i + 1; j <= cnt; ++j) printf("%d ", e[i][j]); puts(""); } return 0; }
|