#P1214. [USACO1.4] 等差数列 Arithmetic Progressions

[USACO1.4] 等差数列 Arithmetic Progressions

Description

An arithmetic progression is a sequence that can be written as a,a+b,a+2b,,a+nb (nN)a, a+b, a+2b, \dots ,a+nb\space (n \in \mathbb N).

In this problem, aa is a non-negative integer and bb is a positive integer. Write a program to find arithmetic progressions of length nn within the set of bisquares:

$$\{ x | x = p^2 + q^2 \wedge p,q \in \mathbb N \cap [0,m]\}$$

Input Format

The first line contains a positive integer nn, the required length of the progression. The second line contains a non-negative integer mm, the upper bound for p,qp, q.

Output Format

If no progression is found, output NONE.

If found, output one or more lines, each containing two integers: a,ba, b.

These lines should be sorted in ascending order by bb as the primary key and by aa as the secondary key.

There will be no more than 10,000 such arithmetic progressions.

5
7

1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24

Hint

Constraints For 100% of the testdata, 3n253 \le n \le 25, 0m2500 \le m \le 250.

Problem translation from NOCOW.

USACO Training Section 1.4.

Translated by ChatGPT 5