这是我写的最优的了。如果还能优化,就是思路问题了。 import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
in.nextLine();
int prex, result = 0;
int min = Math.min(n, m);
int max = Math.max(n, m);
for (int i = 1; i <= min; i++) {
prex = 0;
for (int j = i + 1; j <= min; j++) {
if (sqrt((long) i * j))
prex = 1 + prex;
}
result += prex;
}
result = min + result * 2;
for (int i = 1; i <= min; i++) {
prex = 0;
for (int j = min + 1; j <= max; j++) {
if (sqrt((long) i * j))
prex = 1 + prex;
}
result += prex;
}
System.out.println(result);
in.close();
}
public static boolean sqrt(long x) {
if (x == 1)
return true;
long left = 1, right = x;
while (left <= right) {
long mid = (left + right) / 2;
if (x == (mid * mid))
return true;
if (x > (mid * mid))
left = mid + 1;
else
right = mid - 1;
}
return false;
}
}