#K37522. King's Road Construction
King's Road Construction
King's Road Construction
The kingdom has n towns, numbered from 1 to n. The king decreed that roads should be built between some pairs of towns. Each road connects exactly two distinct towns and no two roads connect the same pair of towns. However, due to a peculiar rule, a road can only be built between towns a and b if the product of their numbers, \(a \times b\), is divisible by a given integer \(k\) (i.e. \(a \times b \equiv 0 \pmod{k}\)).
Your task is to determine all such pairs of towns. If at least one qualifying road exists, output each road as a pair of town numbers on its own line in the order they are found (by increasing a and then b). If no road satisfies the criterion, simply output -1
.
Note: The input will be given via standard input and the output should be printed to standard output.
inputFormat
The input consists of a single line containing two integers n
and k
separated by a space:
n
(1 ≤ n ≤ 105) represents the number of towns.k
(1 ≤ k ≤ 105) is the divisor for the product condition.
outputFormat
If there exists at least one valid road, print each road on a separate line as two space-separated integers representing the pair of towns. The roads should be printed in the order they are discovered (first by the smaller town number and then by the larger). If no valid roads are found, print -1
.
5 2
1 2
1 4
2 3
2 4
3 4
</p>