题意:

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.

思路:先大概确定范围,再二分。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x7fffffff int main(){
int a,b,x,y;
double ans = INF;
while(scanf("%d%d",&x,&y) == 2){
if(y > x){
printf("-1\n");
continue;
}
else if(y == 0){
printf("0.000000000001\n");
continue;
}
b = x+y;
int k = 1;
int t ;
ans = INF ;
for(; ;){
t = k;
k *= 2;
if(b*1.0/k >= y)
ans = ans < b*1.0/k ? ans : b*1.0/k ;
else
break;
}
while(t<k){
int mid = (t+k)/2;
if(mid%2 == 1)
break;
if(b*1.0/mid >= y)
t = mid ;
else
k = mid ;
}
if(t == 1)
t = 2;
if(k == 1)
k = 2;
double s1 = b*1.0/k;
double s2 = b*1.0/t;
if(s1 >= y && s2 >= y)
ans = s1 > s2 ? s2 : s1 ;
else if(s1 >= y)
ans = s1;
else
ans = s2; b = y -x ;
if(b<0)
b = -b;
if(b == 0){
printf("%.12lf\n",(double)y);
continue;
}
k = 1;
for(; ;){
t = k;
k *= 2;
if(b*1.0/k >= y)
ans = ans < b*1.0/k ? ans : b*1.0/k ;
else
break;
}
while(t<k){
int mid = (t+k)/2;
if(mid%2 == 1)
break;
if(b*1.0/mid >= y)
t = mid ;
else
k = mid ;
}
if(t == 1)
t = 2;
if(k == 1)
k = 2;
s1 = b*1.0/k;
s2 = b*1.0/t;
if(s1 >= y && s2 >= y){
double temp = s1 > s2 ? s2 : s1 ;
ans = temp;
}
else if(s1 >= y)
ans = ans > s1 ? s1 : ans;
else if(s2 >= y)
ans = ans > s2 ? s2 : ans;
printf("%.12lf\n",ans);
} return 0;
}

CF 578A A Problem about Polyline的更多相关文章

  1. codeforces 578a//A Problem about Polyline// Codeforces Round #320 (Div. 1)

    题意:一个等腰直角三角形一样的周期函数(只有x+轴),经过给定的点(a,b),并且半周期为X,使X尽量大,问X最大为多少? 如果a=b,结果就为b 如果a<b无解. 否则,b/(2*k*x-a) ...

  2. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] C C Problem about Polyline 数学

                                                                           C. A Problem about Polyline   ...

  3. 【26.09%】【codeforces 579C】A Problem about Polyline

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. cf C. Tourist Problem

    http://codeforces.com/contest/340/problem/C #include <cstdio> #include <cstring> #includ ...

  5. CF 291E. Tree-String Problem [dfs kmp trie图优化]

    CF291E 题意:一棵树,每条边上有一些字符,求目标串出现了多少次 直接求目标串的fail然后一边dfs一边跑kmp 然后就被特殊数据卡到\(O(n^2)\)了... 因为这样kmp复杂度分析的基础 ...

  6. CF 1096D Easy Problem [动态规划]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 有一长度为n的字符串,每一字符都有一个权值,要求现在从中取出若干个字符,使得字符串中没 ...

  7. codeforces Round #320 (Div. 2) C. A Problem about Polyline(数学) D. "Or" Game(暴力,数学)

    解题思路:就是求数 n 对应的二进制数中有多少个 1 #include <iostream> #include<cstdio> using namespace std; int ...

  8. CF 33B String Problem

    对每个位置进行操作,求出最终变成相同字母的代价,然后把所有的位上代价加起来,使得总代价最小.res[i][j]表示将字母i+'a'和字母j+'a'变为相同的代价,设最终都变成字母k+'a',那么res ...

  9. 【CF contest/792/problem/E】

    E. Colored Balls time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. 解决Jenkins上git出现的“ERROR: Error fetching remote repo 'origin'”问题

    今天对清掉了Jenkins中项目的工作空间,结果构建出现“ERROR: Error fetching remote repo 'origin'”问题:网上各种找也没找到解决这个问题的方法. 后来看错误 ...

  2. poj 1466 Girls and Boys(二分匹配之最大独立集)

    Description In the second year of the university somebody started a study on the romantic relations ...

  3. [Qt] searchBox 搜索框实现

    [Qt] searchBox 搜索框实现 也就是在lineEdit中加入button.在搜索框的右边会有个小小的搜索图标,输入内容之后,搜索的图标会变成叉叉. 类中的IconHelper见我的另一篇博 ...

  4. tooltips弹出框制作

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. [iOS]数据库第三方框架FMDB详细讲解

    [iOS]数据库第三方框架FMDB详细讲解 初识FMDB iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较麻烦.于是,就出现了一系列将SQLite API进行封 ...

  6. My way to Python - Day04 - 模块

    re模块 什么是正则表达式 正则表达式,英文叫做Regular Expression.简单说,正则表达式就是一组规则,用于实现字符串的查找,匹配,以实现关于字符串的相关操作,比如替换,删除等. 正则表 ...

  7. python之面向对象(一)

    python编程分为三个阶段: 面向过程编程:根据业务逻辑从上到下垒 函数式编程:将某功能进行函数封装,使用时调用函数即可,减少代码重复量 面向对象编程:对函数进行分类和封装 理论上我们是比较鄙视面向 ...

  8. 拉姆达表达式(Lambda Expressions)

    上面两种写法是一样的 ,拉姆达表达式也是一种委托, 但引用的是匿名方法

  9. 客户Oracle数据库在插入数据的时候报超出最大长度的错误(规避风险)

    背景: 项目使用oracle数据,在开发环境测试一些正常.项目部署到客户的服务器上后,系统在添加数据的时候报错.输出错误信息,发现是“超出最大长度”的异常. 但是按照数据库的设计,添加的数据应该在允许 ...

  10. l连接远程桌面

    参考:http://www.windows7en.com/Win7/17168.html 重要命令:mstsc