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 ;
}

最新文章

  1. 通过PowerShell启用AADC的密码同步功能
  2. php反射
  3. MySQL建表规范与常见问题
  4. C#List的排序和简单去重总结
  5. 20161004 NOIP 模拟赛 T1 解题报告
  6. BZOJ3246 [Ioi2013]Dreaming
  7. MySQL 全文搜索支持
  8. POJ 2541 Binary Witch(逆序KMP,好题)
  9. hadoop集群默认配置和常用配置【转】
  10. webBrowser1_DocumentCompleted不停被调用
  11. libipq —— iptables用户空间数据包排队库
  12. ember.js
  13. [Unity3D]脚本中Start()和Awake()的差别
  14. SVN与eclipse整合和利用、SVN与Apache综合
  15. hadoop系列二:HDFS文件系统的命令及JAVA客户端API
  16. MongoDB基础教程系列--第四篇 MongoDB 查询文档
  17. 老李分享:adb发送的指令都有哪些
  18. RHEL6 安装KVM
  19. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal
  20. Android Studio 使用Menu

热门文章

  1. (转载)Sublime Text 3 快捷键大全
  2. 使用v-for循环写入html内容,每一项的数据的写入
  3. Python 一个抓取糗百的段子的小程序
  4. 将实体类、匿名对象转换为SqlParameter列表
  5. gym 101164 H.Pub crawl 凸包
  6. 2. maven的配置和使用
  7. 【C#】侦听文件系统更改通知 FileSystemWatcher 类
  8. JS中UTF-8和UTF-16互转
  9. java 里面耦合和解耦
  10. DirectX学习之第一个可运行的工程