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: Wrong Answer

Solution code:
from math import sqrt

q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    m = a * b
    s = int(sqrt(m - 1))
    ans = 2 * s - 1
    if s * (s + 1) >= m:
        ans -= 1
    if (s + 1) ** 2 == m and a == b:
        ans += 1
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt

q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    m = a * b
    s = int(sqrt(m - 1))
    ans = 2 * s
    if s * (s + 1) >= m:
        ans -= 1
    if s * s == m or a != b:
        ans -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt

Q = int(input())

for i in range(Q):
    A,B = map(int, input().split())
    m=A*B
    C = int(sqrt(m))
    if C*C==m:
        C=C-1

    if C*(C+1)<m:
        ans = 2*C-1
    else:
            ans = 2*C-2
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt

Q = int(input())

for i in range(Q):
    A,B = map(int, input().split())
    m=A*B
    if A==B:
        ans = 2*A-2
    elif A==B+1 or A+1==B:
        ans = 2*A-2
    else:
        C = int(sqrt(m))
        if C*C==m:
            C=C-1

        if C*(C+1)<m:
            ans = 2*C-1
        else:
            ans = 2*C-2
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt

q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    m = a * b
    s = int(sqrt(m - 1))
    ans = 2 * s
    if s * (s + 1) > m:
        ans -= 1
    if a != b:
        ans -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt

q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    m = a * b
    s = int(sqrt(m - 1))
    ans = 2 * s
    if s * (s + 1) >= m:
        ans -= 1
    if a != b:
        ans -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

Q = int(input())
for i in range(Q):
    x, y = map(int, input().split())
    if x == y:
        print(2 * (x - 1))
    elif x + 1 == y or x - 1 == y:
        print(2 * (x - 1))
    else:
        score = x * y
        a = int(math.sqrt(score - 1))
        is_same = True

        b = a
        if (a + 1) * b < score:
            is_same = False

        if is_same:
            print(2 * a - 2)
        else:
            print(2 * a - 1)

Submission status: Wrong Answer

Solution code:
q=int(input())
for i in range(q):
    a,b=map(int,input().split())
    s=int((a*b-1)**0.5)
    if s==0:
        print(0)
        continue
    ret=2*s
    if (a*b-1)//s==s:
        if min(a,b)<=s:
            ret-=2
        else:
            ret-=1
    elif min(a,b)<=s:
        ret-=1
    print(ret)

Submission status: Wrong Answer

Solution code:
# D
import math
Q = int(input())
A_list = []
B_list = []
for _ in range(Q):
    A, B = map(int, input().split())
    A_list.append(min(A, B))
    B_list.append(max(A, B))
    
for i in range(Q):
    A = A_list[i]
    B = B_list[i]
    R = int(math.sqrt(A*B-1))
    
    if A >= B - 1:
        print(2*A-2)
    elif R*(R+1) >= A*B:
        print(2*R-2)
    else:
        print(2*R-1)

Submission status: Wrong Answer

Solution code:
q=int(input())
for i in range(q):
    a,b=map(int,input().split())
    s=int((a*b-1)**0.5)
    if s==0:
        print(0)
        continue
    ret=2*s
    if (a*b-1)//s==s:
        ret-=1
    if min(a,b)<=s:
        ret-=1
    print(ret)

Submission status: Wrong Answer

Solution code:
# D
import math
Q = int(input())
A_list = []
B_list = []
for _ in range(Q):
    A, B = map(int, input().split())
    A_list.append(min(A, B))
    B_list.append(max(A, B))
    
for i in range(Q):
    A = A_list[i]
    B = B_list[i]
        
    if A >= B - 1:
        print(2*A-2)
        continue
        
    R = int(math.sqrt(A*B-1))
    
    if R*(R+1) >= A*B:
        print(2*R-2)
    else:
        print(2*R-1)

Submission status: Wrong Answer

Solution code:
# D
import math
Q = int(input())
A_list = []
B_list = []
for _ in range(Q):
    A, B = map(int, input().split())
    A_list.append(min(A, B))
    B_list.append(max(A, B))
    
for i in range(Q):
    A = A_list[i]
    B = B_list[i]
        
    if A == B:
        print(A+B-2)
        continue
        
    R = int(math.sqrt(A*B-1))
    
    if R*(R+1) >= A*B:
        print(2*R-2)
    else:
        print(2*R-1)

