codeforces #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 exceed10 - 9. If there is no such x then output  - 1 as the answer.

Sample test(s)
input
3 1
output
1.000000000000
input
1 3
output
-1
input
4 1
output
1.250000000000
Note

You can see following graphs for sample 1 and sample 3.

                                  

 

题意:
  有从原点开始,斜率分别为1和-1的折线周期下去,问是否存在x使得整点(a,b)在折线上,存在的话求最小的x,无解输出-1.
 
思路:由于斜率为1,所以x>=y.那么x<y的情况一定无解。
对于x>=y的情况:我们发现(a,b)点如果是落在斜率为1的折线上,那么该折线与x轴的交点为(a-b,0)
如果(a,b)点落在落在斜率为-1的折线上,那么该折线与x轴的交点为(a+b,0) 
分析可知,如果a+b为奇数,那么一定会落在斜率为-1的折线上,如果a-b为偶数,一定会落在斜率为1的折线上。
而(a+b)不为偶数和(a-b)补为偶数不可能同时成立。
因为碎玉x>=y的情况一定有解。
二分答案即可(其实还有一种比较偷懒(比较聪明?)的办法是...不去考虑是否一定有解。把二分的初始条件设置为-1就好)
不过证明无解也并不难想。
 
需要注意的是精度问题。。。eps要记得根据题目调整。。。比如这道题要求1E-9...
eps至少比要求的多两位才保险。。。。
因为忘记调整eps(因为一般题目要求都是1E-6。。。所以我eps写的是1E-8)而wa了好多次。。。。
 
 1 /*************************************************************************
 2     > File Name: code/cf/#320/C.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年11月10日 星期二 16时54分46秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21 #define fst first              
22 #define sec second      
23 #define lson l,m,rt<<1
24 #define rson m+1,r,rt<<1|1
25 #define ms(a,x) memset(a,x,sizeof(a))
26 using namespace std;
27 const double eps = 1E-10;
28 const int dx4[4]={1,0,0,-1};
29 const int dy4[4]={0,-1,1,0};
30 typedef long long LL;
31 const int inf = 0x3f3f3f3f;
32 const LL linf =1LL<<60;
33 LL a,b;
34 int dblcmp(double d)
35 {
36     return d<-eps?-1:d>eps;
37 }
38 LL bin(LL l,LL r)
39 {
40     LL res = -1;
41     while (l<=r)
42     {
43     LL mid = (l+r)>>1;
44      double x=(a+b)*1.0/(2*mid);
45     if (dblcmp(x-b)>=0) 
46     {
47         l = mid+1;
48         res = mid;
49     }
50     else
51         r = mid -1;
52     }
53     return res;
54 }
55 int main()
56 {
57   #ifndef  ONLINE_JUDGE 
58 //   freopen("in.txt","r",stdin);
59   #endif
60 
61    cin>>a>>b;
62    LL k = bin(1LL,linf);
63    if (a<b)
64     {
65     puts("-1");
66     }
67    else
68     {
69 //    LL k = bin(1LL,linf);
70     printf("%.12f\n",(a+b)*1.0/(2.0*k));
71     }
72   
73    
74  #ifndef ONLINE_JUDGE  
75   #endif
76   fclose(stdin);
77     return 0;
78 }
View Code

 

 
 
posted @ 2015-11-11 15:58  111qqz  阅读(437)  评论(0编辑  收藏  举报