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:
import math

q = int(input())
a = []
for i in range(q):
    a.append(list(map(int, input().split())))
for i in range(q):
    b = max(2 * int(math.sqrt(a[i][0] * a[i][1])) - 3, 0)
    while True:
        status = True
        if b % 2 == 0:
            if (b // 2 + (a[i][0] <= b // 2)) * (b // 2 + 2 - (a[i][1] >= b//2 + 2)) < a[i][0] * a[i][1] and (b // 2 + 1 + (a[i][0] <= b // 2 + 1)) * (b // 2 + 1 - (a[i][1] >= b // 2 + 1)) < a[i][0] * a[i][1]:
                b = b + 1
            else:
                break
        else:
            if ((b - 1) // 2 + (a[i][0] <= (b - 1) // 2)) * ((b + 5) // 2 - (a[i][1] >= (b + 5) // 2)) < a[i][0] * a[i][1] and ((b + 1) // 2 + (a[i][0] <= (b + 1) // 2)) * ((b + 3) // 2 - (a[i][1] >= (b + 3) // 2)) < a[i][0] * a[i][1] and ((b + 3) // 2 + (a[i][0] <= (b + 3) // 2)) * ((b + 1) // 2 - (a[i][1] >= (b + 1) // 2)) < a[i][0] * a[i][1]:
                b = b + 1
            else:
                break
    print(b - 1)

Submission status: Accepted

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

Submission status: Runtime Error

Solution code:
q=int(input())
ab=[list(map(int,input().split())) for _ in range(q)]
for a,b in ab:
    if a>b:a,b=b,a
    if a==b:
        print(2*a-2)
    elif a+1==b:
        print(2*a-2)
    else:
        c=int((a*b)**0.5)
        if c*(c+1)>=a*b:
            print(2*c-2)
        elif c**2<a*b:
            print(2*c-1)


q=int(input())
ab=[list(map(int,input().split())) for _ in range(q)]
from math import floor
for a,b in ab:
    # a*bをぎりぎりまで攻めたい
    # (1,1)とかはもったいない。(1,a*b-1)は良い。片方は√a*b未満で、片方は√a*bより大きい組み合わせ。
    # a==bならa未満の整数yについて適切な数字xを選べばy*x<a*bとできる。xは他のxと被らないように選択できる。
    if a==b:
        print(2*a-2)
    else:
        # t,t+1 組み合わせの積がa*bで抑えられているかどうか。(t+1)*(t+1)は必ずa*bを超える。
        # t未満の自然数については適切な整数を選べばa*b以下にできる。
        t=floor((a*b)**0.5)
        # (1,2*t-1),(2,2*t-2),(3,(a*b-1)//3),...,(t-1,(a*b-1)//(t-1))
        if t*t>=a*b: 
            # t*tはだめ。t*t=a*b、a!=bのとき。t未満の数字と適切な数字を選べばよいが、t未満の数字の内一つはaなので、その分を引く。
            print(2*t-3)
        elif t*(t+1)>=a*b: 
            # t*tはOK。t*(t+1)はだめ。t未満の数字と適切な数字を選べばよいが、t未満の数字の内一つはaなので、その分を引く。
            # さらに一回目tの人は2回目もtで条件を満たす。この分を足す。t!=a,t!=bなので(t,t)は選択できる。
            print(2*t-2)
        else:
            # t*tもt*(t+1)もOK。(t,t+1),(t+1,t)をまず選ぶ。あとはt未満の数字と適切な数字を選べばよいが、t未満の数字の内一つはaなので、その分を引く。
            print(2*t-1)

Submission status: Runtime Error

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
q=int(input())
for i in range(q):
    a,b=map(int,input().split())
    s=a*b
    m=int(pow(s-1,0.5))
    if (s-1)//m==m and (a!=b and abs(a-b)!=1):
        ans=m*2-2
    elif a!=b and abs(a-b)!=1:
        ans=m*2-1
    elif a==b:
        ans=m*2
    else:
        ans=(m-1)*2
    print(ans)

Submission status: Runtime Error

Solution code:
import sys,heapq
input = sys.stdin.buffer.readline

Q = int(input())

ans = []
for testcase in range(Q):
    a,b = map(int,input().split())
    p = int((a*b-1)**0.5)
    res = p*2
    if a != b:
        res -= 1
    if p == (a*b - 1)//p:
        res -= 1
    ans.append(res)

print(*ans,sep='\n')

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
Q = int(input())
AB = [list(map(int,input().split())) for i in range(Q)]

for A,B in AB:
    M = A*B
    A,B = sorted([A,B])
    ans = 0
    i = 1
    b = int(pow(M,0.5))
    if b == B:
        b -= 1
    #print(A,B,b)
    ans += (M-1)//b - A
    ans += b-1
    ans += A-1
    print(ans)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    for _ in range(n):
        a, b = map(int, input().split())
        ref = a * b - 1
        rng = int(ref ** 0.5)
        res = rng * 2
        if ref // rng == rng:
            res -= 2
        elif a > rng and b > rng:
            res -= 0
        else:
            res -= 1
        print(res)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
q = int(input())
for i in range(q):
  a,b = map(int,input().split())
  if a == b:
    print(a*2-2)
    continue
  y = max(a,b)
  z = min(a,b)
  ind = int(math.sqrt(a*b))
  if ind.is_integer():
    ind -= 1
  cnt = 0
  else:
    if ind*(ind+1) < a*b:
      if ind >= z:
        cnt = 1
      if ind+1 >= y:
        cnt = 2
      print(2*ind-cnt)
    else:
      if ind >= z:
        cnt = 1
      print(2*ind-1-cnt)

Submission status: Runtime Error

Solution code:


q = int(input())
info = [list(map(int, input().split())) for i in range(q)]

for i in range(q):
    min_, max_ = info[i]
    min_, max_ = min(min_, max_), max(min_, max_)
    mul = min_ * max_
    ans = 2 * (min_ - 1)

    ok = 0
    ng = max_ + 10
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if mid <= (mul - 1) // mid:
            ok = mid
        else:
            ng = mid
    ans += max(ok - (min_ - 1), 0)
    ans += max(ok - min_, 0)
    
    if ok == (mul - 1) // ok:
        ans -= 1
    print(ans)

Submission status: Runtime Error

Solution code:
def check(a, b):
  if b < a:
    a, b = b, a
  if b - a <= 1:
    return 2 * a - 2
  c = int((a * b - 1)**.5)
  assert(c**2 < a * b <= (c + 1)**2)
  return 2 * c - 1 - (c * (c + 1) >= a * b)

for _ in range(int(input())):
  print(check(*list(map(int, input().split()))))

Submission status: Runtime Error

Solution code:
Q=int(input())
for i in range(Q):
  a,b=map(int,input().split())
  if a>b:
    a,b=b,a
  bound_root=(a*b-1)**0.5
  bi1=int(bound_root)
  bi2=(a*b-1)//bi1
  
  if bound_root.is_integer():
    num_div=2*int(bound_root)-1
  elif bi1==bi2:
    num_div=2*bi1-1
  else:
    num_div=2*int(bound_root)  
  #print(a,b,num_div)
  
  if a>bound_root and b>bound_root:
    num_div-=0
  elif a==bound_root or b==bound_root:
    num_div-=1
  elif a<bound_root:
    num_div-=1
  
  print(num_div)

Submission status: Runtime Error

Solution code:
import sys

# import bisect

# from collections import Counter, deque, defaultdict

# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    q = int(input())

    for i in range(q):
        a, b = list(map(int, readline().split()))
        if a > b:
            a, b = b, a

        if a == b or a + 1 == b:
            print(2 * a - 2)
        else:
            ab = a * b
            sqab = math.floor(math.sqrt(ab))

        if sqab * (sqab + 1) >= ab:
            print(2 * sqab - 2)
        else:
            print(2 * sqab - 1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys

import bisect

from collections import Counter, deque, defaultdict

# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    q = int(input())

    for i in range(q):
        a, b = list(map(int, readline().split()))
        if a > b:
            a, b = b, a

        if a == b or a + 1 == b:
            print(2 * a - 2)
        else:
            ab = a * b
            sqab = math.floor(math.sqrt(ab))

        if sqab * (sqab + 1) >= ab:
            print(2 * sqab - 2)
        else:
            print(2 * sqab - 1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def floorsqrt(x):
  max_ok = 0
  min_ng = x
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if tgt*tgt<=x:
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

def K1(C, r):
  max_ok = 0
  min_ng = C
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if C-tgt>r+tgt*tgt:
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

def K2(C, r):
  max_ok = 0
  min_ng = C
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if C-tgt>r+tgt*(tgt+1):
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

Q = int(input())
for _ in range(Q):
  A, B = map(int, input().split())
  if A*B==1:
    print(0)
    continue
  
  if B<A: A,B = B,A
  
  C = floorsqrt(A*B-1)
  #print(A, B, C, C*C, A*B-1, C*C+2*C+1)
  
  # C:= floor(sqrt(AB-1))
  
  q = (A*B-1)//C
  r = (A*B-1)%C

  if q==C:
    # Case 1: (AB-1)//C == C ... r
    # a :    1 ... C     AB-1
    # b : AB-1     C ...    1
    #
    # (AB-1)//(C-k) == C+k ... r+k*k
    # K = max{k | C-k>r+k*k}
    K = K1(C, r)
    ans = 2*C-3 if B<=C+K else 2*C-2
    #print(K, B, C+K)
  
  elif q==C+1:
    # Case 2: (AB-1)//C == C+1 ... r
    # a :    1 ... C   C+1     AB-1
    # b : AB-1     C+1   C...     1
    #
    # (AB-1)//(C-k) == C+k+1 ... r+k*(k+1)
    # K = max{k | C-k>r+k*(k+1)}
    K = K2(C, r)
    ans = 2*C-2 if B<=C+K+1 else 2*C-1
 
  else:
    # Case 3: (AB-1)//C == C+2
    # a :    1 ... C   C+2     AB-1
    # b : AB-1     C+2   C...     1
    ans = 2*C if A==B else 2*C-1
    
  print(ans)

Submission status: Runtime Error

Solution code:
def floorsqrt(x):
  max_ok = 0
  min_ng = x
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if tgt*tgt<=x:
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

def K1(C, r):
  max_ok = 0
  min_ng = C
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if C-tgt>r+tgt*tgt:
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

def K2(C, r):
  max_ok = 0
  min_ng = C
  while min_ng-max_ok>1:
    tgt = (max_ok+min_ng)//2
    if C-tgt>r+tgt*(tgt+1):
      max_ok = tgt
    else:
      min_ng = tgt
  return max_ok

Q = int(input())
for _ in range(Q):
  A, B = map(int, input().split())
  if B<A: A,B = B,A
  
  C = floorsqrt(A*B-1)
  #print(A, B, C, C*C, A*B-1, C*C+2*C+1)
  
  # C:= floor(sqrt(AB-1))
  
  q = (A*B-1)//C
  r = (A*B-1)%C

  if q==C:
    # Case 1: (AB-1)//C == C ... r
    # a :    1 ... C     AB-1
    # b : AB-1     C ...    1
    #
    # (AB-1)//(C-k) == C+k ... r+k*k
    # K = max{k | C-k>r+k*k}
    K = K1(C, r)
    ans = 2*C-3 if B<=C+K else 2*C-2
    #print(K, B, C+K)
  
  elif q==C+1:
    # Case 2: (AB-1)//C == C+1 ... r
    # a :    1 ... C   C+1     AB-1
    # b : AB-1     C+1   C...     1
    #
    # (AB-1)//(C-k) == C+k+1 ... r+k*(k+1)
    # K = max{k | C-k>r+k*(k+1)}
    K = K2(C, r)
    ans = 2*C-2 if B<=C+K+1 else 2*C-1
 
  else:
    # Case 3: (AB-1)//C == C+2
    # a :    1 ... C   C+2     AB-1
    # b : AB-1     C+2   C...     1
    ans = 2*C if A==B else 2*C-1
    
  print(ans)

Submission status: Runtime Error

Solution code:
"""
N/x-1 - N/x >= 1のところを狙っていく
解aだとして、aまではAを+1ずつインクリメントでOK
a以降はN/aからbをデクリメント
"""
N = int(input())

def calc(a,b):
    N = a * b
    n = int((N-1)**0.5)
    cnta = n
    cntb = n
    if a <= n:
        cnta -= 1
    if b <= n:
        cntb -= 1
    ans = cnta + cntb
    if (N-1)//n == n:
        ans -= 1
    # print("cnt",cnta,cntb)
    print(ans)        

for i in range(N):
    a,b = map(int, input().split())
    ans = calc(a,b)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    ab = [list(map(int, input().split())) for _ in [0]*n]
    for a, b in ab:
        a, b = min(a, b), max(a, b)
        s = (a*b-1)**0.5
        s_int = int(s)
        if (a*b-1)//s_int == s_int:
            ans = 2*s_int-2
        elif a == b:
            ans = 2*s_int
        else:
            ans = 2*s_int-1
        print(ans)


main()

Submission status: Runtime Error

Solution code:
import math
r=[]
q=int(input())
for _ in range(q):
    a,b=map(int,input().split())
    p=a*b
    t = math.floor(math.sqrt(p-1))
    res = t*2-1
    if (p-1)//t ==t:res -= 1
    if a==b:res+=1
    r.append(res)

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

Submission status: Runtime Error

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

for a,b in ab:
    
    if a>b:
        a,b=b,a
    if a==b:
        print(2*(a-1))
        continue
        
    c = math.ceil((a*b)**0.5-1)
    
    if c*(c+1)>=a*b:
        print(2*(c-1))
    else:
        print(2*c-1)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    def df(c, d):
        res = c // d
        if res * d > c: res -= 1
        return res

    q = int(input())
    L = [list(map(int, input().split())) for _ in range(q)]
    for a, b in L:
        if (a, b) == (1, 1):
            print(0)
            continue
        c = a * b - 1
        rc = int(c ** 0.5)
        rc -= 1
        while (rc + 1) ** 2 < c:
            rc += 1
        ans = rc * 2
        if rc * (rc + 1) > c:
            ans -= 1
        ab = [a, b]
        for k in ab:
            ans -= 1
            if k > rc:
                y = df(c, k)
                if y == 0:
                    ans += 1
                else:
                    x = df(c, y)
                    if x != k or (df(c, k) == df(c, k - 1)):
                        ans += 1
        print(ans)

main()

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    def df(c, d):
        res = c // d
        if res * d > c: res -= 1
        return res

    q = int(input())
    L = [list(map(int, input().split())) for _ in range(q)]
    for a, b in L:
        if (a, b) == (1, 1):
            print(0)
            continue
        c = a * b - 1
        rc = int(c ** 0.5)
        rc -= 1
        while (rc + 1) ** 2 < c:
            rc += 1
        ans = rc * 2
        if rc * (rc + 1) > c:
            ans -= 1
        ab = set([a, b])
        for k in ab:
            ans -= 1
            if k > rc:
                y = df(c, k)
                if y == 0:
                    ans += 1
                else:
                    x = df(c, y)
                    if x != k or (df(c, k) == df(c, k - 1)):
                        ans += 1
        print(ans)

main()

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    q = int(input())
    for _ in range(q):
        a, b = map(int, input().split())
        c = a * b - 1
        rc = int(c ** 0.5)
        ans = rc * 2
        if c // rc == rc:
            ans -= 1
        ab = set([a, b])
        for a in ab:
            ans -= 1
            if a > rc:
                y = rc // a
                if y == 0:
                    ans += 1
                else:
                    x = rc // y
                    if x != a or (c // a == c // (a - 1)):
                        ans += 1
        print(ans)

main()

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    q = int(input())
    for _ in range(q):
        a, b = map(int, input().split())
        c = a * b - 1
        rc = int(c ** 0.5)
        ans = rc * 2 - 2
        if c // rc == rc:
            ans -= 1
        if a > rc:
            y = rc // a
            if y == 0:
                ans += 1
            else:
                x = rc // y
                if x != a or (c // a == c // (a - 1)):
                    ans += 1
        if b > rc:
            y = rc // b
            if y == 0:
                ans += 1
            else:
                x = rc // y
                if x != b or (c // b == c // (b - 1)):
                    ans += 1
        print(ans)

main()

Submission status: Runtime Error

Solution code:
t= int(input())
for i in range(t):
    k=input().split()
    a=int(k[0])
    b=int(k[1])
    c=a*b
    d=int(c**.5)
    print(2*d-1-((d*(d+1)>=c)-(c==d*d and a!=b))

Submission status: Runtime Error

Solution code:
t= int(input())
for i in range(t):
    k=input().split()
    a=int(k[0])
    b=int(k[1])
	c=a*b
    d=int(c**.5)
    print(2*d-1-((d*d)==c)-(c==d*d and a!=b))

Submission status: Runtime Error

Solution code:
#include <iostream>
using namespace std;

typedef unsigned long long ll;

ll get_upper(ll lower, ll a, ll b) { 
  ll upper = a*b/lower;
  if (upper*lower == a*b) --upper;
  return upper;
}
// index が条件を満たすかどうか
bool isOK(ll lower, ll a, ll b) {
  ll upper = get_upper(lower, a, b);
  return lower <= upper;
}

int binary_search(ll a, ll b) {
    ll left = a;
    ll right = b-1;

    /* どんな二分探索でもここの書き方を変えずにできる！ */
    while (right - left > 1) {
        int mid = left + (right - left) / 2;

        if (isOK(mid, a, b)) left = mid;
        else right = mid;
    }

    /* left は条件を満たす最大の値、right は条件を満たさない最小の値になっている */
    return left;
}

void solve(ll a, ll b)  { 
  if (a > b) swap(a, b);
  ll ans = 2*(a-1); // (a未満, bより大)の数
  ll lower = binary_search(a, b);
  ll upper = get_upper(lower, a, b);
  if (lower < a || upper >= b) {}
  else if (lower == a) { 
    if (lower < upper && upper < b) ++ans;
  }
  else {
    ++ans; // (a, b-1)
    if (lower == upper) ans += (lower-a-1)*2+1;
    else ans += (lower-a)*2;
  }
  // cout << a << " " << b << " " << lower << " " << upper << " " << ans << endl;
  cout << ans << endl;
}

int main() {
  ll q;
  cin >> q;
  for (ll i = 0; i < q; ++i) {
    ll a, b;
    cin >> a >> b;
    solve(a, b);
  }
}

Submission status: Runtime Error

Solution code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <functional>
#include <cmath>

using namespace std;
typedef long long int ll;

#define repi(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,a) repi(i,0,a)
#define rrep(i,a) for(ll i=a-1;i>=0;i--)

ll solve(ll a, ll b){
  ll sqrtab = sqrt(a * b - 1);
  if(sqrtab * (sqrtab - 1) < a * b)
    return sqrtab * 2 - 1;
  else
    return sqrtab * 2 - 2;
}

int main(){
  ll Q;
  cin >> Q;
  rep(i, Q){
    ll a, b;
    cin >> a >> b;
    cout << solve(a, b) << endl;
  }
  return 0;
}

Submission status: Runtime Error

Solution code:
import math

q = int(input())
a = [0] * q
b = [0] * q

for i in range(0, q):
    a[i], b[i] = [int(i) for i in input().split(" ")]

for i in range(0, q):
    mi = min(a[i], b[i])
    mx = max(a[i], b[i])
    p = mi * mx - 1
    s = p**(1/2)
    c = 2 * math.floor(s - 1)
    if mi <= math.floor(s - 1):
        c -= 1
    if math.floor(p / math.floor(s)) > math.floor(s):
        if math.floor(s) == mi:
            c += 1
        else:
            c += 2
    elif math.floor(p / math.floor(s)) == math.floor(s) and math.floor(s) != mi:
        c += 1
    print(c)

Submission status: Runtime Error

Solution code:
import sys
import os
import itertools
import math
f = open("C:/Users/kazuki/Documents/python/import.txt","r")
sys.stdin = f
# -*- coding: utf-8 -*-
Q = int(input())
AB = [sorted(list(map(int,input().split()))) for i in range(Q)]

for A,B in AB:
    x = math.sqrt(A*B)
    y = int(x)
    if x!=y:
        if y*(y+1)<A*B:
            print(y*2-1)
        else:
            print(y*2-2)
    else:
        if A==B:
            print(y*2-2)
        else:
            print(y*2-3)

Submission status: Runtime Error

Solution code:
Q = int(input())
AB = [sorted(list(map(int,input().split()))) for i in range(Q)]

for A,B in AB:
    x = math.sqrt(A*B)
    y = int(x)
    if x!=y:
        if y*(y+1)<A*B:
            print(y*2-1)
        else:
            print(y*2-2)
    else:
        if A==B:
            print(y*2-2)
        else:
            print(y*2-3)

Submission status: Runtime Error

Solution code:
import sys

def main():
  input = sys.stdin.readline
  Q = int(input())
  for _ in range(Q):
    A, B = map(int, input.split())
    n = int((A*B)**0.5)
    ans = 2 * n
    if A <= 2 * n and B <= 2 * n:
      ans -= 2
    else:
      ans -= 1
    print(ans)


if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import numpy as np
Q=int(raW_input())

for i in range(Q):
	A,B=map(int,raw_input().split())
	C=np.ceil(np.sqrt(A*B))-1
	print 2*C-2

Submission status: Runtime Error

Solution code:
import numpy as np
Q=int(raW_input())

for i in range(Q):
	A,B=map(int,raw_input().split())
	C=np.ceil(np.sqrt(A*B))-1
	print C-1

Submission status: Runtime Error

Solution code:
Q=int(input())
score=[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]==max_num:
        print(2*max_num-3)
    elif score[i][0]*score[i][1]>max_num*(max_num+1):
        print(2*max_num-1)
    elif score[i][0]==score[i][1]:
        print(2*max_num-2)
    else:
        print(2*max_num-2)

Submission status: Runtime Error

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)
        sys.exit()


    # 最大の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(low_x, upper_x + 1):
            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: Runtime Error

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)


    # 最大の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(low_x, upper_x + 1):
        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: Runtime Error

Solution code:
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:
        j = 0
        while True:
            j+=1
            if j*j >= A*B:
                C = j-1
                break
        if C*(C+1)>=A*B:
            n = 2*C-2
        else:
            n = 2*C-1
    print(C)

Submission status: Runtime Error

Solution code:
import math

Q = int(input())
for i in range(Q):
    A, B = [int(a) for a in input.split()]
    N = int(math.sqrt(A * B))
    if N ** 2 == A * B:
        N -= 1
    if A*B > N * (N+1):
        print(str(2*N))
    else:
        print(str(2*N))

Submission status: Runtime Error

Solution code:
import numpy as np
def f(a, b):
    if a > b:
        a, b = b, a
    t = int(np.sqrt(a*b - 1))
    l = (a*b-1) / (b+1)
    r = (a*b-1) / (b-1)
    if a == b:
        return 2*t
    elif t != (a*b-1)/t:
        return 2*t-1
    elif r - l > 1:
        return 2*t-1
    else:
        return 2*t-2

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

Submission status: Runtime Error

Solution code:
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]=list(map(int,input().split()))
    result=0
    mul=a[i]*b[i]-1
    aa=int(mul**(1/2))
    if (aa+1)**2==mul:
        aa+=1
    result=aa*2
    if aa==int(mul/aa):
        result-=1
    if aa>=a[i]:
        result-=1
    if aa>=b[i]:
        result-=1
    print(result)

Submission status: Runtime Error

Solution code:
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]=list(map(int,input().split()))
    result=0
    mul=a[i]*b[i]-1
    aa=int(mul**(1/2))
    if (aa+1)**2==mul:
        aa+=1
    result=aa*2
    if aa==int(mul/aa):
        result-=1
    if aa>=a[i]:
        result-=1
    if aa>=b[i]:
        result-=1
    if aa<a[i] and int(mul/a[i])*(a[i]+1)>mul and int(mul/a[i]+1)*(a[i]-1)<=mul:
        result-=1
    if aa<b[i] and int(mul/b[i])*(b[i]+1)>mul and int(mul/b[i]+1)*(b[i]-1)<=mul:
        result-=1
    print(result)

Submission status: Runtime Error

Solution code:
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]=list(map(int,input().split()))
for i in range(q):
    result=0
    mul=a[i]*b[i]-1
    aa=int(mul**(1/2))
    result=aa*2
    if aa==int(mul/aa):
        result-=1
    if aa>=a[i]:
        result-=1
    if aa>=b[i]:
        result-=1
    if aa<a[i] and int(mul/a[i])*(a[i]+1)>mul and int(mul/a[i]+1)*(a[i]-1)<=mul:
        result-=1
    if aa<b[i] and int(mul/b[i])*(b[i]+1)>mul and int(mul/b[i]+1)*(b[i]-1)<=mul:
        result-=1
    print(result)

Submission status: Runtime Error

Solution code:
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]=list(map(int,input().split()))
for i in range(q):
    result=0
    mul=a[i]*b[i]-1
    aa=int(mul**(1/2))
    result=aa*2
    if aa==int(mul/aa):
        result-=1
    if aa>a[i]:
        result-=1
    if aa>b[i]:
        result-=1
    if aa<=a[i] and int(mul/a[i])*(a[i]+1)>mul and int(mul/a[i]+1)*(a[i]-1)<=mul:
        result-=1
    if aa<=b[i] and int(mul/b[i])*(b[i]+1)>mul and int(mul/b[i]+1)*(b[i]-1)<=mul:
        result-=1
    print(result)

Submission status: Runtime Error

Solution code:
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]=list(map(int,input().split()))
for i in range(q):
    result=0
    mul=a[i]*b[i]-1
    aa=int(mul**(1/2))
    result=aa*2
    if aa==int(mul/aa):
        result-=1
    if aa>=a[i]:
        result-=1
    if aa>=b[i]:
        result-=1
    print(result)

Submission status: Runtime Error

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

def nums(a,b):
    ma = max([a,b])
    prod = a * b - 1
    sq = math.floor(math.sqrt(prod))
    ans = 2 * sq
    if prod // sq == sq:
        ans = ans - 1
    if a != b:
        ans = ans - 1
    return ans
for ab  in inputs:
    print(nums(ab[0],ab[1]))

Submission status: Runtime Error

Solution code:
Q = int(input())
inputs = [ list(map(int,input().split())) for i  in range(Q)  ]

def nums(a,b):
    ma = max([a,b])
    prod = a * b - 1
    sq = math.floor(math.sqrt(prod))
    ans = 2 * sq
    if prod // sq == sq:
        ans = ans - 1
    if a != b:
        ans = ans - 1
    return ans
for ab  in inputs:
    print(nums(ab[0],ab[1]))

Submission status: Runtime Error

Solution code:
from math import sqrt
Q = int(input())
que = []
for i in range(Q):
    que.append(tuple(map(int, input().split())))
for q in que:
    if q[0] > q[1]:
        q[0],q[1] = q[1],q[0]
    if q[0] == q[1]:
        print(2*q[0]-2)
    else:
        a = q[0]*q[1]-1
        p = int(sqrt(a))
        if a >= p*(p+1):
            print(2*p-1)
        else:
            print(2*p-2)

Submission status: Runtime Error

Solution code:
from math import sqrt
Q = int(input())
que = []
for i in range(Q):
    que.append(tuple(map(int, input().split())))
for q in que:
    if q[0] > q[1]:
        q[0],q[1] = q[1],q[0]
    if q[0] == q[1]:
        print(2*q[0]-2)
    elif q[0]+1 == q[1]:
        print(2*q[0]-2)
    else:
        a = q[0]*q[1]-1
        p = int(sqrt(a))
        if a >= p*(p+1):
            print(2*p-1)
        else:
            print(2*p-2)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import bisect

def binsearch(r,l,score):
    while r - l > 1:
        m = l + (r - l) / 2
        if ((score-1) // m - (score-1) // m+1) <= 1:
            r = m
        else:
            l = m
    return r

def calc(a,b):
    score = a * b
    dense = binsearch(1,score+1,score) - 1
    ret = dense + (score-1)/dense
    if min(a,b) <= dense  and min(a,b) <= (a * b - 1)/dense:
        ret = ret - 1
    return ret

def main():
    Q = int(raw_input())
    for i in range(Q):
        A, B = map(int, raw_input().split())
        ans = calc(A,B)
        print ans

main()

Submission status: Runtime Error

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<2 :
        print('0')
    elif (math.sqrt(a*b)==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: Runtime Error

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<2 :
        print('0')
    elif a==1 or b==1:
        print(2c-3)
    elif c*(c+1)<a*b:
        print(2c-1)
    else:
        print(2c-2)

Submission status: Runtime Error

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()
  print(i)
  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 == a*b: c-=1
    if(c*(c+1)>=a*b):
      print(2*c-2)
    else:
      print(2*c-1)