Submission status: Wrong Answer

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

def solve(a,b):
  if a==b:
    return (a-1)*2

  a,b = sorted((a,b))
  c = a*b-1

  r = floor(sqrt(c))

  d = 1
  if r*(r+1) > c:
    d += 1
  if r-b == a-r:
    d += 1

  return 2*r-d




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

Submission status: Wrong Answer

Solution code:
q=int(input())
while q>0:
    q-=1
    a,b=map(int,input().split())
    s=int((a*b)**0.5)
    if s==0:
        print(0)
        continue
    ret=2*s
    if (a*b-1)//s==s:
        ret-=1
    if min(a,b)<=s:
        ret-=1
    print(ret)

Submission status: Wrong Answer

Solution code:
# D
import math
Q = int(input())
A_list = []
B_list = []
for _ in range(Q):
    A, B = map(int, input().split())
    A_list.append(min(A, B))
    B_list.append(max(A, B))
    
for i in range(Q):
    A = A_list[i]
    B = B_list[i]
        
    if A == B:
        print(A+B-2)
        continue
        
    R = int(math.sqrt(A*B-1))
    Q = (A*B-1) // R
    
    if Q <= R+1:
        add = min(Q - A - 1, B - R - 1)  
    elif Q == R+2 and (R+1)**2 == A*B:
        add = 1
    elif (Q - R)**2 - 4*(A*B-Q*R) >= 0:
        add = int(((Q - R) - math.sqrt((Q - R)**2 - 4*(A*B-Q*R))) / 2)
        if (R + add)*(Q-add) == A*B:
            add -= 1
    else:
        print(xxxx)
    print((A-1) + R + add)

Submission status: Wrong Answer

Solution code:
import math 
Q=int(input())
A=[0 for i in range(Q)]
B=[0 for i in range(Q)]
for i in range(Q):
    A[i],B[i]=map(int,input().split())
for i in range(Q):
    C=A[i]*B[i]
    N=int(math.sqrt(C))
    D=0
    if N**2==C:
        N-=1
    else:
        while(True):
            if A[i]*B[i]<N**2:
                N-=1
            elif A[i]*B[i]>(N+1)**2:
                N+=1
            else:
                break
            print(N)
    M=2*N
    S=M-1
    if M*(M-A[i])>=C and M*(M-B[i])>=C and (M-A[i])*(M-B[i])<C:
        S-=1
    elif A[i]>=M and B[i]<M and M*(M-B[i])>=C:
        S-=1
    elif B[i]>=M and A[i]<M and M*(M-A[i])>=C:
        S-=1
    elif (M-A[i])*(M-B[i])>=C:
        S-=2
    print(S)

Submission status: Wrong Answer

Solution code:
import math
Q =int(input())
for i in range(Q):
    a,b = input().split()
    a,b = int(a), int(b)
    bar1 = math.floor(math.sqrt(a*b))
    count = 0
    if (a-1)*(b+1) > a*b:
        count += 1
    if (a+1)*(b-1) > a*b:
        count += 1
    if a == 1:
        count += 1
    if b == 1:
        count += 1
    if a == b:
        count += 1
    print(bar1*2 - count)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import math
Q = int(input())
for _ in range(Q):
    A, B = sorted(map(int, input().split()))
    ans = (A-1)+(B-1)
    if A!=B:
        mid_low = max(int(math.sqrt(A*B-1)), A+1)
        mid_high = (A*B-1)//mid_low
        print(ans-(B-mid_high)+(mid_low-A))
    elif A==B:
        print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from math import sqrt
def inpl(): return map(int, input().split())
Q = int(input())

def check(X, m):
    if (X-1)//m > m:
        return True
    else:
        return False

