import java.util.*;
class Point{
long x,y;
public Point(long x,long y){
this.x = x;
this.y = y;
}
//중복제거 메소드 오버라이딩 ㅋㅋ;;
public boolean equals(Object obj){
if(obj instanceof Point){
Point tmp = (Point)obj;
return x==tmp.x && y==tmp.y;
}
return false;
}
public int hashCode(){
return Objects.hash(x,y);
}
public String toString(){
return x + ":" + y;
}
}
class Solution {
static Set<Point> set = new HashSet<>();
public long solution(int k, int d) {
long answer = 0;
// x제곱 + y제곱 = 대각선제곱 의 식을 이용해서 -> 대각선 제곱 > d 이면 포함 x
// else set.add();
long x=0,y=0;
// y값 먼저 올리면서 x도 올리고
while(y>=0){
if( (Math.pow(x,2) + Math.pow(y*k,2)) <= Math.pow(d,2)){
set.add(new Point(x,y*k));
}
else break;
y++;
while(y % k ==0){
if( (Math.pow(x,2) + Math.pow(y,2)) <= Math.pow(d,2)){
set.add(new Point(x,y));
}
else break;
x+=k;
}
x=0;
}
y=0; x=0;
// x값 후에 올리고
while(x>=0){
if( (Math.pow(x*k,2) + Math.pow(y,2)) <= Math.pow(d,2)){
set.add(new Point(x*k,y));
}
else break;
x++;
}
return set.size();
}
}
피타고라스 방식으로 풀어야겠다고 생각했는데 , 구현 방법이 틀린것같아서 정답 코드를 참고했다.........
class Solution {
public long solution(int k, int d) {
long answer = 0;
// 피타고라스 -> y제곱 = d제곱 - x제곱
for(int i=0; i<=d; i+=k){
int maxY = yDis(i,d);
answer += maxY/k + 1; // 여기서 1은 해당 (x,0)
}
return answer;
}
public static int yDis(int x,int d){
long powX = (long) Math.pow(x,2);
long powD = (long) Math.pow(d,2);
int result = (int)(Math.sqrt(powD-powX));
return result;
}
}
간단 하게 , x좌표를 k의 배수만큼 올려주면서 각 x좌표에서의 가능한 y의 크기를 구해서 거기에 해당되는 y의 개수를 추가해줌으로써 답을 구한다.,,,,,,,,,,,,,,,,,,,,,
분명히 피타고라스인건 알았는데, 구현 방법이 잘못되도 너무 잘못됐다. 쉽게 생각을 해보자.