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:
import random
q=int(input())
a=[]
b=[]
for i in range(q):
    A,B=map(int,input().split())
    #A,B=random.randrange(1,10**6),random.randrange(1,10**6)
    a.append(A)
    b.append(B)
for i in range(q):
    A,B=max(a[i],b[i]),min(a[i],b[i])
    p=A*B-1
    sq=int(p**0.5)
    if(A==1 and B==1):
        pat=0
    else:
        pat=2*sq
        if(sq*(sq+1)>p):pat-=1
        if(B<=sq):pat-=1
        if(p//A*(A+1)>p):pat-=1
        if(A==B and A<=sq):pat+=1
    print(pat)

Submission status: Wrong Answer

Solution code:
import sys

Q=int(input())
l=[]
for i in range(Q):
	l.append(list(map(int,input().split())))

for item in l:

	score=item[0]*item[1]
	if(score>100):
		i=item[0]
		while i*i<score:
			i+=1
		i-=1
		count=i*2
		if i>min(item):
			count-=1
		print(count)
	else:

		count=0
		a=[]
		b=[]
		a.append(item[0])
		b.append(item[1])
		
		i=score-1
		hoge=1
		while i>0:
			if hoge in a or hoge in b:
				i=int(score/(hoge+1))+1
				hoge+=1
			j=1
			while i*j<score:

				if(i not in a) and (j not in b):
					count+=1
					a.append(i)
					b.append(j)
					if i not in b and j not in a:
						count+=1
						a.append(j)
						b.append(i)
					print(i)
					print(j)
					print("-----")
				j+=1
			i-=1
		print(count)

Submission status: Wrong Answer

Solution code:
import math

n = int(raw_input())

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

  if x[0] == 1:
    if x[1] < 3:
      print "0"
      continue

  q = x[0]*x[1]
  q = float(q)
  buf = math.sqrt(q)
  buf = math.floor(buf)
  buf = int(buf)
  # remove takahashi...
  ans = buf * 2 - 2
  # print ans, int(q-1)/buf, buf
  # Wait! too many removal or not?
  if int(q-1)/buf > buf and x[0] != 1:
    ans += 1
  if x[0] == 1:
    ans -= 1
  print ans

#  print int(buf*2) , int(buf*buf) - int(q-1)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
for i in range(n):
    a = [int(a) for a in input().split()]
    s = a[0]*a[1]
    h = int(math.sqrt(s))
    mv = max(a[0],a[1])
    if a[0] == h or a[1] == h:  print(min(2,h-1)*(h-1))
    else: print(min(2,h-1)*(h-1)+(1 if mv % 2 == 1 or (a[0] % 2 == 0 and a[1] % 2 == 0) else 0))

Submission status: Wrong Answer

Solution code:
import math


def get_next_int():
    return int(float(input()))


def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])


def get_next_str():
    return input()


def get_next_strs(delim=" "):
    return tuple(input().split(delim))


def get_next_by_types(*value_types, delim=" "):
    return tuple([t(x) for t, x in zip(value_types, input().split(delim))])


def calc_worst(A, B):
    mul = A * B
    root = int(math.sqrt(mul))
    ans = 0
    # print(A, B)
    for i in range(1, root + 1):
        div = int(mul / i)
        if (div == A and i == B) or (i == A or div == B):
            if not A == B:
                ans += 1
            else:
                continue
        elif div == A or div == B or i == A or i == B or i == div:
            ans += 1
            # print(div, i)
        else:
            ans += 2
            # print(div, i)
    return ans


def solve():
    N = get_next_int()
    for i in range(N):
        A, B = get_next_ints()
        print(calc_worst(A, B))

Submission status: Wrong Answer

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

def qd_func(a, b):
    if max(a, b) == 1:
        return 0
    import math
    center= int(math.sqrt(a * b))
    takahashi = a * b
    c, d = [1, takahashi-1]
    ans = 0

    if (center+1) * center >= takahashi:
        return (center-1) * 2 + 1 - 1

    if (center+1) * center < takahashi:
        return center * 2 - 1

for i in range(q):
    print(qd_func(data[i][0], data[i][1]))

Submission status: Wrong Answer