for q in range(Q):
    A, B = inpl()
    if A > B:
        A, B = B, A

    X = A*B
    if X <= 2:
        print(0)
        continue
    elif X == 3:
        print(1)
        continue
    
    L = int(sqrt(X))
    if L*L == X:
        L -= 1

    ok, ng = 1, X+1
    while ng - ok > 1:
        m = (ok+ng)//2
        if check(X, m):
            ok = m
        else:
            ng = m
    
    if A == B:
        print(L*2)
    else:
        if L - ok:
            if X-1//L == L:
                if L == A:
                    print(ok*2)

        tmp = L+ok
        a = (X-1)//B
        if a <= 1 or ((X-1)//a == B and  (X-1)//(a-1) == B-1):
            print(tmp - 2)
        else:
            print(tmp-1)

Submission status: Wrong Answer

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

for i in range(Q):
  a, b = map(int, input().split())
  ab = a * b
  ans = math.floor(2 * math.sqrt(ab)) - 1
  if math.floor(math.sqrt(ab))**2 == ab:
    ans -= 1
  if a != b:
    ans -= 1
  print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

Q = read()

for _ in range(Q):
  (A, B) = reads()
  if A == B == 1:
    print(0)
    continue
  
  P = A * B - 1
  sqrt = int(P ** 0.5)
  
  if P // sqrt == sqrt:
    ans = sqrt * 2 - 1
  else:
    ans = sqrt * 2

  if A <= sqrt or B <= sqrt:
    ans -= 1
  
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def solve(a, b):
    ans = 0
    cur = int(a * b)

    left = 1
    right = cur + 1
    while left < right:
        mid = int((left + right)/2)
        z = int(mid/2)
        mx = z * (mid - z)
        if mx <= cur:
            ans = mid
            left = mid + 1
        else:
            right = mid

    if int(ans/2) * (ans - int(ans/2)) == cur and a != b:
        ans -= 1
    return ans

n = int(sys.stdin.readline())
for i in range(n):
    x = sys.stdin.readline()
    y = [int(z) for z in x.split()]
    print(int(solve(y[0], y[1]))-2)

Submission status: Wrong Answer

Solution code:
import math

Q = int(input())
for i in range(Q):
    x, y = map(int, input().split())
    score = x * y
    a = int(math.sqrt(score - 1))
    is_same = True

    b = a
    if (a + 1) * b < score:
        is_same = False

    count = 0
    if is_same:
        a -= 1
        b -= 1
    if is_same and a + 1 != x and a + 1 != y:
        count += 1
    count += (a + b)
    if x <= a:
        count -= 1
    if y <= b:
        count -= 1

    print(count)

Submission status: Wrong Answer

Solution code:
import math

q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    c = int(round(math.sqrt(a * b)))
    if a * b < 1000:
        ans = 0
        prv = a * b
        for i in range(1, a * b):
            if i == a:
                 continue
            for j in range(prv - 1, 0, -1):
                if j == b:
                    continue
                if i * j < a * b:
                    ans += 1
                    prv = j
                    break
    elif a == b:
        ans = 2 * c - 2
    elif a * b == c * c:
        ans = 2 * c - 1
    elif c * (c + 1) < a * b:
        ans = 2 * c - 2
    else:
        ans = 2 * c - 3
    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

Q = read()

for _ in range(Q):
  (A, B) = reads()
  if A == B == 1:
    print(0)
    continue
  
  P = A * B - 1
  sqrt = int(P ** 0.5)
  
  if P // sqrt == sqrt:
    ans = sqrt * 2 - 1
  else:
    ans = sqrt * 2

  if A <= sqrt:
    ans -= 1
  if B <= sqrt:
    ans -= 1

  print(ans)

Submission status: Wrong Answer

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

def solve(a,b):
  if a==b:
    return (a-1)**2

  a,b = sorted((a,b))
  c = a*b-1

  r = floor(sqrt(c))

  d = 1
  if r*(r+1) > c:
    d += 1
  if r-b == a-r:
    d += 1

  return 2*r-d




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

Submission status: Wrong Answer

Solution code:
import sys

def solve(a, b):
    ans = 0
    cur = a * b

    left = 1
    right = cur + 1
    while left < right:
        mid = (left + right)/2
        mid = int(mid)
        z = mid/2
        z = int(z)
        mx = z * (mid - z)
        if mx <= cur:
            ans = mid
            left = mid + 1
        else:
            right = mid

    if int(ans/2) * (ans - int(ans/2)) == cur and a != b:
        ans -= 1
    return ans

n = int(sys.stdin.readline())
for i in range(n):
    x = sys.stdin.readline()
    y = [int(z) for z in x.split()]
    print(solve(y[0], y[1])-2)

Submission status: Wrong Answer

Solution code:
import math 
Q=int(input())
A=[0 for i in range(Q)]
B=[0 for i in range(Q)]
for i in range(Q):
    A[i],B[i]=map(int,input().split())
for i in range(Q):
    C=A[i]*B[i]
    N=int(math.sqrt(C))
    D=0
    if N**2==C:
        N-=1
    else:
        while(True):
            if A[i]*B[i]<N**2:
                N-=1
            elif A[i]*B[i]>(N+1)**2:
                N+=1
            else:
                break
            print(N)
    M=2*N-1
    print(M)

Submission status: Wrong Answer

Solution code:
import math

q = int(input())

for i in range(q):
    a, b = map(int, input().split())
    sq = (a * b) ** 0.5
    porog = 10 ** 18
    ans = int(math.modf(sq * 2 - 2)[1])
    if a == 1:
        ans -= 1
    if b == 1:
        ans -= 1
    if a * b > porog:
        ans += 1
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

q = int(input())

for i in range(q):
    a, b = map(int, input().split())
    sq = (a*b)**0.5
    ans = int(math.modf(sq*2 - 2)[1])
    if a < 5 and b < 5:
        ans1 = 0
        ax = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        ax.pop(ax.index(a))
        bx = [9, 8, 7, 6, 5, 4, 3, 2, 1]
        bx.pop(bx.index(b))
        ax1 = []
        bx1 = []
        for ai in ax:
            if ai in ax1:
                continue
            for bi in bx:
                if bi in bx1:
                    continue
                if ai*bi < a*b:
                    ax1.append(ai)
                    bx1.append(bi)
                    ans1 += 1
        print(ans1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
# D
import math
Q = int(input())
A_list = []
B_list = []
for _ in range(Q):
    A, B = map(int, input().split())
    A_list.append(min(A, B))
    B_list.append(max(A, B))
    
for i in range(Q):
    A = A_list[i]
    B = B_list[i]
        
    if A == B:
        print(A+B-2)
        continue
        
    R = int(math.sqrt(A*B-1))
    Q = (A*B-1) // R
    
    if Q <= R+1:
        add = min(Q - A - 1, B - R - 1)    
    elif (Q - R)**2 - 4*(A*B-Q*R) >= 0:
        add = int(((Q - R) - math.sqrt((Q - R)**2 - 4*(A*B-Q*R))) / 2)
        if (R + add)*(Q-add) == A*B:
            add -= 1
    else:
        print(xxxx)
    print((A-1) + R + add)

Submission status: Wrong Answer

Solution code:

import sys

def solve(a, b):
    ans = 0
    cur = a * b

    l = 1
    r = cur + 1
    while(l < r):
        mid = (l + r)/2
        mid = int(mid)
        z = mid/2
        z = int(z)
        mx = z * (mid - z)
        if(mx <= cur):
            ans = mid
            l = mid + 1
        else:
            r = mid

    if int(ans/2 * (ans - ans/2)) == cur:
        if a != b:
            ans-=1
    return ans

n = int(sys.stdin.readline())
for i in range(n):
    x = sys.stdin.readline()
    y = [ int(z) for z in x.split()]
    print(solve(y[0], y[1])-2)

Submission status: Wrong Answer

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

def solve(a,b):
  if a==b:
    return (a-1)**2

  a,b = sorted((a,b))
  c = a*b-1

  r = floor(sqrt(c))
  # check b side
  d = -1
  y = b-1
  x = c//y
  while r <= y:
    x += 1
    if c//x < y:
      break
    else:
      y -= 1
  else:
    d -= 1

  if r*(r+1) > c:
    d -= 1

  return 2*r+d




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

Submission status: Wrong Answer

Solution code:
import math

Q = int(input())
for i in range(Q):
    x, y = map(int, input().split())
    score = x * y
    a = int(math.sqrt(score - 1))
    is_same = True

    b = a
    if (a + 1) * b < score:
        is_same = False
    while a * b >= score:
        a = a - 1
        if a * b < score:
            break
        b = b - 1

    count = a + b
    if x <= a:
        count -= 1
    if y <= b:
        count -= 1
    if is_same:
        count -= 1

    print(count)

Submission status: Wrong Answer

Solution code:
# D
import math
Q = int(input())
A_list = []
B_list = []
for _ in range(Q):
    A, B = map(int, input().split())
    A_list.append(min(A, B))
    B_list.append(max(A, B))
    
for i in range(Q):
    A = A_list[i]
    B = B_list[i]
        
    if A == B:
        print(A+B-2)
        continue
        
    R = int(math.sqrt(A*B-1))
    Q = (A*B-1) // R
    
    if Q <= R+1:
        add = min(Q - A - 1, B - R - 1)    
    elif (Q - R)**2 - 4*(A*B-Q*R) >= 0:
        add = int(((Q - R) - math.sqrt((Q - R)**2 - 4*(A*B-Q*R))) / 2)
        if (R + add)*(Q-add) == A*B:
            add -= 1
    else:
        min(Q - A - 1, B - R - 1)
    print((A-1) + R + add)

Submission status: Wrong Answer

Solution code:
import math

q = int(input())

for i in range(q):
    a, b = map(int, input().split())
    sq = (a*b)**0.5
    porog = 10**18
    ans = int(math.modf(sq*2 - 2)[1])
    if a == 1:
        ans -= 1
    if b == 1:
        ans -= 1
    if a*b > porog:
        ans -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from math import sqrt
def inpl(): return map(int, input().split())
Q = int(input())

def check(X, m):
    if (X)//m > m:
        return True
    else:
        return False

for q in range(Q):
    A, B = inpl()
    if A > B:
        A, B = B, A

    X = A*B
    if X <= 2:
        print(0)
        continue
    elif X == 3:
        print(1)
        continue
    
    L = int(sqrt(X))
    if L*L == X:
        L -= 1

    ok, ng = 1, X+1
    while ng - ok > 1:
        m = (ok+ng)//2
        if check(X, m):
            ok = m
        else:
            ng = m
    
    tmp = L + ok
    
    if A == B:
        print(tmp)
    else:
        if A>1 and (X-1)//(A-1) == B and (X-1)//A == B-1:
            print(tmp-2)
            raise
        elif (X-1)//A == A:
            print(tmp-2)
        else:
            print(tmp-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from math import sqrt
def inpl(): return map(int, input().split())
Q = int(input())

def check(X, m):
    if (X)//m > m:
        return True
    else:
        return False

for q in range(Q):
    A, B = inpl()
    if A > B:
        A, B = B, A

    X = A*B
    if X <= 2:
        print(0)
        continue
    elif X == 3:
        print(1)
        continue
    
    L = int(sqrt(X))
    if L*L == X:
        L -= 1

    ok, ng = 1, X+1
    while ng - ok > 1:
        m = (ok+ng)//2
        if check(X, m):
            ok = m
        else:
            ng = m
    
    tmp = L + ok
    
    if A == B:
        print(tmp)
    else:
        if A>1 and (X-1)//(A-1) == B and (X-1)//A == B-1:
            print(tmp-2)
        elif (X-1)//A == A:
            print(tmp-2)
        else:
            print(tmp-1)

Submission status: Wrong Answer

Solution code:
# D
import math
Q = int(input())
A_list = []
B_list = []
for _ in range(Q):
    A, B = map(int, input().split())
    A_list.append(min(A, B))
    B_list.append(max(A, B))
    
for i in range(Q):
    A = A_list[i]
    B = B_list[i]
        
    if A == B:
        print(A+B-2)
        continue
        
    R = int(math.sqrt(A*B-1))
    Q = (A*B-1) // R
    
    if Q <= R+1:
        add = min(Q - A - 1, B - R - 1)    
    elif (Q - R)**2 - 4*(A*B-Q*R) > 0:
        add = int(((Q - R) - math.sqrt((Q - R)**2 - 4*(A*B-Q*R))) / 2)
        if (R + add)*(Q-add) == A*B:
            add -= 1
    else:
        add = 0
    print((A-1) + R + add)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from math import sqrt
def inpl(): return map(int, input().split())
Q = int(input())

def check(X, m):
    if X//m > m:
        return True
    else:
        return False

for q in range(Q):
    A, B = inpl()
    if A > B:
        A, B = B, A

    X = A*B
    if X <= 2:
        print(0)
        continue
    elif X == 3:
        print(1)
        continue
    
    L = int(sqrt(X))
    if L*L == X:
        L -= 1

    ok, ng = 1, X+1
    while ng - ok > 1:
        m = (ok+ng)//2
        if check(X-1, m):
            ok = m
        else:
            ng = m
    
    tmp = L + ok
    
    if A == B:
        print(tmp)
    else:
        if A>1 and X-1//(A-1) == B and X-1//A == B-1:
            print(tmp-2)
        elif (X-1)//A == A:
            print(tmp-2)
        else:
            print(tmp-1)

Submission status: Wrong Answer

Solution code:
def query(x, y):
    if x * y >= 1000 or True:
        l = int((x * y * 4 - 1) ** 0.5)
        print(l - 1 - int(x != y))


def main():
    n = int(input())
    for _ in range(n):
        x, y = map(int, input().split())
        query(x, y)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

q = int(input())

for i in range(q):
    a, b = map(int, input().split())
    sq = (a*b)**0.5
    ans = int(math.modf(sq*2 - 2)[1])
    if a == 1:
        ans -= 1
    if b == 1:
        ans -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
# D
import math
Q = int(input())
A_list = []
B_list = []
for _ in range(Q):
    A, B = map(int, input().split())
    A_list.append(min(A, B))
    B_list.append(max(A, B))
    
for i in range(Q):
    A = A_list[i]
    B = B_list[i]
        
    if A == B:
        print(A+B-2)
        continue
        
    R = int(math.sqrt(A*B-1))
    Q = (A*B-1) // R
    
    if Q <= R+1:
        add = min(Q - A - 1, B - R - 1)    
    elif (Q - R)**2 - 4*(A*B-1-Q*R) > 0:
        add = int(((Q - R) - math.sqrt((Q - R)**2 - 4*(A*B-1-Q*R))) / 2)
    else:
        add = 0
    print((A-1) + R + add)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import math
Q = int(input())
for _ in range(Q):
    A, B = sorted(map(int, input().split()))
    ans = (A-1)+(B-1)
    mid_low = max(int(math.sqrt(A*B-1)), A+1)
    mid_high = (A*B-1)//mid_low
    print(ans-(B-mid_high)+(mid_low-A))

Submission status: Wrong Answer

Solution code:
import math

q = int(input())

for i in range(q):
    a, b = map(int, input().split())
    sq = (a*b)**0.5
    if a == 1 or b == 1:
        print(int(math.modf(sq*2 - 3)[1]))
    else:
        print(int(math.modf(sq*2 - 2)[1]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from math import sqrt
def inpl(): return map(int, input().split())
Q = int(input())

def check(X, m):
    if X//m > m:
        return True
    else:
        return False

for q in range(Q):
    A, B = inpl()
    if A > B:
        A, B = B, A

    X = A*B
    if X <= 2:
        print(0)
        continue
    elif X == 3:
        print(1)
        continue
    
    L = sqrt(X)
    if int(L) == L:
        L = int(L)-1
    else:
        L = int(L)

    ok, ng = 1, X+1
    while ng - ok > 1:
        m = (ok+ng)//2
        if check(X, m):
            ok = m
        else:
            ng = m
    
    tmp = L + ok
    
    if A == B:
        print(tmp)
    else:
        if A>1 and X-1//(A-1) == B and X-1//A == B-1:
            print(tmp-2)
        elif (X-1)//A == A:
            print(tmp-2)
        else:
            print(tmp-1)

Submission status: Wrong Answer

Solution code:
import math

N=int(raw_input())
for n in range(N):
  a,b=map(int, raw_input().split())
  a,b=min(a,b),max(a,b)
  tmp=int(math.sqrt(a*b-1))
  ans=tmp*2
  if ans!=0:
      if a<=tmp: ans-=1
      if (a*b-1)/tmp== a:
          ans-=1
      elif (a*b-1)/tmp == tmp:
          ans-=1
  print ans
  """  if a==b:    ans-=0
  else: ans-=1
  if (a*b-1)/a == a: ans-=1
  if tmp*tmp==a*b-1: ans-=1
  print max(ans,0)
  """

Submission status: Wrong Answer

Solution code:
Q = int(input())
for i in range(Q):
	A,B = map(int,input().split())
	if A == 1 and B == 1:
		print(0)
		continue
	score = A * B
	root = int((score-1) ** 0.5)
	ans = 2 * root
	if root >= A:
		ans -= 1
	if root >= B:
		ans -= 1
	if (score - 1) // root == root:
		ans -= 1
	print(ans)

Submission status: Wrong Answer

Solution code:
Q = int(input())
for i in range(Q):
	A,B = map(int,input().split())
	if A == 1 and B == 1:
		print(0)
	continue
	score = A * B
	root = int((score-1) ** 0.5)
	ans = 2 * root
	if root >= A:
		ans -= 1
	if root >= B:
		ans -= 1
	if (score - 1) // root == root:
		ans -= 1
	print(ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt,floor

def solve(a,b):
  c = a*b
  r = floor(sqrt(c-1))
  if a == b:
    return 2*r
  if r*(r+1) < c:
    return 2*r-1
  else:
    return 2*r-2


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

Submission status: Wrong Answer

Solution code:
q=int(raw_input())
for i in range(q):
	a,b=map(int,raw_input().split())
	p=int((a*b-1)**0.5)
	r=p*2-1
	if p*p+p>a*b-1:
		r=r-1
	if a==b:
		r=r+1
	print r

Submission status: Wrong Answer

Solution code:
import math

N=int(raw_input())
for n in range(N):
  a,b=map(int, raw_input().split())
  a,b=min(a,b),max(a,b)
  tmp=int(math.sqrt(a*b-1))
  ans=tmp*2
  if a==b:    ans-=0
  else: ans-=1
  if (a*b-1)/a == a: ans-=1
  if tmp*tmp==a*b-1: ans-=1
  print max(ans,0)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import math
Q=int(input())
for _ in range(Q):
    A,B = list(map(int,input().split()))
    if A<B:
        A,B=B,A
    a=math.floor(math.sqrt(A*B-1))


    
    if A-B==1:
        print(2*(B-1))
#    print('a A B',a,A,B)
    elif a*a ==A*B-1:
        print((B-1)+a+max(0,a-B-1))
    else:
        print((B-1)+a+max(0,a-B))

Submission status: Wrong Answer

Solution code:
import math

N=int(raw_input())
for n in range(N):
  a,b=map(int, raw_input().split())
  a,b=min(a,b),max(a,b)
  tmp=int(math.sqrt(a*b-1))
  ans=tmp*2
  if a==b:    ans-=0
  else: ans-=1
  if (a*b-1)/a == a: ans-=1
  if tmp*tmp==a*b-1: ans-=1
  print ans

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
# MOD = 10**9 + 7

def solve(f):
    q = f.read_int()

    res = []
    for _ in xrange(q):
        a, b = f.read_int_list()
        if a*b == 1:
            res.append("0")
            continue
        sq = int(math.sqrt(a*b-1+0.05))
        ans = sq*2
        if (a*b-1)/sq == sq: ans -= 1
        if a <= sq: ans -= 1
        if b <= sq: ans -= 1
        res.append(str(ans))

    return "\n".join(res)


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    rr = []
    for _ in range(n):
        a,b = LI()
        t = a*b
        s = int(t ** 0.5)
        if s*s == t:
            rr.append((s-1)*2-1)
        elif s*(s+1) < t:
            rr.append(s*2-1)
        else:
            rr.append((s-1)*2)


    return '\n'.join(map(str, rr))


print(main())

Submission status: Wrong Answer

Solution code:
q=int(raw_input())
for i in range(q):
	a,b=map(int,raw_input().split())
	r=int(((a*b-1)**0.5)*2)-2
	if a==b:
		r=r+1
	print r

Submission status: Time Limit Exceeded

Solution code:

Q = int(input().strip('\n'))
for i in range(Q):
    inDatas = input().strip('\n').split(' ')
    a, b = list(map(int, inDatas))
    score = a*b
    if a < b:
        tmp = a
        a = b
        b = tmp
    pa = b
    pb = a - 1
    count = b - 1 + b - 1
    while pb >= b+1:
        if pa == a: pa += 1
        elif pb == b: pb -= 1
        else:
            while pa*pb >= score or pb == b:
                pb -= 1
            if pb >= 1:
                count += 1
                pa += 1
                pb -= 1
    print(count)