I found an interesting problem on https://www.codechef.com/problems/GUESSNUM:
Chef is playing a game with his brother Chefu. He asked Chefu to choose a positive integer $N$, multiply it by a given integer $A$, then choose a divisor of $N$ (possibly $N$ itself) and add it to the product. Let's denote the resulting integer by $M$; more formally, $M = A \cdot N + d$, where $d$ is some divisor of $N$.
Chefu told Chef the value of $M$ and now, Chef should guess $N$. Help him find all values of $N$ which Chefu could have chosen.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $A$ and $M$.

-----Output-----
For each test case, print two lines. The first line should contain a single integer $C$ denoting the number of possible values of $N$. The second line should contain $C$ space-separated integers denoting all possible values of $N$ in increasing order.
It is guaranteed that the sum of $C$ over all test cases does not exceed $10^7$.

-----Constraints-----
- $1 \le T \le 100$
- $2 \le M \le 10^{10}$
- $1 \le A < M$

-----Subtasks-----
Subtask #1 (50 points):
- $M \le 10^6$
Subtask #2 (50 points): original constraints

-----Example Input-----
3
3 35
5 50
4 65

-----Example Output-----
1
10
0

3
13 15 16

I tried it in Python, but could not do it. Can you solve it?
T = int(input())
import math
for i in range(T):
 list = []
 A,M = input().split()
 A = int(A)
 M = int(M)
 L1 = M//A
 L2 = math.ceil(M/(A+1))
 while L2 <= L1 :
  N = L2
  if M - N*A !=0 and N%(M - N*A) == 0 :
   list.append(L2)
  L2 += 1

 print(len(list))
 if len(list) > 0:
  for j in range(len(list)-1):
   print(list[j],end=" ")
  print(list[len(list)-1])
 else:
  print(" ")