RealPhobia

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

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
 

    | A/B - C/D |= minn   <=>  | AD - BC| / BD =minn

    如果AB可以约分的话直接约分就是答案。否则说明 gcd(A,B)=1, 我们有 A*D+B*C = gcd(A,B) = 1,原分子加了绝对值,有两种情况

D>0,C<0 或者是 D<0,C>0  ,解完之后对D分正负讨论一下那个使得分母更大就选那个,分子已经是1了。

  因为D<B,所以记得%B,正负分别对应唯一的一个解。

  

 #include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define inf 0x3f3f3f3f
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){d=a;x=;y=;}
else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}
}
int main(){
LL a,b,d,x,y;
int t;
cin>>t;
while(t--){
scanf("%lld/%lld",&a,&b);
exgcd(a,b,d,x,y);
if(d!=){
printf("%lld/%lld\n",a/d,b/d);
}
else{
LL d1,d2,c1,c2;
d1=(x%b+b)%b,c1=-(-a*d1)/b;
d2=-(x%b-b)%b,c2=(+a*d2)/b;
if(d1>d2){
printf("%lld/%lld\n",c1,d1);
}
else{
printf("%lld/%lld\n",c2,d2);
}
}
}
return ;
}

hdu-4180-exgcd的更多相关文章

  1. HDU 1211 EXGCD

    EXGCD的模板水题 RSA算法给你两个大素数p,q定义n=pq,F(n)=(p-1)(q-1) 找一个数e 使得(e⊥F(n)) 实际题目会给你e,p,q计算d,$de \mod F(n) = 1$ ...

  2. hdu 4180

    题意; 求接近规定 分数 的 最大分数用到 farey 数列的第二条性质 1 #include <iostream> #include<stdio.h> using names ...

  3. HDU 5377 (Exgcd + 原根)

    转载自:大牛 知道一个定理了 a ^ x = y (mod p) ===>>   logd(a) * x = logd(y) (mod O(p) )      d 为 p 的 原根,  O ...

  4. HDU 4180 扩展欧几里得

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

  5. HDU 2239 polya计数 欧拉函数

    这题模数是9937还不是素数,求逆元还得手动求. 项链翻转一样的算一种相当于就是一种类型的置换,那么在n长度内,对于每个i其循环节数为(i,n),但是由于n<=2^32,肯定不能直接枚举,所有考 ...

  6. A/B HDU - 1576 (exgcd)

    要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input数据的第一行是一个T,表示有T组数据. 每组数据有两 ...

  7. HDU 5446——Unknown Treasure——————【CRT+lucas+exgcd+快速乘+递推求逆元】

    Each test case starts with three integers n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where k is the number o ...

  8. 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...

  9. HDU 5768:Lucky7(中国剩余定理 + 容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description   When ?? was born, seven ...

  10. HDU 2669 第六周 I题

    Description The Sky is Sprite.  The Birds is Fly in the Sky.  The Wind is Wonderful.  Blew Throw the ...

随机推荐

  1. (转)干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码)

    干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码) 该博客来源自:https://mp.weixin.qq.com/s?__biz=MzA4NzE1NzYyMw==& ...

  2. K8S 安装笔记

    1. 准备CentOS7环境 #关闭防火墙 # systemctl disable firewalld # systemctl stop firewalld #安装etcd, kubernetes(会 ...

  3. using Redis in .net core

    Using Redis Cache in .net Core Distributed Cache using Redis and ASP.NET Core ASP.NET Core Data Prot ...

  4. VirtuablBox 出错: VERR_SUPLIB_OWNER_NOT_ROOT 解决方法

    刚刚把 VirtualBox 升级, 从 3.2 到 4.0.4 后,虚拟机上的系统无法运行, 提示: VERR_SUPLIB_OWNER_NOT_ROOT 查了一下,发现是因为 /opt 的 own ...

  5. 项目Alpha冲刺——代码规范、冲刺任务与计划

    作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及可视化分析平台 这个作业的 ...

  6. JSON数据展示神器:react-json-view(常用于后台网站)

    一.react-json-view - npm 官方定义: RJV is a React component for displaying and editing javascript arrays ...

  7. hdu 3832 Earth Hour bfs

    Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Prob ...

  8. Mybatis中sql语句中的in查询,判断null和size为0的情况

    不严谨的写法,可能会报错:in (),这种情况不符合SQL的语法,导致程序报错. 如果简单只做非空判断,这样也有可能会有问题:本来in一个空列表,应该是没有数据才对,却变成了获取全部数据! 所以一个比 ...

  9. android 利用CountDownTimer实现时分秒倒计时效果

    https://blog.csdn.net/mrzhao_perfectcode/article/details/81289578

  10. System.arraycopy和arrays.copyOf

    public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 这 ...