Everyone loves short problem statements.

Given a function $ f(x) $ find its minimum value over the range $ 0 < x < π/2$
f(x) = ( x^{2} + b*x + c ) / sin( x )

------ Input: ------

First-line will contain $T$, the number of test cases. Then the test cases follow. 
Each test case contains a single line of input, two real numbers $b, c$. 

------ Output: ------
For each test case, output the minimum value of $ f(x) $ over the given range. Absolute error of $10^{-6}$ is allowed.

------ Constraints  ------
$1 ≤ T ≤ 100000$
$1 ≤ b,c ≤ 20$

------ Sample Input: ------
	1
	2 2

------ Sample Output: ------
	5.8831725615

Difficulty: HARD
Tags: Sorting

Complete search
Skill types: Sorting

Complete search

Starter code:

Solutions:
import math

def function(x, b, c):
	return (x ** 2 + b * x + c) / math.sin(x)

def ternary_search(l, r, precision, b, c):
	count = 0
	while r - l > precision:
		partition = (r - l) / 3
		m1 = l + partition
		m2 = r - partition
		fm1 = function(m1, b, c)
		fm2 = function(m2, b, c)
		if fm1 > fm2:
			l = m1
		elif fm1 < fm2:
			r = m2
		else:
			l = m1
			r = m2
		count += 1
	return l
cant = int(input())
for i in range(cant):
	(b, c) = map(float, input().split())
	print(function(ternary_search(0, math.pi / 2, 10 ** (-6), b, c), b, c))


import math

def f(x):
	return (x ** 2 + b * x + c) / math.sin(x)

def busqueda(f, l, r, precision):
	for i in range(50):
		m1 = l + (r - l) / 3
		m2 = r - (r - l) / 3
		if f(m1) > f(m2):
			l = m1
		else:
			r = m2
	return (l + r) / 2
cant = int(input())
for i in range(cant):
	(b, c) = map(float, input().split())
	print(f(busqueda(f, 0, math.pi / 2, 10 ** (-6))))


import math

def function(x):
	if x != 0:
		return float((x ** 2 + b * x + c) / math.sin(x))
for _ in range(int(input())):
	(b, c) = map(float, input().strip().split())
	l = 0
	r = math.pi / 2
	for _ in range(25):
		aux = (r - l) / 3
		m1 = l + aux
		m2 = r - aux
		f1 = function(m1)
		f2 = function(m2)
		if f1 <= f2:
			r = m2
		elif f2 <= f1:
			l = m1
	average = (l + r) / 2
	print(round(function(average), 10))


from math import sin, pi

def func(x, b, c):
	return (x * x + b * x + c) / sin(x)
for _ in range(int(input())):
	(b, c) = map(float, input().split())
	start = 0
	end = pi / 2
	while end - start > 1e-06:
		m1 = start + (end - start) / 3
		m2 = end - (end - start) / 3
		fm1 = func(m1, b, c)
		fm2 = func(m2, b, c)
		if fm1 > fm2:
			start = m1
		elif fm1 < fm2:
			end = m2
		else:
			start = m1
			end = m2
	print(func(start, b, c))


from math import sin, pi

def f(x):
	return (x ** 2 + b * x + c) / sin(x)

def ts(left, right, depht):
	for i in range(depht):
		p1 = left + (right - left) / 3
		p2 = right - (right - left) / 3
		f2 = f(p1)
		f3 = f(p2)
		if f2 <= f3:
			right = p2
		if f2 >= f3:
			left = p1
	return (left + right) / 2
for _ in range(int(input())):
	(b, c) = map(float, input().split())
	x = ts(0, pi / 2, 50)
	print(f(x))


from math import sin, pi

def function(x):
	return (x ** 2 + b * x + c) / sin(x)

def ternary_search():
	left = 0
	right = pi / 2
	for _ in range(100):
		mid1 = left + (right - left) / 3
		mid2 = right - (right - left) / 3
		f1 = function(mid1)
		f2 = function(mid2)
		if f1 == f2:
			return f1
		elif f1 < f2:
			right = mid2
		elif f1 > f2:
			left = mid1
	return function((left + right) / 2)
aws_dict = {}
T = int(input())
for _ in range(T):
	(b, c) = map(float, input().split())
	if f'{b}_{c}' not in aws_dict:
		aws = ternary_search()
		aws = round(aws, 10)
		aws_dict[f'{b}_{c}'] = aws
	else:
		aws = aws_dict[f'{b}_{c}']
	print(aws)


