题意:

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. hdu1028:整数拆分

    求整数的拆分数.. 一种解法是母函数 #include <iostream> #include <stdio.h> #include<string.h> #incl ...

  2. 找出最小的k个数

    •已知数组中的n个正数,找出其中最小的k个数. •例如(4.5.1.6.2.7.3.8),k=4,则最小的4个数是1,2,3,4 •要求: –高效: –分析时空效率 •扩展:能否设计出适合在海量数据中 ...

  3. thinkphp分页时修改last显示标题

    需要修改Page.class.php里lastSuffix为false,这样才能修改last显示标题. 然后就可以设置了 或者直接在方法中声明: $p->lastSuffix = false; ...

  4. hdu 4586 Play the Dice(概率dp)

    Problem Description There is a dice with n sides, which are numbered from 1,2,...,n and have the equ ...

  5. vue-cli 脚手架总结

    > vue-cli 的脚手架项目模板有browserify 和 webpack , 现在自己在用的是webpack , 官网给出了两个模板: webpack-simple 和 webpack 两 ...

  6. Java_.jar .war .ear 详解

      .jar 全称:                     java archive: 包含:                     class.properties文件,是文件封装的最小单元: ...

  7. C# WPF 建立无边框(标题栏)的登录窗口

    前言:笔者最近用c#写WPF做了一个项目,此前未曾做过完整的WPF项目,算是一边学一边用,网上搜了不少资料,效率当然是不敢恭维的,有时会在一些很简单的问题上纠结很长时间,血与泪的教训可不少. 不过,正 ...

  8. 你所不知道的java编程思想

    读thinking in java这本书的时候,有这么一句话“在编译单元的内部,可以有一个公共(public)类,它必须拥有与文件相同的名字” 有以下疑问: 在一个类中说可以有一个public类,那是 ...

  9. 调试EF源码

    在解决方案中添加下载好的EF的源码的引用 在新建项目中添加程序集的引用 添加配置文件中的节点 测试 获取程序集的公钥标记: 使用sn.exe命令 使用reflector

  10. (转)android客户端从服务器端获取json数据并解析的实现代码

    今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下       首先客户端从服务器端获取json数据 1.利用HttpUrlConnection   复制代码 ...