728x90
import math
def f(x):
    if x == 1:
        return 1
    elif x == 2:
        return 2
    else:
        a = int(math.sqrt(x))
        b = int(math.sqrt(x)) + 1
        if math.sqrt(x) - a == 0:
            return 2*a -1 
        else:
            if a**2 + 1 <= x < ((a ** 2 + 1) + b ** 2)/2 :
                return 2*a
            else:
                return 2*a + 1
            
N = int(input())
ans = []
for i in range (0,N):
    a,b = map(int,input().split())
    ans.append(f(b-a))
for i in range (0,N):
    print(ans[i])

힌트는 아래와 같다.

1 = 1 :1
2 = 1+1 :2
3 = 1+1+1 :3
4 = 1+2+1 :3
5 = 1+2+1+1 :4
6 = 1+2+2+1 :4
7 = 1+2+2+1+1 :5
8 = 1+2+2+2+1 :5
9 = 1+2+3+2+1 :5
10 = 1+2+3+2+1+1 :6
11 = 1+2+3+2+2+1 :6
12 = 1+2+3+3+2+1 :6
13 = 1+2+3+3+2+1+1 :7
14 = 1+2+3+3+2+2+1 :7
15 = 1+2+3+3+3+2+1 :7
16 = 1+2+3+4+3+2+1 :7
17 = 1+2+3+4+3+2+1+1 :8
18 = 1+2+3+4+3+2+2+1 :8
19 = 1+2+3+4+3+3+2+1 :8
20 = 1+2+3+4+4+3+2+1 :8
21 = 1+2+3+4+4+3+2+1+1 :9
22 = 1+2+3+4+4+3+2+2+1 :9
23 = 1+2+3+4+4+3+3+2+1 :9
24 = 1+2+3+4+4+4+3+2+1 :9
25 = 1+2+3+4+5+4+3+2+1 :9

규칙이 생기는 지점이 제곱수와 제곱수 사이 (ex:9,16,25) 인것에 착목하면 좋을 것 같다 :)

+ Recent posts