#P2765. 魔术球问题

    ID: 1770 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>网络流Special JudgeO2优化最大流网络流 24 题

魔术球问题

Description

Assume there are nn rods. We insert balls labeled 11, 22, 33, ... into these nn rods in order, following the rules below:

  1. Each time, you can only place a ball on the top of some rod.
  2. On the same rod, the sum of the labels of any 22 adjacent balls is a perfect square.

Design an algorithm to compute the maximum number of balls that can be placed on nn rods. For example, with 44 rods, at most 1111 balls can be placed.

Given nn, compute the maximum number of balls that can be placed on nn rods.

Input Format

There is only one line containing an integer nn, representing the number of rods.

Output Format

This problem has a Special Judge.

Please output the maximum number of balls that can be placed on nn rods, along with a corresponding placement scheme.

The first line is the number of balls.

The next nn lines each contain several integers, representing the labels of the balls on one rod, separated by a single space.

4
11
1 8
2 7 9
3 6 10
4 5 11

Hint

Constraints

For 100%100\% of the testdata, it is guaranteed that 1n551 \leq n \leq 55.

Translated by ChatGPT 5