hdoj4180
题意:
使(a/b-c/d)最小,然后让你求c/d.
我们能说最小the error |A/B - C/D|
然后C,D的范围是 0 < C < D < B。
其实就是:求接近(A/B)分数的最大分数
思路:
因为是神队友搞得exgcd专题,所以往这方面想想。
我们先把那个减式通分得:(AD-BC)/BD;求这个最小
若A,B有最大公约数不是1,则化简就是答案。
若最大公约数为1,那么(分子)AD-BC=1,即求AX-BY=1或 -AX+BY=1。
那么把X,Y算出来,比较一下分母就好了。后面自己推咯,很简单的。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL __int64
#define mod 9973
#define N 100010
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL ans=exgcd(b,a%b,x,y);
LL temp=x;
x=y;
y=temp-a/b*y;
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL a,b,x,y;
scanf("%I64d/%I64d",&a,&b);
LL d=exgcd(a,b,x,y);
if(d!=1)
{
printf("%I64d/%I64d\n",a/d,b/d);
continue;
}
if(a==1)
{
printf("1/%I64d\n",b-1);
continue;
}
LL d1=(x+b)%b;
LL c1=(-y+a)%a;
LL d2=(-x+b)%b;
LL c2=(y+a)%a;
if(d2>d1)
{
printf("%I64d/%I64d\n",c2,d2);
}
else
{
printf("%I64d/%I64d\n",c1,d1);
}
}
}
hdoj4180的更多相关文章
随机推荐
- NVIDIA---CUDA
http://en.wikipedia.org/wiki/CUDA CUDA From Wikipedia, the free encyclopedia CUDA Developer(s) N ...
- Intel Chipsets
http://en.wikipedia.org/wiki/Chipset Chipset From Wikipedia, the free encyclopedia A chipset is ...
- XMLHTTPRequest DEMO(发送测试)
对于其中的HTTP状态,我们知道200-299表明访问成功:300-399表明需要客户端 反应来满足请求:400-499和500-599表明客户端和服务器出错:其中常用的如404表示资源没找到,403 ...
- linux中select网络通信
//ser.cpp #include <iostream> #include <fcntl.h> #include <sys/socket.h> #include ...
- Arcgis Engine(ae)接口详解(2):featureClass查询
//属性查询~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //IQueryFilter代表查询条件,QueryFilterClass代表只限于属性查询(就是没有空间查询) ...
- js之substr和substring的差别
今天有人在群里问这两个的差别,借这个机会在这罗列下 substring(from,to) 開始和结束的位置,从零開始的索引 參数 描写叙述 from 必需. 一个非负的整数,规定要提取 ...
- LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- 微信小程序存放视频文件到阿里云用到算法js脚本文件
peterhuang007/weixinFileToaliyun: 微信小程序存放视频文件到阿里云用到算法js脚本文件 https://github.com/peterhuang007/ ...
- HTML5 and Websocket
在HTML5规范中,我最喜欢的Web技术就是正迅速变得流行的WebSocket API.WebSocket提供了一个受欢迎的技术,以替代我们过去几年一直在用的Ajax技术.这个新的API提供了一个方法 ...
- JSP JDBC 读取SQL Server 数据2
<%-- Created by IntelliJ IDEA. User: hellohongfu Date: 2017/12/21 Time: 0:16 To change this templ ...