hdu-4180-exgcd
RealPhobia
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 938 Accepted Submission(s): 435
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.
1. B is a 32 bit integer strictly greater than 2, and
2. 0 < A < B
1/4
2/3
13/21
1/2
8/13
| 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的更多相关文章
- 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$ ...
- hdu 4180
题意; 求接近规定 分数 的 最大分数用到 farey 数列的第二条性质 1 #include <iostream> #include<stdio.h> using names ...
- HDU 5377 (Exgcd + 原根)
转载自:大牛 知道一个定理了 a ^ x = y (mod p) ===>> logd(a) * x = logd(y) (mod O(p) ) d 为 p 的 原根, O ...
- HDU 4180 扩展欧几里得
RealPhobia Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2239 polya计数 欧拉函数
这题模数是9937还不是素数,求逆元还得手动求. 项链翻转一样的算一种相当于就是一种类型的置换,那么在n长度内,对于每个i其循环节数为(i,n),但是由于n<=2^32,肯定不能直接枚举,所有考 ...
- A/B HDU - 1576 (exgcd)
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input数据的第一行是一个T,表示有T组数据. 每组数据有两 ...
- 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 ...
- 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...
- HDU 5768:Lucky7(中国剩余定理 + 容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description When ?? was born, seven ...
- HDU 2669 第六周 I题
Description The Sky is Sprite. The Birds is Fly in the Sky. The Wind is Wonderful. Blew Throw the ...
随机推荐
- 微服务架构与实践4_Docker
构建Docker映像 定义Dockerfile=>Docker根据Dockerfile构建出映像 包含: 基础映像(父映像)信息 维护者信息 映像操作命令 容器启动命令 .net standar ...
- Termux 一款安卓终端模拟器
Termux官网 https://termux.com/ 用处 提供了一个类似于Linux终端的界面,可以使用apt安装程序.目前我在上面跑了我以前写的一些爬虫脚本,运行完全没有问题. 玩法 玩法还是 ...
- BZOJ 1055: [HAOI2008]玩具取名(记忆化搜索)
http://www.lydsy.com/JudgeOnline/problem.php?id=1055 题意: 思路:记忆化搜索. #include<iostream> #include ...
- RN返回navigation方法
RN官方指定的路由管理是navigation 通过打印我们可以得到navgation的相关属性 1:dispatch ,Redux的事件发起 2:goback()返回 3:navigate(rout ...
- Hash——字符串匹配(求s1在s2中出现的次数)
题目描述: 这是一道模板题. 给定一个字符串 A 和一个字符串 B ,求 B 在 A 中的出现次数.A 和 B中的字符均为英语大写字母. 求A 在 B 中出现了几次.(可重叠) 样例输入: 3 BA ...
- Mysql一些常用语句
1.查询表创建的时间: SELECT CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERETABLE_NAME='TableName'
- 将矩阵数据转换为栅格图 filled.contour()
require(grDevices) # for colours filled.contour(volcano, color = terrain.colors, asp = 1) # simple x ...
- DRF框架固定配置
1.认证Authentication 可以在配置文件中配置全局默认的认证方案 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_ ...
- redux与redux-react使用示例
redux使用 <script type="text/babel"> var Counter=React.createClass({ incrementIfOdd:fu ...
- 2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest
2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest A Secret of Chocolate Poles 思路:暴力枚举黑巧克力的个数和厚黑巧克力的个 ...