P2640 Mysterious Magnet

Background

On the distant continent of Arad, there exists a mysterious magnet crafted by the Demon Emperor.

Description

1. In a one-dimensional coordinate system, its magnetic force reaches its maximum only at positions whose coordinates are prime numbers (regardless of the coordinate’s magnitude, as long as the coordinate is prime, the magnetic force is equally strong). 2. If two magnets are at distance $k$, their destructive power reaches the peak of the current magnetic force. Clearly, the maximum destructive power between two magnets depends on the magnetic force and the distance between the magnets. Given a one-dimensional coordinate line of length at most $n$, find all pairs of coordinates whose magnets achieve the maximum destructive power, i.e., all pairs of primes at distance $k$ within $[1, n]$.

Input Format

Two positive integers $n, k$.

Output Format

Output all pairs of primes $(p, q)$ such that $q - p = k$ and $q \leq n$ (equivalently, all pairs $(p, p + k)$ with $p \geq 2$ and $p + k \leq n$). Output one pair per line, with a single space between the two numbers. If no prime pair is found, output `empty`.

Explanation/Hint

For $100\%$ of the testdata, $1 \leq k \leq n \leq 10^4$. Translated by ChatGPT 5