hdu 6209 The Intersection

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 (1T100000)  which is the number of test cases.
For each test case, there is a line containing the integer  K (1K100000) , 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

比赛时想用连分数直接算出答案的,结果精度死活卡不过去

赛后换了一种做法

假设答案是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;
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值