import math

def func(x, b, c):
	ans = (x * x + b * x + c) / math.sin(x)
	return ans

def findMin(b, c):
	start = 0.0
	end = 1.57079632679
	while end - start > 1e-06:
		m1 = start + (end - start) / 3
		m2 = end - (end - start) / 3
		fm1 = func(m1, b, c)
		fm2 = func(m2, b, c)
		if fm1 > fm2:
			start = m1
		elif fm2 > fm1:
			end = m2
		else:
			start = m1
			end = m2
	mn = func(start, b, c)
	return mn

def solve():
	for _ in range(int(input())):
		(b, c) = map(float, input().split())
		ans = findMin(b, c)
		print(ans)
solve()


from math import *
tc = 1
tc = int(input())
for _ in range(tc):
	(b, c) = map(float, input().split())
	error = 10 ** (-6)
	(l, r) = (error, pi / 2 - error)

	def f(x):
		return (x ** 2 + b * x + c) / sin(x)
	while r - l > error:
		mid = (r - l) / 3
		m1 = l + mid
		m2 = r - mid
		if f(m1) > f(m2):
			l = m1
		else:
			r = m2
	print(f(l))


import math
(b, c) = (0, 0)

def f(x):
	return (x * x + b * x + c) / math.sin(x)

def m(x):
	return (2 * x + b) * math.sin(x) - (x * x + b * x + c) * math.cos(x)
for t in range(int(input())):
	(b, c) = map(float, input().split())
	(LL, HL) = (0.0, math.pi / 2)
	x = (LL + HL) / 2
	while abs(m(x)) > math.pow(10, -6):
		if m(x) > 0:
			HL = x
			x = (LL + HL) / 2
		else:
			LL = x
			x = (LL + HL) / 2
	print(f(x))


import math

def f(x, b, c):
	return (x * x + b * x + c) / math.sin(x)
test_cases = int(input())
for _ in range(test_cases):
	numbers = list(map(float, input().split()))
	b = numbers[0]
	c = numbers[1]
	low = 0
	high = math.pi / 2
	tries = 25
	while tries > 0:
		stretch = (high - low) / 3.0
		v1 = low + stretch
		v2 = high - stretch
		f1 = f(v1, b, c)
		f2 = f(v2, b, c)
		if f1 >= f2:
			low = v1
		if f1 <= f2:
			high = v2
		if low == high:
			break
		tries -= 1
	print(f((low + high) * 0.5, b, c))


from math import sin, pi

def f(x, b, c):
	return (x * x + b * x + c) / sin(x)

def ternary(low, high):
	(p1, p2) = (low + (high - low) / 3, high - (high - low) / 3)
	if f(p1, b, c) <= f(p2, b, c):
		high = p2
	elif f(p1, b, c) >= f(p2, b, c):
		low = p1
	return (low, high)
t = int(input())
for _ in range(t):
	(b, c) = list(map(float, input().split()))
	low = 0
	high = pi / 2
	x = 0
	while x < 25:
		(low, high) = ternary(low, high)
		x += 1
	print(f((low + high) * 0.5, b, c))


import math

def f(x, b, c):
	return (x * x + b * x + c) / math.sin(x)
t = int(input())
for i in range(t):
	num = [float(j) for j in input().split(' ')]
	b = num[0]
	c = num[1]
	l = 0
	r = 3.1416 / 2
	times = 30
	while times > 0:
		d = (r - l) / 3.0
		m1 = l + d
		m2 = r - d
		f1 = f(m1, b, c)
		f2 = f(m2, b, c)
		if f1 >= f2:
			l = m1
		if f2 >= f1:
			r = m2
		if l == r:
			break
		times -= 1
	print(f((l + r) * 0.5, b, c))


import math

def fun(x, b, c):
	return (x ** 2 + b * x + c) / math.sin(x)
for _ in range(int(input())):
	d = input().split(' ')
	b = float(d[0])
	c = float(d[1])
	l = 0
	r = math.pi / 2
	for _ in range(25):
		dist = (r - l) / 3
		corte1 = l + dist
		corte2 = r - dist
		if fun(corte1, b, c) <= fun(corte2, b, c):
			r = corte2
		else:
			l = corte1
	print(fun(corte1, b, c))


from math import sin, pi