Solution code:
import math
Q = int(input())
for i in range(Q):
    a, b = map(int, input().split())
    score = a * b
    root_score = math.sqrt(score)
    if root_score == int(root_score):
        print(int(root_score * 2))
    else:
        print(int(root_score * 2))

Submission status: Wrong Answer

Solution code:
import random
q=int(input())
a=[]
b=[]
for i in range(q):
    A,B=map(int,input().split())
    #A,B=random.randrange(1,10**6),random.randrange(1,10**6)
    print(A,B)
    a.append(A)
    b.append(B)
for i in range(q):
    A,B=max(a[i],b[i]),min(a[i],b[i])
    p=A*B-1
    sq=int(p**0.5)
    if(A==1 and B==1):
        pat=0
    else:
        pat=2*sq
        if(sq*(sq+1)>p):pat-=1
        if(B<=sq):pat-=1
        if(p//A*(A+1)>p):pat-=1
        if(A==B and A<=sq):pat+=1
    print(pat)

Submission status: Wrong Answer

Solution code:
def inpl(): return list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    A, B = inpl()
    if A==B:
        ans = (A*2-1)-1
    elif abs(A-B)==1:
        ans = min(A, B)*2-2
    else:
        ans = int((A*B)**(1/2))*2-1
        
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

n = int(raw_input())

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

  if x[0] == 1:
    if x[1] < 3:
      print "0"
      continue

  q = x[0]*x[1]
  q = float(q)
  buf = math.sqrt(q)
  buf = math.floor(buf)
  buf = int(buf)
  # remove takahashi...
  ans = buf * 2 - 2
  # Wait! too many removal or not?
  if int(q-1)/buf > buf:
    ans += 1
  if x[0] == 1:
    ans -= 1
  print ans

Submission status: Wrong Answer

Solution code:
def inpl(): return list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    A, B = inpl()
    if A==B:
        ans = (A*2-1)-1
    elif abs(A-B)==1:
        ans = min(A, B)*2-2
    else:
        if int((A*B-1)**(1/2))==(A*B-1)**(1/2):
            ans = int((A*B)**(1/2))*2-1-1
        else:
            ans = int((A*B)**(1/2))*2-1
        
    print(ans)

Submission status: Wrong Answer

Solution code:
# coding=utf-8


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_2 + 1) == 0:
        max_1 = number_1 * number_2 // (number_2 + 1) - 1
    else:
        max_1 = number_1 * number_2 // (number_2 + 1)
    cases += max_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
    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: Wrong Answer

Solution code:
n = int(input().rstrip())

for i in range(n):
    a,b = map(int,input().split())
    counter = 0
    max_score = a*b
    a_list = [i for i in range(1,max_score+1)]
    b_list = [j for j in range(1,max_score+1)]
    answer_list = []
    a_list.remove(a)
    a_list.sort(reverse=True)
    b_list.remove(b)

    for b_number in b_list[:]:
        for a_number in a_list[:]:
            if a_number*b_number < max_score:
                a_list.remove(a_number)
                b_list.remove(b_number)
                answer_list.append([a_number,b_number])
                break

    print(answer_list)
    counter = len(answer_list)

    print(counter)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
for i in range(n):
    a, b = map(int, input().split())
    r = a * b
    q = math.sqrt(r)
    x = int(q)
    if r > x * (x + 1):
        print(x * 2 - 1)
    else:
        print(x * 2 - 2)

Submission status: Wrong Answer

Solution code:
q=int(input())
for i in range(q):
    a,b=map(int,input().split())
    a,b=max(a,b),min(a,b)
    s=int((a*b)**(1/2)*2)
    if (a==1 and b==1) or (a==2 and b==1) or (a==3 and b==1):
        print(0)
        continue
    if a==1 or b==1:
        print(s-3)
        continue
    else:
        print(s-2)

Submission status: Wrong Answer

Solution code:
from math import sqrt, ceil
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    ma = a*b - 1
    mi = ma + 1
    i = int(sqrt(ma))
    while(True):
        i += 1
        t = ceil(ma / i) - 1
        if t < mi:
            mi = t
        else:
            break
    if a == b == 1:
        print(0)
    else:
        print(i + mi - (2 if a == b else 3))

Submission status: Wrong Answer

Solution code:
import math
Q = int(input())
for i in range(Q):
    a, b = map(int, input().split())
    score = a * b
    root_score = math.sqrt(score)
    if root_score == int(root_score):
        print(int(root_score) * 2 - 2)
    else:
        print(int(root_score) * 2 - 1)

