题意:一个折现的路径为:(0,0)->(x,x)->(2x,0)->(3x,x)...(2kx,0),(2kx+x,x)..
1<=a,b,<=1e9.给出(a,b):找到一个最小的正数x 使得(a,b)在x生成的折现上.无解输出-1.
设上面的x=c,p为某个非负整数.
折线的斜率为1,-1,方程有两种 y=x-2pc , y=-x+2pc
c=(x-y)/2p , c=(y+x)/2p
1<=a,b,<=1e9.给出(a,b):找到一个最小的正数x 使得(a,b)在x生成的折现上.无解输出-1.
设上面的x=c,p为某个非负整数.
折线的斜率为1,-1,方程有两种 y=x-2pc , y=-x+2pc
c=(x-y)/2p , c=(y+x)/2p
p越大c越小 并且c要>=y .二分p即可.
#include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; const int N=2e5+5; ld x,y; int main() { cin>>x>>y; //y=x-2pc //y=-x+2pc c=(y+x)/2p if(y>x) puts("-1"); else { ll l=1,r=1e9; ld res=1e15; while(l<=r) { // y=-x+2pc ll mid=l+r>>1; ld c=(x+y)/(2.0*mid); if(c>=y) l=mid+1,res=c; else r=mid-1; } l=1,r=1e9; while(l<=r) { //y=x-2pc ll mid=l+r>>1; ld c=(x-y)/(2.0*mid); if(c>=y) l=mid+1,res=min(res,c); else r=mid-1; } cout<<fixed<<setprecision(10)<<res<<'\n'; } return 0; }