class Solution:

	def f(self, x, b, c):
		return (x ** 2 + b * x + c) / sin(x)

	def solve(self, b, c):
		eps = 10 ** (-9)
		(l, r) = (eps, pi / 2 - eps)
		while r - l > eps:
			m1 = l + (r - l) / 3
			m2 = r - (r - l) / 3
			if self.f(m1, b, c) > self.f(m2, b, c):
				l = m1
			else:
				r = m2
		print(f'{self.f(l, b, c):.10f}')
sol = Solution()
for _ in range(int(input())):
	(b, c) = [float(x) for x in input().split()]
	sol.solve(b, c)


import math
b = 0
c = 0
try:
	outList = []
	T = int(input())

	def f(x):
		y = (x ** 2 + b * x + c) / math.sin(x)
		return y
	for i in range(T):
		inputValue = input().split()
		b = float(inputValue[0])
		c = float(inputValue[1])
		lower = math.exp(-8)
		upper = math.pi / 2.0
		for j in range(50):
			mid = (lower + upper) / 2.0
			if math.tan(mid) - (mid * mid + b * mid + c) / (2.0 * mid + b) <= 0:
				lower = mid
			else:
				upper = mid
		outList.append(f(lower))
except:
	pass
for i in outList:
	print(i)


from math import *

def func(x):
	return (x * x + b * x + c) / sin(x)

def diff(x):
	return (2 * x + b) * sin(x) - (x * x + b * x + c) * cos(x)
t = int(input())
for i in range(0, t):
	bc = list(map(float, input().split()))
	b = bc[0]
	c = bc[1]
	l = 0
	h = pi / 2
	m = (l + h) / 2
	for j in range(50):
		if diff(m) > 0:
			h = m
			m = (l + h) / 2
		else:
			l = m
			m = (l + h) / 2
	print(func(m))


import math
t = int(input())

def fn(x, b, c):
	return (x ** 2 + b * x + c) / math.sin(x)

def fn_der(x, b, c):
	return (2 * x + b) * math.sin(x) - (x ** 2 + b * x + c) * math.cos(x)

def answer(rmin, rmax, b, c):
	if abs(fn_der(rmin, b, c)) < 1e-08:
		return rmin
	if fn_der((rmin + rmax) / 2, b, c) < 0:
		return answer((rmin + rmax) / 2, rmax, b, c)
	else:
		return answer(rmin, (rmin + rmax) / 2, b, c)
for i in range(t):
	(b, c) = map(float, input().split())
	rmin = 0
	rmax = math.pi / 2
	print(fn(answer(rmin, rmax, b, c), b, c))


import math

def f(x, b, c):
	return (x * x + b * x + c) / math.sin(x)
t = int(input())
ans = []
for i in range(0, t):
	domainStart = 1e-06
	domainEnd = 1.570796
	iterations = 1
	bc = list(map(float, input().split()))
	b = bc[0]
	c = bc[1]
	while iterations < 150:
		x = (domainStart + domainEnd) / 2
		if f(x, b, c) > f(x - 1e-06, b, c) and f(x, b, c) < f(x + 1e-06, b, c):
			domainEnd = x
		elif f(x, b, c) < f(x - 1e-06, b, c) and f(x, b, c) > f(x + 1e-06, b, c):
			domainStart = x
		else:
			k = (x * x + b * x + c) / math.sin(x)
			k = round(k, 10)
			ans.append(k)
			break
		iterations = iterations + 1
for x in ans:
	print(x)


from math import sin, cos, pi

def f(x):
	return (x ** 2 + b * x + c) / sin(x)

def df(x):
	return (2 * x + b) * sin(x) - cos(x) * (x ** 2 + b * x + c)
for _ in range(int(input())):
	(b, c) = map(float, input().split())
	low = 0
	high = pi / 2
	while low < high:
		mid = (low + high) / 2
		if abs(df(mid)) < 0.0013:
			print(f(mid))
			break
		elif df(mid) < 0:
			low = mid
		else:
			high = mid


from math import pi, sin, cos
(c, b) = (0, 0)

def slope(x):
	return (2 * x + b) * sin(x) - (x * x + b * x + c) * cos(x)

def f(x):
	return (x * x + b * x + c) / sin(x)
for t in range(int(input())):
	(b, c) = map(float, input().split())
	(mn, mx, x) = (0, pi / 2, pi / 4)
	for i in range(25):
		if slope(x) > 0:
			mx = x
			x = mn + mx
			x /= 2
		else:
			mn = x
			x = mx + mn
			x /= 2
	print(f(x))


