So, I managed to write the solution that should be correct but I got TE.
Code: Select all
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Main {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int k = scanner.nextInt();
Set<Integer> dividers = new HashSet<>();
int sqrt = (int) Math.sqrt(n);
for (int divider = 1; divider <= sqrt; divider++) {
if (n % divider == 0) {
if (divider % k != 0) {
dividers.add(divider);
}
int otherDivider = n / divider;
if (otherDivider % k != 0) {
dividers.add(otherDivider);
}
}
}
System.out.println(dividers.stream().mapToInt(number -> number).sum());
}
}
}