Submission status: Wrong Answer

Solution code:
import random
q=int(input())
for i in range(q):
    a,b=map(int,input().split())
    print(random.randint(1,1000000000)-random.randint(1,1000000000))

Submission status: Wrong Answer

Solution code:
import sys

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    Q = int(input_raw[0])
    for i in range(Q):
        input_raw = read_func().strip().split()
        [A, B] = [long(input_raw[0]), long(input_raw[1])]
        score = long(A * B)
        s = 0
        se = long(1)
        fi = score
        li = []
        #for fi in range(score, 0, -1):
        while(1):
            if fi == A:
                fi -= 1
            if fi <= 0:
                break
            if long(fi * se) < score:
                #li.append((fi, se))
                #print li
                s += 1
                se += 1
                if se == B:
                    se += 1
                fi_next = score/se;
                if fi_next >= fi:
                    fi -= 1
                else:
                    fi = fi_next
            else:
                fi -= 1

            #print 'now:',s

        print s



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math

s_args = sys.stdin.read().splitlines()

q = [(int(s_q.split(' ')[0]), int(s_q.split(' ')[1])) for s_q in s_args[1:]]


for qi in q:
    res = int((math.sqrt(qi[0]*qi[1])-1) * 2)
    if qi[0] == 1 or qi[1] == 1:
        res -= 1
    print(res)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

import math

q = int(input())

arr = []
for _ in range(q):

    a, b = map(int, input().split())

    prod = a * b

    z = int(math.sqrt(prod))
    isSqNum = float(z) == math.sqrt(prod)

    res = 0

    if z > 100:
        res = z * 2 - 1
        if isSqNum: res -= 1
    else:
        rev = 0
        for x in range(1, prod):
            y = math.ceil(prod / x) - 1
            if y != rev:
                res += 1
                rev = y
        res -= 1
    
    if a == b: res += 1
    arr.append(res)

for row in arr:
    print(row)

Submission status: Wrong Answer

Solution code:
import math
q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    n = math.sqrt(a*b)
    ans = int(n*2 - 2)
    if a == 1 or b == 1:
        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())
    ma = a*b - 1
    mi = ma + 1
    i = int(sqrt(ma))
    while(True):
        i += 1
        t = ma // i
        if t < mi:
            mi = t
        else:
            break
    if a==b==2:
        print(1)
    elif a==b:
        print(i + mi - 2)
    else:
        print(i + mi - 3)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

q = int(input())

a = np.zeros(q, np.int)
b = np.zeros(q, np.int)

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

for i in range(q):
    if(a[i] > 1000 or b[i] > 1000): 
        s = np.sqrt(a[i]) * np.sqrt(b[i])
    else:
        s = np.sqrt(a[i] * b[i])
    if(s <= np.sqrt(2)):
        print("0")
    elif(s <= np.sqrt(4)):
        print("1")
    else:
        print(int(math.floor(s*2))-2)

Submission status: Wrong Answer

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

for i in range(Q):
    A, B = map(int,input().split())
    if (math.sqrt(A*B))%1 == 0:
        if A == 1 or B==1:
            print(int(math.sqrt(A*B)-1) * 2 - 1)
        else:
            print(int(math.sqrt(A*B)-1) * 2)
    else:
        if abs(A-B) == 1:
            print(int(math.sqrt(A*B)-1) * 2)
        else:
            print(int(math.sqrt(A*B)) * 2 - 1)

Submission status: Wrong Answer

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