from math import *

def f(x, b, c):
	return (x ** 2 + b * x + c) / sin(x)

def f_d(x, b, c):
	return (2 * x + b) * sin(x) - (x ** 2 + b * x + c) * cos(x)

def solve(l, h, b, c):
	e = 0.0013
	mid = (l + h) / 2
	if abs(f_d(mid, b, c)) < e:
		return mid
	if f_d(mid, b, c) < 0:
		return solve(mid, h, b, c)
	else:
		return solve(l, mid, b, c)
t = int(input())
for i in range(t):
	l = list(map(float, input().rstrip().split()))
	a1 = l[0]
	a2 = l[1]
	mini = solve(0, 3.141592 / 2, a1, a2)
	print(f(mini, a1, a2))


from math import *

def f(x, b, c):
	num = x ** 2 + b * x + c
	denom = sin(x)
	return num / denom

def f_prime(x, b, c):
	first = (2 * x + b) * sin(x) - (x ** 2 + b * x + c) * cos(x)
	return first

def solve(min_, max_, b, c):
	if abs(f_prime(min_, b, c)) < 1e-08:
		return min_
	if f_prime((min_ + max_) / 2, b, c) < 0:
		return solve((min_ + max_) / 2, max_, b, c)
	else:
		return solve(min_, (min_ + max_) / 2, b, c)
t = int(input())
for _ in range(t):
	(b, c) = [float(d) for d in input().split()]
	min_ = 0
	max_ = pi / 2
	print(f(solve(min_, max_, b, c), b, c))


from math import sin, pi

def calc(x):
	return (x ** 2 + b * x + c) / sin(x)

def ternary_search():
	left = 0
	right = pi / 2
	absolute_precision = 1e-06
	while True:
		if abs(right - left) < absolute_precision:
			return (calc(left) + calc(right)) / 2
		left_third = left + (right - left) / 3
		right_third = right - (right - left) / 3
		if calc(left_third) > calc(right_third):
			left = left_third
		elif calc(right_third) > calc(left_third):
			right = right_third
		else:
			left = left_third
			right = right_third
t = int(input())
for i in range(t):
	(b, c) = [float(i) for i in input().split()]
	print('%.10f' % ternary_search())


import math
testcases = int(input())

def f(x):
	return (x * x + b * x + c) / math.sin(x)

def slope(x):
	return (2 * x + b) * math.sin(x) - (x * x + x * b + c) * math.cos(x)
for i in range(testcases):
	(b, c) = list(map(float, input().split()))
	mini = 0
	maxi = math.pi / 2
	mid = math.pi / 4
	for i in range(25):
		if slope(mid) > 0:
			maxi = mid
			mid = (maxi + mini) / 2
		else:
			mini = mid
			mid = (maxi + mini) / 2
	print(f(mid))


import sys
import math
input = sys.stdin.readline

def binary(l, r, co, b, c):
	x = (l + r) / 2
	val1 = (2 * x + b) * math.sin(x)
	val2 = (x ** 2 + b * x + c) * math.cos(x)
	x = (l + r) / 2
	val = val1 - val2
	if abs(val) < 1e-07 or co == 150:
		return (l + r) / 2
	if val < 0:
		return binary((l + r) / 2, r, co + 1, b, c)
	else:
		return binary(l, (l + r) / 2, co + 1, b, c)
t = int(input())
for _ in range(t):
	(b, c) = map(float, input().split())
	x = binary(1e-10, math.pi / 2 - 1e-10, 0, b, c)
	val = (x * x + b * x + c) / math.sin(x)
	print(val)


import math

def func(x, b, c):
	ans = x * x + b * x + c
	ans /= math.sin(x)
	return ans
for asd in range(int(input())):
	(b, c) = map(float, input().split())
	st = 0
	en = math.pi / 2
	iterations = 0
	ans = func(en, b, c)
	while en - st > 1e-06:
		if iterations > 100:
			break
		x1 = st + (en - st) / 3
		x2 = en - (en - st) / 3
		fx1 = func(x1, b, c)
		fx2 = func(x2, b, c)
		if abs(fx2 - fx1) < 1e-08:
			break
		ans = min(ans, min(fx1, fx2))
		if fx2 < fx1:
			st = x1
		else:
			en = x2
		iterations += 1
	print('%.12f' % ans)