
📐 해당 문제를 보고 피타고라스의 정리가 떠올랐다.
(거리)
즉, 가 된다.

점을 찍을수 있는 모든 좌표는
(0, 0), (0, 2), (0, 4)
(2, 0), (2, 2), (2, 4)
(4, 0), (4, 2), (4, 4)
9가지가 있다.
총 6가지가 가능하다.
굉장히 쉽게 생각하여 바로 코드를 작성하여 제출했다
class Solution {
public long solution(int k, int d) {
long cnt = 0;
for (int x = 0; x <= d; x += k) {
for (int y = 0; y <= d; y += k) {
if (x * x + y * y <= d * d) {
cnt++;
}
}
}
return cnt;
}
public static void main(String[] args) {
Solution sol = new Solution();
int k = 1;
int d = 5;
System.out.println(sol.solution(k, d));
}
}

그제서여 보이는 제한사항....

100만이 넘어가는 큰 수는 시간도 엄청나게 오래걸리고, 두개의 반복문을 사용해 모든 쌍을 탐색하기 때문에 시간이 만큼 소요된다.
long dSquare = (long) d * d;
for (long x = 0; x <= d; x += k)
long y = (long) Math.sqrt(dSquare - x * x);
cnt += (y / k) + 1;
class Solution {
public long solution(int k, int d) {
long cnt = 0;
long dSquare = (long) d * d;
for (long x = 0; x <= d; x += k) {
long y = (long) Math.sqrt(dSquare - x * x);
cnt += (y / k) + 1;
}
return cnt;
}
public static void main(String[] args) {
Solution sol = new Solution();
int k = 1000000;
int d = 1000000;
System.out.println(sol.solution(k, d));
}
}
