RoAlgo PreOJI 2024 XI-XII | Morcovi

This was the problem page during the contest. Access the current page here.
Time limit: 1s Memory limit: 256MB Input: morcovi.in Output: morcovi.out

Task

Andrei has nn carrots, conveniently numbered from 11 to nn, and a favorite number kk.

Two carrots aa and bb are compatible if gcd(a,b)+k=lcm(a,b)gcd(a,b) + k = lcm(a,b), where:

  • gcd(a,b)gcd(a,b) represents the greatest common divisor of the numbers aa and bb;
  • lcm(a,b)lcm(a,b) represents the least common multiple of the numbers aa and bb.

In how many ways can Andrei choose two carrots aa and bb (1a<bn1 \le a < b \le n) such that they are compatible?

Input data

Each test contains multiple test cases. On the first line of the input file morcovi.in will be the number of test cases tt.

Each test case consists of two numbers nn and kk — the number of Andrei's carrots and Andrei's favorite number, respectively.

Output data

For each test case, print the number of ways Andrei can choose two carrots aa and bb (1a<bn1 \le a < b \le n) such that they are compatible in the output file morcovi.out.

Constraints and clarifications

  • 1t1001 \le t \le 100
  • 1n,k1091 \le n,k \le 10^9
  • To obtain points for a particular subtask, at least one submitted source code will have to pass all the tests of that subtask.
# Points Constraints
0 0 Example
1 25 n,k300n,k \le 300
2 21 n,k3000n,k \le 3\,000
3 18 n,k50000n,k \le 50\,000
4 12 t=1t=1
5 24 No additional constraints

Example

morcovi.in

5
1000000000 1
30 6
6 10
420 69
123456789 987654321

morcovi.out

1
4
1
8
54

Explanation

  • In the first test case, the only way to choose the carrots is a=1a=1 and b=2b=2.
  • In the second test case, the 44 ways to choose the carrots are: (1,7)(1,7), (2,8)(2,8), (3,9)(3,9), and (6,12)(6,12).
  • In the third test case, the only way to choose the carrots is a=4a=4 and b=6b=6.
  • In the fourth test case, the 88 ways to choose the carrots are: (1,70)(1,70), (2,35)(2,35), (3,72)(3,72), (5,14)(5,14), (7,10)(7,10), (9,24)(9,24), (23,92)(23,92), and (69,138)(69,138).

Log in or sign up to be able to send submissions!