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 ...
随机推荐
- tomcat 启动显示指定的服务未安装
解决方法: 命令行进入tomcat的bin目录 输入“service.bat install” 重启,OK
- JS encodeURIComponent函数
为了避免歧义,可以用JS 的encodeURIComponent函数 将有歧义的字符(?+=等)转换成对应的ASCII编码 for(var i=0;i<whichform.elements.l ...
- 全面了解linux情况常用命令
查看linux服务器CPU详细情况1. 显示CPU个数命令 # cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc ...
- 解决Postgresql服务启动又关闭的问题
查看日志发现如下错误消息:%t LOG: could not receive data from client: An operation was attempted on something tha ...
- js中随机数获取
// 结果为0-1间的一个随机数(包括0,不包括1) var randomNum1 = Math.random(); //console.log(randomNum1); // 函数结果为入参的整数部 ...
- C#装箱与拆箱的研究
在对这个问题展开讨论之前,我们不妨先来问这么几个问题,以系统的了解我们今天要探究的主题. 观者也许曾无数次的使用过诸如System.Console类或.NET类库中那些品种繁多的类.那么,我想问的是它 ...
- oc字典
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- 第十三篇、OC_UICollectionView的基本配置
- (UICollectionView *) categoryCollectionView { if (! _categoryCollectionView) { // 创建布局 UICollectio ...
- Vue之Vue-touch的使用
最近项目中,有的页面发现设置返回键看起来怪怪的,感觉与整体不协调,于是就考虑使用手势滑动事件来实现返回功能~ 开叉查阅资料~找到了vue-touch,使用起来可谓是简单粗暴啊,适合我这样的快速开发人员 ...
- js字符串去掉所有空格
字符串去掉所有空格 "abc 123 def".replace(/\s/g, "") 字符串去掉左右两端空格 " abc 123 def " ...