hdu 6209 The Intersection
来源:互联网 发布:工程预算软件大全 编辑:程序博客网 时间:2024/06/18 15:25
Problem Description
A given coefficient K leads an intersection of two curves f(x) and gK(x) . In the first quadrant, the curve f is a monotone increasing function that f(x)=x−−√ . The curve g is decreasing and g(x)=K/x .
To calculate thex -coordinate of the only intersection in the first quadrant is the following question. For accuracy, we need the nearest rational number to x and its denominator should not be larger than 100000 .
To calculate the
Input
The first line is an integer T (1≤T≤100000) which is the number of test cases.
For each test case, there is a line containing the integerK (1≤K≤100000) , which is the only coefficient.
For each test case, there is a line containing the integer
Output
For each test case, output the nearest rational number to x . Express the answer in the simplest fraction.
Sample Input
512345
Sample Output
1/1153008/9638950623/2433796389/38252226164/77347
Source
2017 ACM/ICPC Asia Regional Qingdao Online
比赛时想用连分数直接算出答案的,结果精度死活卡不过去
赛后换了一种做法
假设答案是x,整数部分是a
一开始取区间[a/1,(a+1)/1]
假设当前区间是[a/b,c/d],那么取(a+b)/(c+d)作为中间值
和x比较后把边界修改,一直到c+d大于10w就break掉
一开始c++没过,改成g++过的
#include<map>#include<set>#include<cmath>#include<queue>#include<vector>#include<cstdio>#include<string>#include<cstring>#include<cassert>#include<iostream>#include<algorithm>using namespace std;inline int gcd(int x,int y){ int m=x%y; while(m!=0) { x=y; y=m; m=x%y; } return y;}long double ffabs(long double x) { if (x < 0) x = -x; return x;}int main(){// freopen("1004.in","r",stdin);// freopen("1004.out","w",stdout); int T; scanf("%d",&T); while(T>0) { T--; int k; scanf("%d",&k); //long double x=pow((long double)k*k,1.0/3.0); long double x=pow((long double)(k),(long double)2.0/(long double)3.0); //long double l1=0,l2=1,r1=1,r2=0; int xx=round(x); if(1LL*xx*xx*xx==1LL*k*k) { printf("%d/1\n",xx); continue; } int l1=int(x),l2=1,r1=int(x)+1,r2=1,g; long double ans1=0,ans2=0; while(1) { int xt1=l1+r1,xt2=l2+r2; g=gcd(xt1,xt2); xt1/=g; xt2/=g; if(xt2>100000) break; long double mid1=xt1,mid2=xt2; if(ans2==0||ffabs(mid1/mid2-x)<ffabs(ans1/ans2-x)) { ans1=mid1; ans2=mid2; } if(x<mid1/mid2) { //if(l2+mid2>100000) //break; r1=mid1; r2=mid2; } else if(x>mid1/mid2) { //if(r2+mid2>100000) //break; l1=mid1; l2=mid2; } else break; } int mid1=ans1,mid2=ans2; g=gcd(mid1,mid2); printf("%d/%d\n",mid1/g,mid2/g); } return 0;}
阅读全文
0 0
- hdu 6209 The Intersection
- [HDU 6209] The Intersection
- HDU 6209 The Intersection(二分)
- 【2017青岛网络赛】1004 hdu 6209 The Intersection 二分
- Hdu 6209 The Intersection(Stern-Brocot tree+二分)
- Intersection HDU
- hdu 5120 Intersection
- hdu 5120 题解 Intersection
- hdu 5120 Intersection
- hdu 5120 Intersection
- hdu 5120 Intersection
- HDU 5120 Intersection
- HDU 5120 Intersection
- hdu 5120 Intersection
- HDU.5120 Intersection
- Intersection (hdu-5120)
- HDU —5120 Intersection
- HDU 4873ZCC Loves Intersection
- jmeter 初识 no cookies
- python基础知识总结
- HDU 1157 Who's in the Middle
- loadrunner Web_类函数之Web_remove_auto_filter()
- MySQL数据的主从复制、半同步复制和主主复制详解
- hdu 6209 The Intersection
- SQL数据库简单语句操作
- 从FBTweak 源码剖析一些中阶知识
- 【2017青岛网络赛】1009 Smallest Minimum Cut hdu6214 最小割 最大流模版
- H5本地存储详细使用教程(localStorage + JSON数据存储应用框架)
- 计算机人物总结
- 监听EditText,跟随输入文字而变化,double类型取余小数点后两位
- 数据结构学习技巧
- HDU---1732 Push Box【BFS】