题意:

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. TortoiseGit 使用教程

    原文地址:http://blog.csdn.net/ethan_xue/article/details/7749639 git的使用越来越广泛 使用命令比较麻烦,下面讲解一下tortoisegit的使 ...

  2. acdream:Andrew Stankevich Contest 3:Two Cylinders:数值积分

    Two Cylinders Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...

  3. Html5 Canvas 实现滚动的图片

    今天一直在找html5 canvas的使用实例.想画一张地图,再画个小车在上面跑.运气好找到了一个大神写的js代码.该代码实现了图片的左右来回滚动,现在粘贴在博客里记录一下: <html> ...

  4. PyCharm 4.0下载(附keygen)

    百度网盘:http://pan.baidu.com/s/1nvAdEM9  密码:xfz9

  5. VCS仿真生成vpd文件(verilog)

    VCS仿真生成vpd文件(verilog) 一.环境与文件 Linux平台  csh环境 VCS 64bit 代码文件请参考<一个简单的Verilog计数器模型> 二.开始仿真 1.com ...

  6. 使用Dataset

    string sqlStr="Select * from Tb_news"; SqlDataAdapter myDa=new SqlDataAdapter(SqlStr,myCon ...

  7. js改变div宽度

    document.getElementById('Content_Right_id').style.width = document.documentElement.clientWidth - 250 ...

  8. oc随笔四:NSString、NSNumber

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  9. 基于webrtc的多人视频会话的demo运行程序

    服务端程序: 该服务程序为windows平台下的程序,使用libevent书写,并集成了UDP的中转程序.(该服务器程序不能和客户端程序运行在同一台PC机电脑,不然服务器程序和客户端程序会抢占同一UD ...

  10. 洛谷 P1830 轰炸Ⅲ

    P1830 轰炸Ⅲ 题目提供者wanglichao1121 标签模拟矩阵洛谷原创 难度普及/提高- 题目背景 一个大小为N*M的城市遭到了X次轰炸,每次都炸了一个每条边都与边界平行的矩形. 题目描述 ...