[Codeforces Round #320 (Div. 2) C. A Problem about Polyline (数学)
C. A Problem about Polyline
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a polyline going through points (0, 0) – (x, x) – (2x, 0) – (3x, x) – (4x, 0) – ... - (2kx, 0) – (2kx + x, x) – ....
We know that the polyline passes through the point (a, b). Find minimum positive value x such that it is true or determine that there is no such x.
Input
Only one line containing two positive integers a and b (1 ≤ a, b ≤ 109).
Output
Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9. If there is no such x then output - 1 as the answer.
Examples
input
Copy
3 1
output
Copy
1.000000000000
input
Copy
1 3
output
Copy
-1
input
Copy
4 1
output
Copy
1.250000000000
Note
You can see following graphs for sample 1 and sample 3.
思路:
当\(a<b\)时,不存在答案,输出“-1”,下面考虑有答案的情况。
如果点\((a,b)\)在折线的上坡上,那么折线一定经过点\((a-b,0)\),如果在折线的下坡上,那么折线一定经过点\((a+b,0)\)。设$\mathit c=(a+b)\or(a-b) $
我们要求解的答案\(\mathit x\),一定满足这两个条件:
1️⃣、\(x/(2*x)\)为正数,因为\(2*x\)是折线的最小周期,而\((c,0)\)又是一个周期的结尾点。
2️⃣、\(b \leq x\) ,这也是显然的。
设\(y=c/(2*x)\)代表到点$(c,0) \(时经过了多少个最小周期,显然当\)\mathit y$ 最大时,\(\mathit x\)最小。
则\(x=c/(2*y) ,b\leq x\) ,则答案为:
代码:
int a, b;
int main()
{
#if DEBUG_Switch
freopen("C:\\code\\input.txt", "r", stdin);
#endif
//freopen("C:\\code\\output.txt","r",stdin);
cin >> a >> b;
if (a < b)
{
cout << -1 << endl;
} else
{
double ans = a + b;
ans = ans / (2.0 * (int)(ans / (2.0 * b)));
cout << fixed << setprecision(9) << ans << endl;
}
return 0;
}