题意:
有从原点开始,斜率分别为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 #include9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include