for i in range(q):
    pro = a[i][0] * a[i][1]
    ed_r = []
    ed_l = []
    for j in range(1, pro):
        
        if 1 in ed_r:
            break
        if (pro-1) // j >= 1 and ((pro-1) // j) * j < pro and (pro-1) // j not in ed_r:
            ed_r.append((pro-1) // j)
            ed_l.append(j)


    #print(ed_r)
    #print(ed_l)
    print(len(ed_r)-1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

    root = math.floor((A*B-1)**(1/2))
    ans = root * 2
    if A <= root:
        ans = ans-1
    if B <= root:
        ans = ans-1
    if root*root == A*B-1:
        ans = ans-1
    if abs(A-B) == 1:
        ans = ans-1
    print(ans)

Submission status: Wrong Answer

Solution code:
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]:
        print(2*q[0]-2)
    else:
        a = q[0]*q[1]-1
        p = int(a**(1/2))
        if a >= p*(p+1):
            print(2*p-1)
        else:
            print(2*p-2)

Submission status: Wrong Answer

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

for i in range(Q):
    A, B = map(int,input().split())
    if (math.sqrt(A*B))%1 == 0:
        if A == 1 or B==1:
            print(int(math.sqrt(A*B)-1) * 2 - 1)
        else:
            print(int(math.sqrt(A*B)-1) * 2)
    else:
        print(int(math.sqrt(A*B)) * 2 - 1)

Submission status: Wrong Answer

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

for _ in range(q):
    a, b = [int(x) for x in input().strip().split()]
    p = a*b-1
    sq = math.sqrt(p)
    r = math.floor(sq)
    s = r*2

    if r**2 == p:
        s -= 1
    if abs(a-b) == 1:
        s -= 2
    elif a!=b:
        s -= 1

    print(s)

Submission status: Wrong Answer

Solution code:
from math import sqrt, ceil
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    ma = a*b - 1
    mi = ma + 1
    i = int(sqrt(ma))
    while(True):
        i += 1
        t = ceil(ma / i) - 1
        if t < mi:
            mi = t
        else:
            break
    print(i + mi - 3 + (1 if a == b else 0))

Submission status: Wrong Answer

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

for i in range(Q):
    a, b = A[i]
    tmp = int(math.sqrt(a * b))
    if max(a, b) > 10:
        print(tmp * 2 - 1)
    elif a * b > 4:
        print(tmp * 2 - 2)
    elif a * b > 2:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:

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

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

Submission status: Wrong Answer

Solution code:
import math
Q = int(input())
for i in range(Q):
    a, b = map(int, input().split())
    score = a * b
    root_score = math.sqrt(score)
    if root_score == int(root_score):
        print(max(int(root_score) * 2 - 1, int(root_score - 1) * 2 - 1))
    else:
        print(int(root_score) * 2 - 1)

Submission status: Wrong Answer

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 = 2 * int(math.sqrt(a[i][0] * a[i][1])) - 1
    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: Wrong Answer

Solution code:
import numpy as np
import math

q = int(input())

a = np.zeros(q, np.int)
b = np.zeros(q, np.int)

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

for i in range(q):
    if(a[i] > 1000 or b[i] > 1000): 
        s = np.sqrt(a[i]) * np.sqrt(b[i])
    else:
        s = np.sqrt(a[i] * b[i])
    if(s <= np.sqrt(2)):
        print("0")
    elif(s <= np.sqrt(4)):
        print("1")
    elif(int(round(s)) == int(math.floor(s))):
        print(int(math.floor(s))*2-2)
    else:
        print(int(math.floor(s))*2-1)

Submission status: Wrong Answer

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:
            cnt += 1
            if n ** 2 == a * b:
                cnt -= 1
            if n * (n + 1) >= a * b:
                cnt -= 1

    print(cnt)

Submission status: Wrong Answer

Solution code:
import math
Q = int(input())
ANS = [0]*Q
for q in range(Q):
    A,B = map(int,input().split())

    root = math.floor((A*B-1)**(1/2))
    ans = root * 2
    if A <= root:
        ans = ans-1
    if B <= root:
        ans = ans-1
    if root*root == A*B-1:
        ans = ans-1
    ANS[q] = ans
for q in range(Q):
    print(ANS[q])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

q = int(input())

a = np.zeros(q, np.int)
b = np.zeros(q, np.int)

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

for i in range(q):
    s = np.sqrt(a[i]) * np.sqrt(b[i])
    if(s <= np.sqrt(2)):
        print("0")
    elif(s <= np.sqrt(4)):
        print("1")
    elif(int(round(s)) == int(math.floor(s))):
        print(int(math.floor(s))*2-2)
    else:
        print(int(math.floor(s))*2-1)

Submission status: Wrong Answer

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

    root = math.floor((A*B-1)**(1/2))
    ans = root * 2
    if A <= root:
        ans = ans-1
    if B <= root:
        ans = ans-1
    if root*root == A*B-1:
        ans = ans-1
    print(ans)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
for i in range(n):
    a, b = map(int, input().split())
    r = a * b
    q = math.sqrt(r)
    x = int(q)
    if a == r or b == r:
        if q == x:
            print(x - 1)
        else:
            print(x)
    elif abs(x ** 2 - r) >= abs((x + 1) ** 2 - r):
        print(x * 2 - 1)
    else:
        print(x * 2 - 2)

Submission status: Wrong Answer

Solution code:
q = int(input())


for i in range(q):
    a, b = map(int, input().split())
    
    print(int(2*((a*b) ** (0.5)))-2)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
for i in range(n):
    a, b = map(int, input().split())
    r = a * b
    q = math.sqrt(r)
    x = int(q)
    if a == r or b == r:
        if q == x:
            print(x - 1)
        else:
            print(x)
    elif abs(x ** 2 - r) > abs((x + 1) ** 2 - r):
        print(x * 2 - 1)
    else:
        print(x * 2 - 2)

Submission status: Wrong Answer

Solution code:
import math
q=int(input())
for i in range(q):
  a,b=list(map(int,input().split()))
  c=math.sqrt(a*b-1)
  if c==int(c):
    print((c-1)*2)
  else:
    print(2*c-1)

Submission status: Wrong Answer

Solution code:
q=int(input())
a=[]
b=[]
for i in range(q):
    A,B=map(int,input().split())
    a.append(A)
    b.append(B)
for i in range(q):
    A,B=a[i],b[i]
    p=A*B-1
    sq=int(p**0.5)
    if(A==1 and B==1):print(0)
    elif(sq**2==p):
        pat=2*sq-1
        if(A<=sq):pat-=1
        if(B<=sq):pat-=1
        print(pat)
    else:
        pat=2*sq
        if(sq*(sq+1)>p):pat-=1
        if(A<=sq):pat-=1
        if(B<=sq):pat-=1
        print(pat)

Submission status: Wrong Answer

Solution code:
Q = int(input())
for q in range(Q):
    A,B = map(int,input().split())

    X = A*B-1
    a,b = 1,X
    ans = 0
    while(b>=1):
        print(a,b) #test
        if a != A and b != B:
            ans = ans+1
        elif a == A:
            a = a+1
            continue
        elif b == B:
            b = b-1
            continue
        if a<=b:
            a = a+1
            b = X//a
        else:
            b = b-1
            if b==0:
                break
            a = X//b
    print(ans)

Submission status: Wrong Answer

Solution code:
Q = int(input())
que = []
for i in range(Q):
    que.append(tuple(map(int, input().split())))
for q in que:
    p = (q[0]*q[1]-1)//(min(q)+1)
    print(p+min(q)-1)

Submission status: Time Limit Exceeded

Solution code:
'''
研究室PCでの解答
'''
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 #998244353
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    q = int(ipt())
    for _ in range(q):
        a,b = map(int,ipt().split())
        if a > b:
            a,b = b,a
        ans = a-1
        tgt = a*b
        pi = b
        for i in range(a+1,10**9+1):
            xi = (tgt-1)//i
            if i**2 > tgt:
                ans += (tgt-1)//i
                if xi == pi:
                    ans -= 1
                break
            ans += 1
            pi = xi
        print(ans)

    return None

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
'''
研究室PCでの解答
'''
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 #998244353
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    q = int(ipt())
    for _ in range(q):
        a,b = map(int,ipt().split())
        if a > b:
            a,b = b,a
        ans = a-1
        tgt = a*b
        pi = b
        for i in range(a+1,10**9+1):
            xi = (tgt-1)//i
            if xi == pi:
                ans += xi-1
                break
            ans += 1
            pi = xi
        print(ans)

    return None

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
'''
研究室PCでの解答
'''
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 #998244353
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    q = int(ipt())
    for _ in range(q):
        a,b = map(int,ipt().split())
        if a > b:
            a,b = b,a
        ans = a-1
        tgt = a*b
        a += 1
        for i in range(b-1,0,-1):
            if i*a < tgt:
                ans += 1
                a += 1
            if i**2 < tgt:
                ans += i-1
                break
        print(ans)

    return None

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
q = int(input())
for _ in range(q):
  a,b = map(int,input().split())
  ans = 0
  i = 1
  while i * i < a*b:
    x,y = i,(a*b-1)//i
    if x == a or x == b or y == a or y == b:
      ans += 1
    else:
      ans += 2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve(A, B):
    M = A * B
    cnt = 0

    x, y = A, B
    while y - x >= 1:
        x += 1
        y = M // x if M % x else M // x - 1
        cnt += 1
    cnt += y - 1

    x, y = B, A
    while y - x >= 1:
        x += 1
        y = M // x if M % x else M // x - 1
        cnt += 1
    cnt += y - 1

    return cnt


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

for A, B in Query:
    print(solve(A, B))

Submission status: Time Limit Exceeded

Solution code:
Q=int(input())
for i in range(Q):
    A,B=map(int,input().split())
    if A>B:
        A,B=B,A
    M=A*B-1
    CNT=B
    cnt=0
    for i in range(A,B):
        if CNT<=A:
            break
        if A<M//i<CNT:
            cnt+=1
            CNT=M//i
        elif M//i>=CNT and CNT-1>A:
            cnt+=1
            CNT-=1
        else:
            break
    print(cnt+(A-1)*2)

Submission status: Time Limit Exceeded

Solution code:
t= int(input())
for i in range(t):
    k=input().split()
    a=int(k[0])
    b=int(k[1])
    if a<b:
        a,b=b,a
    k_act=a+a-1
    k=k_act
    pair = 0
    prod=a*b
    for i in range(1,k+1):
        for j in range(k,0,-1):
            if i*j<prod:
                k=j-1
                pair+=1
                break
            else:
                continue
    if a==b:
        print(pair)
    else:
        print(pair-1)

Submission status: Time Limit Exceeded

Solution code:
q = int(input())
for _ in range(q):
    a, b = sorted(map(int, input().split()))
    if a == b:
        print(2 * a - 2)
    elif a + 1 == b:
        print(2 * a - 2)
    else:
        c = a
        while True:
            if (c + 1) * (c + 1) >= a * b:
                break
            c += 1

        if c * (c + 1) >= a * b:
            print(2 * c - 2)
        else:
            print(2 * c - 1)

Submission status: Time Limit Exceeded

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

def cn(A,B):
    ans = 0
    pre = B
    score = A*B
    for i in range(A+1,score+1):
        tmp = (score-1)//i
        if tmp < 1:
            break
        if tmp == pre:
            ans += pre-1
            break
        ans += 1
        pre = tmp
    return ans
for ab in AB:
    print(cn(ab[0],ab[1])+cn(ab[1],ab[0]))

Submission status: Time Limit Exceeded

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(n)

Submission status: Time Limit Exceeded

Solution code:
Q = int(input())

ABlist = []

for _ in range(Q):
    ABlist.append(list(map(int, input().split())))

def function(a,b):
    ab = a*b
    k = 1
    k_ = 2
    people = 0
    while k <= k_:
        if ab % k == 0:
            k_ = ab//k - 1
        else:
            k_ = ab//k
        if k <= k_:
            if k != k_:
                if k != a and k != b:
                    people += 2
                else: people += 1
            if k ==k_:
                if k != a and k != b:
                    people += 1
        k += 1
    return people

for i in ABlist:
    a, b = i
    print(function(a,b))

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
rankList = []

for x in range(0,num):
	inp = list(map(int, input().split()))
	rankList.append(inp)


for rank in rankList:
	count = 0
	score = rank[0] * rank[1]
	usedRnak1 = [rank[0]]
	usedRnak2 = [rank[1]]

	for r1 in range(1,rank[0]):
		if (r1 in usedRnak1) is False:
			if score % r1 == 0:
				r2 = int(score / r1) - 1
			else:
				r2 = int(score / r1)

			if (r2 in usedRnak2) is False:
				# print(str(r1)+":"+str(r2))
				count += 1
				usedRnak1.append(r1)
				usedRnak2.append(r2)

	for r2 in range(1,rank[1]):
		if (r2 in usedRnak2) is False:
			if score % r2 == 0:
				r1 = int(score / r2) - 1
			else:
				r1 = int(score / r2)
			
			if (r1 in usedRnak1) is False:
				# print(str(r1)+":"+str(r2))
				count += 1
				usedRnak1.append(r1)
				usedRnak2.append(r2)

	print(count)