zoj3593One Person Game (扩展欧几里德)
There is an interesting and simple one person game. Suppose there is a number axis under your feet. You are at point A at first and your aim is point B. There are 6
kinds of operations you can perform in one step. That is to go left or right by a,b and c, here c always equals to a+b.
You must arrive B as soon as possible. Please calculate the minimum number of steps.
Input
There are multiple test cases. The first line of input is an integer T(0 < T ≤ 1000) indicates the number of test cases. Then T test cases follow. Each test
case is represented by a line containing four integers 4 integers A, B, a and b, separated by spaces. (-231 ≤ A, B < 231, 0 < a, b < 231)
Output
For each test case, output the minimum number of steps. If it's impossible to reach point B, output "-1" instead.
Sample Input
2
0 1 1 2
0 1 2 4
Sample Output
1
-1
题意:有两个端点A,B,每次你可以向左或者向右走a,b,a+b的距离,问最少走多少次能从A走到B。
思路:设A,B之间的距离为dis,a+b=c,那么题目等价于min{|x|+|y| | (ax+by=dis) || (ax+cy=dis) || (bx+cy=dis) }.注:扩展欧几里德算出来ax+by=gcd(c,d)中的特殊值x,y一定满足|x|+|y|最小,但是如果算的是ax+by=d,(d%gcd(a,b),但是d!=gcd(a,b))那么就不一定|x|+|y|最小,此时要使得x趋近于0,或者使得y趋近于0,这样算出来的k带入然后取几者中的最小值,因为x,y的关系式是一条直线,|x|+|y|=m在直线接近坐标轴的情况下取到最小值。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 10000000000000LL
#define pi acos(-1.0)
#define MOD 1000000007
#define maxn 1000005
ll extend_gcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1;y=0;return a;
}
ll d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
ll niyuan(ll a,ll n){
ll x,y;
ll d=extend_gcd(a,n,x,y);
if(d==1) return (x%n+n)%n;
else return -1;
}
ll gcd(ll a,ll b){
return (b>0)?gcd(b,a%b):a;
}
ll solve(ll a,ll b,ll dis)
{
ll x,y,d,x0,y0,ans;
d=extend_gcd(a,b,x,y);
if(dis%d!=0)return -1;
x0=x*dis/d;
y0=y*dis/d;
ll aa,bb;
aa=a/d;
bb=b/d;
ll k;
k=-x0/bb-1;
ans=abs(x0+bb*k)+abs(y0-aa*k);
k++;
ans=min(ans,abs(x0+bb*k)+abs(y0-aa*k) );
k++;
ans=min(ans,abs(x0+bb*k)+abs(y0-aa*k) );
k=y0/aa-1;
ans=min(ans,abs(x0+bb*k)+abs(y0-aa*k) );
k++;
ans=min(ans,abs(x0+bb*k)+abs(y0-aa*k) );
k++;
ans=min(ans,abs(x0+bb*k)+abs(y0-aa*k) );
return ans;
}
int main()
{
int n,m,i,j,T;
ll x1,x2,a,b,c,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld%lld",&x1,&x2,&a,&b);
ll dis=abs(x2-x1);
c=a+b;
ll ans=solve(a,b,dis);
ans=min(ans,solve(a,c,dis));
ans=min(ans,solve(b,c,dis));
printf("%lld\n",ans);
}
return 0;
}
zoj3593One Person Game (扩展欧几里德)的更多相关文章
- (扩展欧几里德算法)zzuoj 10402: C.机器人
10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...
- [BZOJ1407][NOI2002]Savage(扩展欧几里德)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...
- 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm
欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...
- 51nod 1352 扩展欧几里德
给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数. 提示: 对于第二组测试数据,集合分别 ...
- CF 7C. Line(扩展欧几里德)
题目链接 AC了.经典问题,a*x+b*y+c = 0整数点,有些忘记了扩展欧几里德,复习一下. #include <cstdio> #include <iostream> # ...
- poj2142-The Balance(扩展欧几里德算法)
一,题意: 有两个类型的砝码,质量分别为a,b;现在要求称出质量为d的物品, 要用多少a砝码(x)和多少b砝码(y),使得(x+y)最小.(注意:砝码位置有左右之分). 二,思路: 1,砝码有左右位置 ...
- poj2115-C Looooops(扩展欧几里德算法)
本题和poj1061青蛙问题同属一类,都运用到扩展欧几里德算法,可以参考poj1061,解题思路步骤基本都一样.一,题意: 对于for(i=A ; i!=B ;i+=C)循环语句,问在k位存储系统中循 ...
- poj1061-青蛙的约会(扩展欧几里德算法)
一,题意: 两个青蛙在赤道上跳跃,走环路.起始位置分别为x,y. 每次跳跃距离分别为m,n.赤道长度为L.两青蛙跳跃方向与次数相同的情况下, 问两青蛙是否有方法跳跃到同一点.输出最少跳跃次数.二,思路 ...
- HDU 1576 A/B【扩展欧几里德】
设A/B=x,则A=Bx n=A%9973=A-9973*y=Bx-9973*y 用扩展欧几里德求解 #include<stdio.h> #include<string.h> ...
随机推荐
- readhat6.5下安装weblogic10.3.6
转载自:http://www.mianhuage.com/752.html 1.安装前准备 1.1.准备安装包generic.jar1.2.创建weblogic用户及用户组创建组命令:groupadd ...
- linux之平均负载(学习笔记非原创)
什么是平均负载 [root@111 ~]# uptime 11:03:33 up 149 days, 17:34, 1 user, load average: 0.08, 0.05, 0.01 最后三 ...
- zabbix_agent items not supported状态
不记得自己究竟更改了什么东西,然后突然发现所有的有关mysql的监控items都变成了not supported,怎么做不行,最后在web主页把主机删除,又重新添加一下,重新添加了一下模版就好了.这究 ...
- Docker安装配置及华为云镜像加速
Docker华为云镜像加速 软件介绍 支持的操作系统 docker安装 docker镜像加速(华为云实现) 1.登录华为云网站,注册华为云账户 2.登录华为云账户,点击网页右上角的控制台 3.点击左上 ...
- response返回特性
1. response 返回特性 r=requests.get("http://www.baidu.com")print(r.text) #打印返回正文print(r.status ...
- libuv中实现tcp服务器
目录 1.说明 2.libuv的tcp server 3.API简介 3.1.uv_tcp_init 3.2.uv_ip4_addr 3.3.uv_tcp_bind 3.4.uv_listen 3.5 ...
- Mac下IDEA激活Jrebel
第一步:在idea中下载jrebel,过程省略 第二步:配置反向代理工具 Windows 版:http://blog.lanyus.com/archives/317.html MAC 版: 安装hom ...
- 监听套接字描述字 已连接套接字描述字 和打电话的情形非常不一样的地方 完成了 TCP 三次握手,操作系统内核就为这个客户生成一个已连接套接字
1. accept: 电话铃响起了-- 当客户端的连接请求到达时,服务器端应答成功,连接建立,这个时候操作系统内核需要把这个事件通知到应用程序,并让应用程序感知到这个连接.这个过程,就好比电信运营商完 ...
- Coded UI
Coded UI Test是Visual Studio 2010对于Testing Project(测试工程)提供的关于UI自动化测试的框架,支持Win32,Web,WPF等UI的自动化测试,是一个非 ...
- LOJ10131暗的连锁
题目描述 原题来自:POJ 3417 Dark 是一张无向图,图中有 N 个节点和两类边,一类边被称为主要边,而另一类被称为附加边.Dark 有 N–1 条主要边,并且 Dark 的任意两个节点之间都 ...