hdu 6209 The Intersection
2017-09-18 20:54
197 查看
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 the x-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.
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 integer K (1≤K≤100000),
which is the only coefficient.
Output
For each test case, output the nearest rational number to x.
Express the answer in the simplest fraction.
Sample Input
5
1
2
3
4
5
Sample Output
1/1
153008/96389
50623/24337
96389/38252
226164/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;
}
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 the x-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.
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 integer K (1≤K≤100000),
which is the only coefficient.
Output
For each test case, output the nearest rational number to x.
Express the answer in the simplest fraction.
Sample Input
5
1
2
3
4
5
Sample Output
1/1
153008/96389
50623/24337
96389/38252
226164/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;
}
相关文章推荐
- Hdu 6209 The Intersection(Stern-Brocot tree+二分)
- HDU 6209 数学
- HDU 6209 The Intersection(二分)
- HDU 6209 二分分数
- HDU - 6209
- 【2017青岛网络赛】1004 hdu 6209 The Intersection 二分
- [HDU 6209] The Intersection
- HDU 6209 The Intersection(二分)
- HDU - 4277(暴力加+hash)
- HDU 1317 XYZZ(Floyd(传递闭包)+bellmanfd(判断正权环))
- hdu1709 The Balance (放入+取出 01背包)
- Hdu 1051(贪心思想)
- hdu acmsteps 2.1.8 Leftmost Digit
- hdu 4790 Just Random
- hdu 2838 树状数组
- HDU 1025 子序列
- hdu 4507 吉哥系列故事——恨7不成妻
- HDU-2602Bone Collector
- HDU 4166 Robot Navigation
- HDU 1043 Eight(A* + 奇偶剪枝 + 康拓展开)