RealPhobia

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 376    Accepted Submission(s): 151

Problem Description
Bert
is a programmer with a real fear of floating point arithmetic. Bert has
quite successfully used rational numbers to write his programs but he
does not like it when the denominator grows large. Your task is to help
Bert by writing a program that decreases the denominator of a rational
number, whilst introducing the smallest error possible. For a rational
number A/B, where B > 2 and 0 < A < B, your program needs to
identify a rational number C/D such that:
1. 0 < C < D < B, and
2. the error |A/B - C/D| is the minimum over all possible values of C and D, and
3. D is the smallest such positive integer.
 
Input
The
input starts with an integer K (1 <= K <= 1000) that represents
the number of cases on a line by itself. Each of the following K lines
describes one of the cases and consists of a fraction formatted as two
integers, A and B, separated by “/” such that:
1. B is a 32 bit integer strictly greater than 2, and
2. 0 < A < B
 
Output
For
each case, the output consists of a fraction on a line by itself. The
fraction should be formatted as two integers separated by “/”.
 
Sample Input
3
1/4
2/3
13/21
 
Sample Output
1/3
1/2
8/13
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  4186 4181 4182 4183 4179
 
 
 
#include<stdio.h>
#include<string.h> long long gcd1(long long a,long long b,long long &x,long long &y)
{
if(b == )
{
x = ;
y = ;
return a;
}
long long d = gcd1(b,a%b,x,y);
long long t = x;
x = y;
y = t - a/b*y;
return d;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long a,b;
scanf("%lld/%lld",&a,&b);
long long x = ,y = ;
long long p = gcd1(a,b,x,y);
//printf("%lld,%lld\n",x,y);
//printf("---%lld\n",p);
//printf("==%lld %lld\n",a,b);
if(p != )
{
printf("%lld/%lld\n",a/p,b/p);
continue;
}
if(a == )
{
printf("1/%lld\n",b-);
continue;
}
long long x1 = ,y1 = ;
if(x > )
{
x1 = (a + y)%a;
y1 = (b - x)%b;
}
else
{
x1 = (a - y)%a;
y1 = (b + x)%b;
}
//printf("%lld %lld %lld %lld\n",x1,y1);
printf("%lld/%lld\n",x1,y1);
}
return ;
}

HDU 4180 扩展欧几里得的更多相关文章

  1. HDU 5114 扩展欧几里得

    题目大意:给你两个球的坐标 他们都往(1, 1)这个方向以相同的速度走,问你他们在哪个位置碰撞. 思路:这种题目需要把x方向和y方向分开来算周期,两个不同周期需要用扩展欧几里得来求第一次相遇. #in ...

  2. hdu 2669(扩展欧几里得)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. hdu 2669 扩展欧几里得(裸)

    #include<stdio.h> #include<iostream> #define ll __int64 ll gcd(ll a,ll b,ll &x,ll &a ...

  4. HDU RSA 扩展欧几里得

    Problem Description RSA is one of the most powerful methods to encrypt data. The RSA algorithm is de ...

  5. 扩展欧几里得 hdu 1576

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 不知道扩展欧几里得的同学可以参考:https://blog.csdn.net/zhjchengf ...

  6. hdu 5512 Pagodas 扩展欧几里得推导+GCD

    题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...

  7. hdu 1573 A/B (扩展欧几里得)

    Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). Input 数据的第一行 ...

  8. hdu 1576 A/B 【扩展欧几里得】【逆元】

    <题目链接> <转载于 >>> > A/B Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)( ...

  9. [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

    Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...

随机推荐

  1. 使用HANA Web-based Development Workbench创建最简单的Server Side JavaScript

    服务器端的JavaScript, 看下wikipedia的介绍: https://en.wikipedia.org/wiki/JavaScript#Server-side_JavaScript Ser ...

  2. IOS 截屏(保存到相册中)

    @interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...

  3. iOS 提交form表单,上传图片

    之前不会用,总感觉很难,用后感觉不是太难,本文只是简单的讲一下怎么使用的, //实例话对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager ...

  4. 第五章 javascript编程可养成的好习惯

    用户点击某个链接时弹出一个新窗口javascript使用window对象的open()方法来创建新的浏览器窗口,这个方法有三个参数:window.open(url,name,features)url: ...

  5. 关于后台获取不到HiddenField值的有关问题

    服务器加载aspx程序时,首先要执行后台cs文件中的page_load等方法中的代码,其它按钮事件等是不执行的,生成页面发送到客户端.客户端执行时会响应js脚本,提交到服务器后除了执行page_loa ...

  6. 2018.11.7 Nescafe29 T1 穿越七色虹

    题目 题目背景 在 Nescafe27 和 28 中,讲述了一支探险队前往 Nescafe 之塔探险的故事…… 当两位探险队员以最快的时间把礼物放到每个木箱里之后,精灵们变身为一缕缕金带似的光,簇簇光 ...

  7. C/C++程序基础 (六)面向对象

    类的特性 抽象.封装.继承.多态.重载 class 和 struct 区别 C中struct仅仅可以包含数据成员,不可以有成员函数,属于复杂数据结构. C++ 中struct成员访问权限默认为publ ...

  8. 深入理解java虚拟机读书笔记1--java内存区域

    Java在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途.创建和销毁的时间,有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,有些则是与线程一一对应,随 ...

  9. Python循环的一些基本练习

    #1:# name = input('请输入你的身份')# if name == 'egon':# print('--> 超级管理员')# elif name == 'tom':# print( ...

  10. yii2 基本的增删改查

    一:添加方法 1.1 使用成员属性的方式 save $user_name = $_POST['user_name']; $password = $_POST['password']; //实例化 $u ...