HDU 4180 扩展欧几里得
RealPhobia
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 376 Accepted Submission(s): 151
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 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
each case, the output consists of a fraction on a line by itself. The
fraction should be formatted as two integers separated by “/”.
1/4
2/3
13/21
1/2
8/13
#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 扩展欧几里得的更多相关文章
- HDU 5114 扩展欧几里得
题目大意:给你两个球的坐标 他们都往(1, 1)这个方向以相同的速度走,问你他们在哪个位置碰撞. 思路:这种题目需要把x方向和y方向分开来算周期,两个不同周期需要用扩展欧几里得来求第一次相遇. #in ...
- hdu 2669(扩展欧几里得)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2669 扩展欧几里得(裸)
#include<stdio.h> #include<iostream> #define ll __int64 ll gcd(ll a,ll b,ll &x,ll &a ...
- HDU RSA 扩展欧几里得
Problem Description RSA is one of the most powerful methods to encrypt data. The RSA algorithm is de ...
- 扩展欧几里得 hdu 1576
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 不知道扩展欧几里得的同学可以参考:https://blog.csdn.net/zhjchengf ...
- hdu 5512 Pagodas 扩展欧几里得推导+GCD
题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...
- hdu 1573 A/B (扩展欧几里得)
Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). Input 数据的第一行 ...
- hdu 1576 A/B 【扩展欧几里得】【逆元】
<题目链接> <转载于 >>> > A/B Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)( ...
- [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)
Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...
随机推荐
- 富文本编辑器Ueditor的使用
1.下载:http://ueditor.baidu.com/website/download.html. 2.解压,并放到项目webapp下. 3.jsp页面的配置. 4.配置根路径. 5.页面展示: ...
- Ajax的open方法
Ajax的open()方法有3个参数:1.method:2.url:3.boolean: 参数1有get和post两个取值 参数2是表单的action属性值 参数3:boolean的取值 当该bool ...
- JSP开发过程遇到的中文乱码问题及解决方案
对于程序猿来说,乱码问题真的很头疼,下面列举几种常见的乱码. 1.数据库编码不一致导致乱码 解决方法: 首先查看数据库编码,输入: show variables like "%char%&q ...
- 关于explain
> db.imooc_2.find({x:}).explain() { "queryPlanner" : { , "namespace" : " ...
- python操作文件目录
# 查看当前目录的绝对路径: >>> os.path.abspath('.') /Users/NaCl/Documents/GitHub #同样的道理,要拆分路径时,也不要直接去拆字 ...
- Windows下配置Jmeter环境变量
一.安装SDK 1.下载并安装sdk,安装目录为D:\Program Files (x86)\Java\jdk1.7.0_01 2.配置环境变量 1)新建系统变量:JAVA_HOME = D:\Pro ...
- form 表单 和 jQuery HTML / CSS 方法($().html 类似的样式)
1 有关链接 :http://www.runoob.com/tags/tag-form.html https://www.cnblogs.com/Jxwz/p/4509618.html https:/ ...
- CUDA直方图实例=CPU+GPU(global)+GPU(shared)
项目打包下载链接 顺便批判下CSDN上传坑爹现象,好多次都是到了95%或者99%就不动了.我……
- vue.js 三(数据交互)isomorphic-fetch
至于fetch的介绍,在这里就不多说了,官方和网络上的说明不少 之前使用jquery的Ajax朋友,可以尝试一下使用一下这个新的api 推荐使用isomorphic-fetch,兼容Node.js和浏 ...
- Flask初学者:g对象,hook钩子函数
Flask的g对象 作用:g可以可以看作是单词global的缩写,使用“from flask import g”导入,g对象的作用是保存一些在一次请求中多个地方的都需要用到的数据,这些数据可能在用到的 ...