Problem:


Score : 700 points

Problem Statement

10^{10^{10}} participants, including Takahashi, competed in two programming contests.
In each contest, all participants had distinct ranks from first through 10^{10^{10}}-th.

The score of a participant is the product of his/her ranks in the two contests.

Process the following Q queries:

In the i-th query, you are given two positive integers A_i and B_i. Assuming that Takahashi was ranked A_i-th in the first contest and B_i-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.

Constraints

1 \leq Q \leq 100

1\leq A_i,B_i\leq 10^9(1\leq i\leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1
:
A_Q B_Q

Output

For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.

Sample Input 1

8
1 4
10 5
3 3
4 11
8 9
22 40
8 36
314159265 358979323

Sample Output 1

1
12
4
11
14
57
31
671644785

Let us denote a participant who was ranked x-th in the first contest and y-th in the second contest as (x,y).

In the first query, (2,1) is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print 1.



Sample input:
8
1 4
10 5
3 3
4 11
8 9
22 40
8 36
314159265 358979323


Sample output:
1
12
4
11
14
57
31
671644785

Submission status: Accepted

Solution code:
from math import sqrt
import sys
sdin = sys.stdin.readline

q = int(sdin())
ab = []
for i in range(q):
    a,b = map(int, sdin().split())
    if a > b:
        a,b = b,a
    ab.append(tuple([a,b]))

for a, b in ab:
    if  b-a < 2:
        print(2*a - 2)
        
    else:
        if not (sqrt(a*b) - int(sqrt(a*b))):
            c = int(sqrt(a*b) - 1)
        else:
            c  = int(sqrt(a*b))
        
        if c*(c+1) >= a*b:
            print(2*c - 2)
        else:
            print(2*c - 1)

Submission status: Accepted

Solution code:
from math import sqrt
import sys
sdin = sys.stdin.readline

q = int(sdin())
ab = []
for i in range(q):
    ab.append(tuple(map(int, sdin().split())))

for a, b in ab:
    if b-a >= 0 and b-a < 2:
        print(2*a - 2)
        
    else:
        if not (sqrt(a*b) - int(sqrt(a*b))):
            c = int(sqrt(a*b) - 1)
        else:
            c  = int(sqrt(a*b))
        
        if c*(c+1) >= a*b:
            print(2*c - 2)
        else:
            print(2*c - 1)

Submission status: Accepted

Solution code:
from math import sqrt
import sys
sdin = sys.stdin.readline

q = int(sdin())
ab = []
for i in range(q):
    ab.append(tuple(map(int, sdin().split())))

for a, b in ab:
    if b-a >= 0 and b-a <= 1:
        print(2*a - 2)
        
    else:
        if not (sqrt(a*b) - int(sqrt(a*b))):
            c = int(sqrt(a*b) - 1)
        else:
            c  = int(sqrt(a*b))
        
        if c*(c+1) >= a*b:
            print(2*c - 2)
        else:
            print(2*c - 1)

Submission status: Accepted

Solution code:
from math import sqrt
import sys
sdin = sys.stdin.readline

q = int(sdin())
ab = []
for i in range(q):
    ab.append(tuple(map(int, sdin().split())))

for a, b in ab:
    if b-a == 0 or b-a == 1:
        print(2*a - 2)
        
    else:
        if not (sqrt(a*b) - int(sqrt(a*b))):
            c = int(sqrt(a*b) - 1)
        else:
            c  = int(sqrt(a*b))
        
        if c*(c+1) >= a*b:
            print(2*c - 2)
        else:
            print(2*c - 1)

Submission status: Accepted

Solution code:
from math import sqrt
import sys
sdin = sys.stdin.readline

q = int(sdin())
ab = []
for i in range(q):
    ab.append(tuple(map(int, sdin().split())))

for a, b in ab:
    if a == b or a == b-1:
        print(2*a - 2)
        
    else:
        if not (sqrt(a*b) - int(sqrt(a*b))):
            c = int(sqrt(a*b) - 1)
        else:
            c  = int(sqrt(a*b))
        
        if c*(c+1) >= a*b:
            print(2*c - 2)
        else:
            print(2*c - 1)

Submission status: Accepted

Solution code:
from math import sqrt
import sys
sdin = sys.stdin.readline

q = int(sdin())
ab = []
for i in range(q):
    ab.append(tuple(map(int, sdin().split())))

for a, b in ab:
    if a == b or a == b-1:
        print(2*a - 2)
        
    else:
        if not (sqrt(a*b) - int(sqrt(a*b))):
            c = int(sqrt(a*b) - 1)
        else:
            c  = int(sqrt(a*b))
        
        if c*(c+1) >= a*b:
            print(2*c - 2)
        else:
            print(2*c - 1)

Submission status: Accepted

Solution code:
from math import floor, sqrt
Q = int(input())
A = []

for i in range(Q):
  A.append(list(map(int,input().split())))

for i in range(Q):
  P,Q = min(A[i][0], A[i][1]), max(A[i][0], A[i][1])
  if P == Q or P == Q-1:
    print(2*(P - 1))
  else:
    C = floor(sqrt(P*Q))
    if C == sqrt(P*Q):
      C -= 1

    if C*(C+1) >= P*Q:
      print(2*C - 2)
    else:
      print(2*C - 1)

Submission status: Accepted

Solution code:
def f(a,b):
  if a>b:a,b=b,a
  if a==b or a+1==b:
    return 2*a-2
  else:
    c=int((a*b)**(1/2))
    if c**2==a*b:c-=1
    if c*(c+1)>=a*b:
      return 2*c-2
    else:
      return 2*c-1

q=int(input())
for _ in range(q):
  a,b=map(int,input().split())
  print(f(a,b))

Submission status: Accepted

Solution code:
from math import sqrt

q = int(input())

p = []
for _ in range(q):
    p.append(list(map(int, input().split())))

for t in p:
    x = min(t)
    y = max(t)
    if x == y or x + 1 == y:
        print(x * 2 - 2)
    else:
        temp = sqrt(x * y)
        hoge = int(temp)
        if hoge == temp:
            hoge -= 1
        if hoge * (hoge + 1) >= x * y:
            print(2 * hoge - 2)
        else:
            print(2 * hoge - 1)

Submission status: Accepted

Solution code:
Q=int(input())
score=[list(map(int,input().split())) for _ in range(Q)]
import math
for i in range(Q):
    max_num=int(math.sqrt(score[i][0]*score[i][1]))

    if score[i][0]==score[i][1]:
        print(2*max_num-2)
    elif score[i][0]*score[i][1]==max_num**2:
        print(2*max_num-3)
    elif score[i][0]*score[i][1]>max_num*(max_num+1):
        print(2*max_num-1)
    else:
        print(2*max_num-2)

Submission status: Accepted

Solution code:
q = int(input())
ab = [tuple(map(int, input().split())) for _ in range(q)]



for a, b in ab:
    if a > b:
        large = a
        small = b
    elif a < b:
        large = b
        small = a
    else:
        print(2* a - 2)
        continue


    # 最大のxを探す
    low_x = 0 
    upper_x = large - small + 1

    while upper_x- low_x> 1:
        mid_x = (low_x + upper_x ) //2

        # a * bを満たすかチェック
        is_ok = True
        for i in range(mid_x // 2 -2, mid_x //2 +3) :
            if i< 0 and b -a < i:
                continue
            if (small + i) * (small + mid_x - i) >= large* small:
                is_ok = False
                break

        if is_ok:
            low_x = mid_x
            x = mid_x
        else:
            upper_x = mid_x
            x= mid_x


    print(2 * small -2  + low_x)

Submission status: Accepted

Solution code:
from itertools import count, takewhile
from math import floor, sqrt

n = int(input())
xys = [map(int, input().split()) for _ in range(n)]

def mysqrt(n):
    x = floor(sqrt(n))
    for x in takewhile(lambda x: x**2 <= n, count(max(0, x-2))):
        pass
    return x

for x, y in xys:
    xy = x*y
    root = mysqrt(xy-1)
    ans = root*2 - (root**2 + root >= xy) - 1 + (x == y)
    print(ans)

Submission status: Accepted

Solution code:
from itertools import count, takewhile
from math import floor, sqrt
 
n = int(input())
xys = [map(int, input().split()) for _ in range(n)]
 
def mysqrt(n):
    x = floor(sqrt(n))
    for y in takewhile(lambda y: y**2 <= n, count(max(0, x-2))):
        x = y
    return x
 
for x, y in xys:
    xy = x*y
    root = mysqrt(xy-1)
    ans = root*2 - (root**2 + root >= xy) - 1 + (x == y)
    print(ans)

Submission status: Accepted

Solution code:
from itertools import count, takewhile
from math import floor, sqrt

n = int(input())
xys = [map(int, input().split()) for _ in range(n)]

def mysqrt(n):
    x = floor(sqrt(n))
    for y in takewhile(lambda y: y**2 <= n, count(max(0, x-10))):
        x = y
    return x

for x, y in xys:
    xy = x*y
    root = mysqrt(xy-1)
    ans = root*2 - (root**2 + root >= xy) - 1 + (x == y)
    print(ans)

Submission status: Accepted

Solution code:
from math import floor, sqrt
from itertools import count, takewhile

n = int(input())
xys = [tuple(map(int, input().split())) for _ in range(n)]

def mysqrt(n):
    x = floor(sqrt(n))
    for y in takewhile(lambda y: y**2 <= n, count(max(0, x-10))):
        x = y
    return x

for xy in xys:
    a, b = sorted(xy)
    if b - a < 2:
        print(2*a - 2)
    else:
        ab = a*b
        c = mysqrt(ab-1)
        if c*(c+1) >= ab:
            print(c*2 - 2)
        else:
            print(c*2 - 1)

Submission status: Accepted

Solution code:
q = int(input())
ab = [tuple(map(int, input().split())) for _ in range(q)]



for a, b in ab:
    if a > b:
        large = a
        small = b
    elif a < b:
        large = b
        small = a
    else:
        print(2* a - 2)
        continue


    # 最大のxを探す
    low_x = 0 
    upper_x = large - small + 1

    while upper_x- low_x> 1:
        mid_x = (low_x + upper_x ) //2

        # a * bを満たすかチェック
        is_ok = True
        for i in range(mid_x // 2 -2, mid_x //2 +3) :
            if i< 0 and b -a < i:
                continue
            if (small + i) * (small + mid_x - i) >= large* small:
                is_ok = False
                break

        if is_ok:
            low_x = mid_x
            x = mid_x
        else:
            upper_x = mid_x
            x= mid_x


    print(2 * small -2  + low_x)

Submission status: Accepted

Solution code:
import numpy as np

def solve(a, b):
    if a == b: return 2 * (a-1)
    scoreT = a*b
    x = y = int(np.sqrt(scoreT)) - 2
    while True:
        if (x+1)*y < scoreT:
            x += 1
        else:
            break
        if x*(y+1) < scoreT:
            y += 1
        else:
            break
    if x == y:
        return (x-1) * 2
    else:
        return y * 2 - 1


for i in range(int(input())):
    a, b = map(int, input().split())
    print(solve(a, b))

Submission status: Accepted

Solution code:
import math
Q = int(input())
for i in range(Q):
    a = list(map(int,input().split()))
    a.sort()
    A = a[0]
    B = a[1]
    n = 0
    if A==B:
        n = 2*A-2
    elif B == A+1:
        n = 2*A-2
    else:
        C = math.ceil(math.sqrt(A*B)-1)
        if C*(C+1)>=A*B:
            n = 2*C-2
        else:
            n = 2*C-1
    print(n)

Submission status: Accepted

Solution code:
n=int(input())

for i in range(n):
  a,b=[int(i) for i in input().split()]
  if a==b:
    print((min(a,b)-1)*2)
  else:
    if int((a*b)**0.5) * (int((a*b)**0.5)+1) < a*b:
      print(int((a*b)**0.5) *2 - 1)
    elif int((a*b)**0.5) **2 == a*b:
      print(int((a*b)**0.5) *2 - 3)
    else:
      print(int((a*b)**0.5) *2 -2)

Submission status: Accepted

Solution code:
Q = int(input())
ab = [tuple(map(int, input().split())) for _ in range(Q)]

for a, b in ab:
    if a > b:
        tmp = a
        a = b
        b = tmp
    elif a == b:
        print(2 * a - 2)
        continue

    ans = 2 * a - 2

    # x の二分探索
    x = 0
    low_x = 0
    upper_x = b - a + 1
    while upper_x - low_x > 1:
        mid_x = (low_x + upper_x) // 2

        # 与えられた x の中央値付近で a * b 未満を満たすか調べる
        flg = 1
        for j in range(mid_x//2-2,  mid_x//2+2 + 1):
            if j < 0 and b - a < j:
                continue
            if (a + j) * (a + mid_x - j) >= a * b:
                flg = 0
                break
        # mid_rank = a + (mid_x // 2)  # a ＋ xの幅の半分（中央）
        # for i in range(mid_rank - 2, (mid_rank + 2) + 1):
        #     k = mid_rank * 2 - i
        #     if a < i <= b and a <= k < b:
        #         if i * k >= a * b:
        #             flg = 1
        #             break
        if flg:
            low_x = mid_x
            x = mid_x
        else:
            upper_x = mid_x
            x = mid_x

    print(ans + low_x)

Submission status: Accepted

Solution code:
from math import floor, sqrt

Q = int(input())
for q in range(Q):
  a, b = map(int, input().split())
  K = floor(sqrt(a*b))
  if a== b:
    print(2*K-2)
  elif a*b == K*K:
    print(2*K-3)
  elif K*(K+1) < a*b:
    print(2*K-1)
  else:
    print(2*K-2)

Submission status: Accepted

Solution code:
Q = int(input())
ans = []
for q in range(Q):
    A, B = map(int, input().split())
    M = int((A*B)**0.5)
    if M*(M + 1) < A*B: 
        ans.append(2*M - 1)
    elif A*B == M*M and A != B: 
        ans.append(2*M - 3)
    else: 
        ans.append(2*M - 2)

print("\n".join(map(str, ans)))

Submission status: Accepted

Solution code:
import math

def worst_case(A, B):
    c = min(A, B)
    d = max(A, B)
    if c == d or c + 1 == d:
        return 2 * (c - 1)
    root = int(math.sqrt(A * B))
    if root ** 2 == A * B:
        root -= 1
    return 2 * (root - 1) + 1 if root * (root + 1) < A * B else 2 * (root - 1)

Q = int(input())
A = []
B = []
for i in range(Q):
    line = input().split()
    A.append(int(line[0]))
    B.append(int(line[1]))

for i in range(Q):
    print(worst_case(A[i], B[i]))

Submission status: Accepted

Solution code:
import sys

def kth(ed, erase, k):
  if (k < erase):
    return k
  return k + 1

def ck(a, b, x):
  c = x
  if a <= x:
    c += 1
  d = x
  if b <= x:
    d += 1
  if x % 2 == 1:
    m = (x + 1) / 2
    return kth(c, a, m) * kth(d, b, m) < a * b
  else:
    m0 = x / 2
    m1 = x / 2 + 1
    return kth(c, a, m0) * kth(d, b, m1) < a * b and kth(c, a, m1) * kth(d, b, m0) < a * b

T = int(raw_input())
while T > 0:
  a, b = map(int, raw_input().split())
  l = 0
  r = a * b
  while r - l > 1:
    m = (l + r) / 2
    if ck(a, b, m):
      l = m
    else:
      r = m
  if ck(a, b, r):
    print r
  else:
    print l
  T -= 1

Submission status: Accepted

Solution code:
# PROBLEM D
Q = int(input())
for _ in range(Q):
    a, b = sorted([int(s) for s in input().split()])
    c_max = int((a * b) ** 0.5)
    if c_max * c_max == a * b:
        c_max -= 1
    sol = 2 * c_max
    if a <= c_max:
        sol -= 1
    if (c_max * c_max < a * b) and (c_max * (c_max + 1) >= a * b):
        sol -= 1
    print(sol)

Submission status: Accepted

Solution code:
from math import sqrt
from math import floor

Q = int(input())
A_list = []
B_list = []

for i in range(Q):
    A, B = map(int, input().split())
    A_list.append(A)
    B_list.append(B)

for A, B in zip(A_list, B_list):
    if A == B:
        print(2*A-2)
        continue
    if abs( A - B) == 1:
        print(2*min(A, B)-2)
        continue
    C = int(floor(sqrt(A*B)))
    if C*C >= A*B:
        C -= 1
    if C * (C + 1) >= A*B:
        print(2*C-2)
        continue
    print(2*C-1)

Submission status: Accepted

Solution code:
import math

def solve(a, b):
	if a>b: a,b = b,a
	if a==b or a+1==b:
		return 2*a-2
	
	mul = a*b
	c = int(math.sqrt(mul))
	if c**2==mul: c-=1
	if c*(c+1)<mul:
		return 2*c-1 #(a-1)+(2c-a)
	else:
		return 2*c-2 #(a-1)+(2c-a-1)

n = int(input())
for _ in range(n):
	a,b = map(int, input().split())
	print(solve(a,b))

Submission status: Accepted

Solution code:
def solve(a,b):
  if a==b:
    return (a-1)+(b-1)
  elif a+1==b or b+1==a:
    return (min(a,b)-1)*2
  else:
    cnt=(a*b)**(1/2)
    if cnt==int(cnt):
      cnt=int(cnt)-1
    else:
      cnt=int(cnt)
    if cnt*(cnt+1)>=a*b:
      return int(cnt)*2-2
    else:
      return int(cnt)*2-1

Q=int(input())

for i in range(Q):
  a,b=map(int,input().split())
  print(solve(a,b))

Submission status: Accepted

Solution code:
#abc093d
import math

q=int(raw_input())
res=[]
for i in xrange(q):
 a,b=map(int,raw_input().split())
 m=a*b
 x=int(math.sqrt(m-1))
 while x**2>=m:
  x-=1
 y=x*2
 if x*(x+1)>=m:
  y-=1
 if max(a,b)<=x:
  res-=1
 if min(a,b)<=x:
  y-=1
 res.append(y)
for i in xrange(q):
 print res[i]

Submission status: Accepted

Solution code:
import math
q = int(input())
a = [list(map(int, input().split())) for i in range(q)]

for i in range(q):
    ans = 0
    AB = a[i][0]*a[i][1]
    if a[i][0]<=a[i][1]:
        A, B = a[i][0], a[i][1]
    else:
        A, B = a[i][1], a[i][0]
    x = int(math.sqrt(AB-1))
    while x*x>=AB:
        x-=1
    if A==B:
        ans=2*A-2
    elif A+1==B:
        ans= 2*A-2
    elif x*(x+1)>=AB:
        ans = 2*x-2
    else:
        ans = 2*x-1
    print(ans)

Submission status: Accepted

Solution code:
import math
for _ in range(int(input())):
    a, b = map(int, input().split())
    a, b = max(a, b), min(a, b)
    p = a * b
    x = int(math.sqrt(p - 1))
    while x * x >= p:
        x -= 1
    c = x * 2
    if x * (x + 1) >= p:
        c -= 1
    if a <= x:
        c -= 1
    if b <= x:
        c -= 1
    print(c)

Submission status: Accepted

Solution code:
import math
n = int(input())
for i in range(n):
    a, b = map(int, input().split())
    if a == b:
        print(2 * a - 2)
    elif max(a, b) - min(a, b) == 1:
        print(2 * min(a, b) - 2)
    else:
        c = math.floor(math.sqrt(a * b))
        if c == math.sqrt(a*b):
            c -= 1
        if c * (c + 1) >= a * b:
            print(2 * c - 2)
        else:
            print(2 * c - 1)

Submission status: Accepted

Solution code:
import math
I,j=int,input
q=I(j())
for i in[0]*q:
	a,b=map(I,j().split())
	c=I(math.sqrt(a*b))
	if c*(c+1)<a*b:print(2*c-1)
	elif (a*b==c*c)*(a!=b):print(2*c-3)
	else:print(2*c-2)

Submission status: Accepted

Solution code:
import math
I=int
j=input
q=I(j())
for i in[0]*q:
	a,b=map(I,j().split())
	c=I(math.sqrt(a*b))
	if c*(c+1)<a*b:print(2*c-1)
	elif (a*b==c*c)*(a!=b):print(2*c-3)
	else:print(2*c-2)

Submission status: Accepted

Solution code:
import math
I=int
q=I(input())
for i in[0]*q:
	a,b=map(I,input().split())
	c=I(math.sqrt(a*b))
	if c*(c+1)<a*b:print(2*c-1)
	elif (a*b==c*c)*(a!=b):print(2*c-3)
	else:print(2*c-2)

Submission status: Accepted

Solution code:
import math
I=int
q=I(input())
while q:
	a,b=map(I,input().split())
	c=I(math.sqrt(a*b))
	if c*(c+1)<a*b:print(2*c-1)
	elif (a*b==c*c)*(a!=b):print(2*c-3)
	else:print(2*c-2)
	q-=1

Submission status: Accepted

Solution code:
import math
q=int(input())
for i in[0]*q:
	a,b=map(int,input().split())
	c=int(math.sqrt(a*b))
	if c*(c+1)<a*b:print(2*c-1)
	elif a*b==c*c and a!=b:print(2*c-3)
	else:print(2*c-2)

Submission status: Accepted

Solution code:
import math
Q = int(input())
for _ in range(Q):
	A,B = map(int,input().split())
	res = 0
	if A == B or A + 1 == B:
		res = 2 * A - 2
	else:
		C = int(math.sqrt(A * B))
		if C * C == A * B:
			C -= 1
		if C * (C + 1) >= A * B:
			res = 2 * C - 2
		else:
			res = 2 * C - 1
	print(res)

Submission status: Accepted

Solution code:
from numpy import sqrt

N = int(input())
for _ in range(N):
    a, b = map(int, input().split())
    score = a*b
    ans = 2*int(sqrt(score))
    if (ans//2) ** 2 == a*b:
        ans -= 2
    if a == b:
        ans = 2*a - 2
    else:
        if a > b:
            a, b = b, a

        if b - a == 1:
            ans = 2*a - 2
        elif ans//2 * (ans//2+1) >= a*b:
            ans -= 2
        else:
            ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
import math
n=int(input())
for _ in range(n):
    a,b=map(int,input().split())
    c=int(math.sqrt(a*b))
    if a*b==c*c and a!=b:  print(2*c-3)
    elif c*(c+1)<a*b: print(2*c-1)
    else:  print(2*c-2)

Submission status: Accepted

Solution code:
import math
n=int(input())
for _ in range(n):
    a,b=map(int,input().split())
    c=int(math.sqrt(a*b))
    if c*(c+1)<a*b: print(2*c-1)
    elif a*b==c*c and a!=b:  print(2*c-3)
    else:  print(2*c-2)

Submission status: Accepted

Solution code:
import math
Q=input()

a=[]
for i in range(Q):
	x,y=map(int, raw_input().split())
	if x<=y:
		A=x
		B=y
	else:
		A=y
		B=x
	a.append((x,y))


for A,B in a:
	C=int(math.sqrt(A*B))
	if C**2<A*B:
		pass
	else:
		C-=1
	if A==B:		print 2*A-2
	elif A+1==B:		print 2*A-2
	elif C*(C+1)>=A*B:	print 2*C-2
	else:			print 2*C-1

Submission status: Accepted

Solution code:
import math

q = int(input())
a = []
b = []
for i in range(q):
  c = [int(i) for i in input().split()]
  a += [c[0]]
  b += [c[1]]

for i in range(q):
  prod = a[i] * b[i]
  sqrt = int(math.sqrt(prod))
  cnt = 2 * sqrt

  if a[i] == b[i]:
    cnt -= 2
  else:
    if sqrt * sqrt == prod:
      cnt -= 2
    elif prod // sqrt == sqrt:
      cnt -= 1
    elif prod // sqrt == sqrt + 1 and prod // sqrt * sqrt == prod:
      cnt -= 1
    cnt -= 1
  
  print(cnt)

Submission status: Accepted

Solution code:
import math

N = int(input().strip())
grid = []
for i in range(N):
	array = list(map(int, input().strip().split(' ')))
	grid.append(array)

for i in grid:
	i.sort()
	a,b = [i[0],i[1]]
	if(a == b or a+1 == b):
		print(2*a-2)
	else:
		c = int(math.sqrt(a*b))
		if c**2 == a*b:
			c-=1
		if(c*(c+1)>=a*b):
			print(2*c-2)
		else:
			print(2*c-1)

Submission status: Accepted

Solution code:
import math
n = int(input())

for i in range(n):
    s, t = map(int, input().split())
    a = min(s, t)
    b = max(s, t)
    r = a * b
    q = math.sqrt(r)
    x = int(q)
    if x == q:
        x -= 1

    if a + 1 >= b:
        ans = a * 2 - 2
    elif x * (x + 1) >= a * b:
        ans = x * 2 - 2
    else:
        ans = x * 2 - 1
    print(ans)

Submission status: Accepted

Solution code:
import math

q = int(input())
lis = [list(map(int, input().split())) for _ in range(q)]
for i in range(q):
	product = lis[i][0]*lis[i][1]
	rootInt = int(math.sqrt(product))
	if lis[i][0] == lis[i][1]:
		print((lis[i][0]-1)*2)
	elif math.sqrt(product) == rootInt:
		print((rootInt-2)*2+1)
	else:
		if rootInt*(rootInt+1) < product:
			print((rootInt-1)*2+1)
		else:
			print((rootInt-1)*2)

Submission status: Accepted

Solution code:
from math import sqrt

if __name__ == '__main__':
    Q = int(input())
    values = [list(map(int, input().split())) for _ in range(Q)]
    As, Bs = zip(*values)

    for A, B in zip(As, Bs):
        A, B = min(A, B), max(A, B)

        # A = B
        if A == B:
            print(2 * A - 2)
            continue

        # A + 1 = B
        if A + 1 == B:
            print((A - 2) + (B - 2) + 1)
            continue

        C = int(sqrt(A * B))
        if C == sqrt(A * B):
            C -= 1

        # C(C + 1) >= AB
        if C * (C + 1) >= A * B:
            print(2 * C - 2)
            continue

        # C^2 < AB
        if C ** 2 < A * B:
            print(2 * C - 1)
            continue

Submission status: Accepted

Solution code:
from math import sqrt

if __name__ == '__main__':
    Q = int(input())
    values = [list(map(int, input().split())) for _ in range(Q)]
    As, Bs = zip(*values)

    # As = [1]
    # Bs = [4]

    for A, B in zip(As, Bs):
        A, B = min(A, B), max(A, B)

        # A = B
        if A == B:
            print(2 * A - 2)
            continue

        # A + 1 = B
        if A + 1 == B:
            print((A - 2) + (B - 2) + 1)
            continue

        C = int(sqrt(A * B))
        if C == sqrt(A * B):
            C -= 1

        # C(C + 1) >= AB
        if C * (C + 1) >= A * B:
            print(2 * C - 2)
            continue

        # C^2 < AB
        if C ** 2 < A * B:
            print(2 * C - 1)
            continue

Submission status: Accepted

Solution code:
from math import sqrt
q = int(input())
for _ in range(q):
    a, b = map(int, input().split(" "))
    c = a * b
    d = int(sqrt(c))
    answer = 2 * d - 2
    if d * d == c:
        answer -= 1
    if d * (d + 1) < c:
        answer += 1
    if a == b:
        answer += 1
    print(answer)

Submission status: Accepted

Solution code:
from math import sqrt
q = int(input())
for _ in range(q):
    a, b = map(int, input().split(" "))
    c = a * b
    d = int(sqrt(c))
    answer = 2 * d - 2
    if d**2 == c:
        answer -= 1
    if d * (d + 1) < c:
        answer += 1
    if a == b:
        answer += 1
    print(answer)

Submission status: Accepted

Solution code:
Q = int(input())

while Q:
    Q -= 1
    A, B = map(int, input().split())
    M = max(A, B)
    m = min(A, B)
    if M == m:
        print(2 * m - 2)
        continue
    if M == m + 1:
        print(2 * m - 2)
        continue
    last = int((M * m) ** 0.5)
    if last == (M * m) ** 0.5:
        last -= 1
    if last * (last + 1) >= M * m:
        print(last * 2 - 2)
    else:
        print(last * 2 - 1)

Submission status: Accepted

Solution code:
def d_worst_case(Q, R):
    import math
    ans = []
    for a, b in R:
        a, b = min(a, b), max(a, b)
        if a == b:
            ans.append(str(2 * a - 2))
        elif a + 1 == b:
            ans.append(str(2 * a - 2))
        else:
            c = int((a * b)**0.5)
            if c == (a * b)**0.5:
                c -= 1
            if c * (c + 1) >= a * b:
                ans.append(str(2 * c - 2))
            else:
                ans.append(str(2 * c - 1))
    return '\n'.join(ans)

Q = int(input())
R = [[int(i) for i in input().split()] for j in range(Q)]
print(d_worst_case(Q, R))

Submission status: Accepted

Solution code:
# coding=utf-8
import math


def solve_worst(number_1, number_2):
    cases = 0
    if number_1 < number_2:
        number_1, number_2 = number_2, number_1

    if (number_1 * number_2) % (number_1 + 1) == 0:
        max_2 = number_1 * number_2 // (number_1 + 1) - 1
    else:
        max_2 = number_1 * number_2 // (number_1 + 1)
    cases += max_2

    min_sqrt = math.floor(math.sqrt(number_1 * number_2))
    if min_sqrt == number_2:
        min_sqrt = number_2 + 1
    cases += min_sqrt - number_2

    if (number_1 * number_2) % min_sqrt == 0:
        max_1 = number_1 * number_2 // min_sqrt - 2
    else:
        max_1 = number_1 * number_2 // min_sqrt - 1

    cases += max_1

    return cases


if __name__ == '__main__':
    Q = int(input())
    for i in range(Q):
        A, B = map(int, input().split())
        worst_cases = solve_worst(A, B)
        print(worst_cases)

Submission status: Accepted

Solution code:
Q = int(input())
for i in range(Q):
    a,b = (map(int, input().split()))
    if(a == b):
        print(2*a -2)
    elif(abs(a-b) == 1):
        print(2 * min(a,b) -2)
    else:
        c = int((a*b) ** 0.5)
        if c == (a*b) ** 0.5:
            c -= 1
        if(c*(c+1) >= a*b):
            print(2*c-2)
        else:
            print(2*c-1)

Submission status: Accepted

Solution code:
from math import sqrt, floor
q = int(input())
for _ in range(q):
    a, b = map(int, input().split(" "))
    if a == b:
        print(2 * a - 2)
    elif max(a, b) - min(a, b) == 1:
        print(2 * min(a, b) - 2)
    else:
        c = floor(sqrt(a * b))
        if c == sqrt(a*b):
            c -= 1
        if c * (c + 1) >= a * b:
            print(2 * c - 2)
        else:
            print(2 * c - 1)

Submission status: Accepted

Solution code:
import math

n = int(raw_input())

for a in range(n):
  x = map(int,raw_input().split())
  x.sort()

  if x[0] == x[1] or x[0] == (x[1]-1):
    print (x[0]-1)*2
    continue

  q = x[0]*x[1]
  buf = math.sqrt(q)
  buf = int(buf)
  if buf*buf == x[0]*x[1]:
    buf -= 1

  ans = buf * 2 - 2
  if buf*(buf+1) < x[0]*x[1]:
    ans += 1

  print ans

Submission status: Accepted

Solution code:
import math

n = int(raw_input())

for a in range(n):
  x = map(int,raw_input().split())
  x.sort()

  if x[0] == x[1]:
    print 2*x[0] - 2
    continue

  if x[0]+1 == x[1]:
    print 2*x[0] - 2
    continue

  q = x[0]*x[1]
  q = int(math.sqrt(q))

  if q*q == x[0]*x[1]:
    q -= 1

  if q*(q+1) >= x[0]*x[1]:
    print 2*q - 2
    continue

  print 2*q - 1

Submission status: Accepted

Solution code:
from math import sqrt
q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    t = a*b
    s = int(sqrt(t))
    an = 2 * s - 2
    if s*s == t:
        an -= 1
    if s*(s+1) < t:
        an += 1
    if a == b:
        an += 1
    print(an)

Submission status: Accepted

Solution code:
Q = int(input())

def f(L):
    A, B = L
    N = 2 * (A * B) ** 0.5
    K = int(N)
    #print(K, N)
    K -= 1 if K == N else 0
    #print(K)
    res = K - 1
    if A + B == K:
        res -= 1
    else:
        #print(res)
        if (K - 1 >= A or K - 1 >= B) and A != B:
            res -= 1
    return res
    
for _ in range(Q):
    q = list(map(int, input().split()))
    print(f(q))

Submission status: Accepted

Solution code:
import math
Q = int(input())
for _ in range(Q):
    A, B = list(map(int, input().split()))

    div = A * B
    ca = int(math.sqrt(div))
    if ca == A:
        ca += 1
    result = ca - 1
    if A <= ca:
        result -= 1
    cand = float("inf")
    while True:
        if ca == A:
            ca += 1
        cb = min(div // ca, cand)
        if cb * ca == div:
            cb -= 1
        if cb == B:
            cb -= 1
        if cb == cand:
            result += cb - 1
            if B <= cand:
                result -= 1
            break
        cand = cb
        result += 1
        # print(ca, cand, result)
        ca += 1
        if cb == 1 or ca == 1:
            if B <= cand:
                result -= 1
            break
    print(result)

Submission status: Accepted

Solution code:
from math import sqrt

Q = int(input())

for i in range(Q):
    A, B = list(map(int, input().split()))

    sq = sqrt(A*B)
    sq_int = int(sq)

    ans = sq_int*2 - 2
    if sq_int**2==A*B and A!=B:
        ans -= 1
    if sq_int*(sq_int+1)<A*B:
        ans += 1

    #print(A*B, sq_int)
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
q = input()

n = np.zeros((q, 2), dtype=np.int64)
for i in range(0, q):    
    n[i, :] = map(int, raw_input().split())

for i in range(0, q):


    s = n[i, 0] * n[i, 1]
    m = int(np.floor(np.sqrt(s)))

    # print m, s

    if n[i, 0] == n[i, 1]:        #equal
        print m * 2 - 2
    elif m ** 2 == s:         # square
        print m * 2 - 3
    # elif m ** 2 == s - 1:         # square - 1
    #     print m * 2 - 3
    elif m ** 2 < s - m:         # square - 1
        print m * 2 - 1
    else:
        print m * 2 - 2


    # mmm = []
    # for j in range(2, M + 1):
    #     # print mmm
    #     mm = M / j
    #     # print mm
    #     if mm in mmm or mm < 1:
    #         print len(mmm)
    #         break
    #     else:
    #         mmm.append(mm)

Submission status: Accepted

Solution code:
import numpy as np

q = int(input())

for _ in range(q):
    a, b = sorted(list(map(int, input().split(' '))))
    n = int(np.floor(np.sqrt(a * b)))

    cnt = (n - 1) * 2

    if a != b:
        if a == n:
            if n * (n + 1) < a * b:
                cnt += 1
        else:
            cnt += 1
            if n ** 2 == a * b:
                cnt -= 1
            if n * (n + 1) >= a * b:
                cnt -= 1

    print(cnt)