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 ...
随机推荐
- nginx搭建流媒体服务器
1.安装PCRE库 到www.pcre.org 下载pcre-8.37.tar.gz tar -zxvf pcre-8.37.tar.gz cd pcre-8.37 ./configure make ...
- 《Ruby on Rails教程》学习笔记
本文是我在阅读 Ruby on Rails 教程的简体中文版时所做的摘录,以及学习时寻找的补充知识.补充知识主要来自于 Ruby on Rails 實戰聖經. Asset Pipeline 在最新版 ...
- CDQ分治入门
前言 \(CDQ\)分治是一个神奇的算法. 它有着广泛的用途,甚至在某些题目中还能取代\(KD-Tree\).树套树等恶心的数据结构成为正解,而且常数还小得多. 不过它也有一定的缺点,如必须离线操作, ...
- CentOS替换系统自带JDK
1.解压jdk安装包到/opt 下 /opt/jdk1.8.0_181 2.编辑/etc/profile, 增加如下内容 export JAVA_HOME=/opt/jdk1.8.0_181expor ...
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第九节
原文链接 第九节:使用CUDA拓展高等级语言 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的 ...
- 利用原生JS实现类似浏览器查找高亮功能(转载)
利用原生JS实现类似浏览器查找高亮功能 在完成 Navify 时,增加一个类似浏览器ctrl+f查找并该高亮的功能,在此进行一点总结: 需求 在.content中有许多.box,需要在.box中找出搜 ...
- windows平台下MongoDB安装和环境搭建
下载安装包或者压缩包 添加db存储和日志存储文件夹 添加服务.配置环境变量.启动Mongo 本例:安装路径:D:Program Files/MongoDB 配置文件的路径:D:MongoDB 一.安装 ...
- https及其背后的加密原理阅读总结
https是以安全为目标的http通道,简单讲是http的安全版.当我们往服务器发送比较隐私的数据(比如说你的银行卡,身份证)时,如果使用http进行通信.那么安全性将得不到保障. 首先数据在传输的过 ...
- CURLOPT_PROGRESSFUNCTION
Author:李强(李祥祥) Date :2012-12-15 17:26 LIBCURL官方网站都没有说清楚道明白进度函数的参数的作用,这点我感觉文档很片面,经总结如下: size_t CUpl ...
- GPT分区表的备份与恢复
GPT分区表的备份与恢复 keenshoes 2016-01-13 21:02:25 关键词: GPT, Partition, MBR,APPLE, GUID, Protective MBR 对于现 ...