One Person Game


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 integersABa and b, separated by spaces. (-231 ≤ AB < 231, 0 < ab < 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点,一次可以走x 步,或者 y步,或者x+y步。共有6种走法,因为前进和后退都是可以的。
现在告诉你A,B位置,x ,y值。求最小的步数。
思路:首先转化为求方程 ax+by=c的形式,题意将转化为max(x,y);这样似乎的正确的,但是不够完整。
因为这个是在x,y都是同向的前提,如果x,y不方向走,那么就应该是abc(x)+abc(y);
根据扩展欧几里得可以求出x' y'的取值范围。都是一条直线,而且它们的斜率肯定是一正一负,当它们有交叉点的时候,就是最优的。但是可能是小数。
在这个点的前后几个点进行讨论,找出最值就可以了。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<math.h>
using namespace std;
typedef long long LL; LL Ex_GCD(LL a,LL b,LL &x,LL &y)
{
if(b==)
{
x=;
y=;
return a;
}
LL g=Ex_GCD(b,a%b,x,y);
LL hxl=x-(a/b)*y;
x=y;
y=hxl;
return g;
}
LL get(LL a,LL b)
{
if(a*b<)
{
return abs(a)+abs(b);
}
else return abs(a)>abs(b)? abs(a):abs(b);
}
int main()
{
int T;
LL A,B,a,b,g,x,y,ans,cur,c;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld%lld",&A,&B,&a,&b);
g=Ex_GCD(a,b,x,y);
c=A-B;
if(c<) c=-c;
if(c%g!=)
{
printf("-1\n");
continue;
}
a=a/g;
b=b/g;
x=x*(c/g);
y=y*(c/g); double t = (y-x)*1.0/(a+b);
LL K = (LL)t; cur = get(x+b*K,y-a*K);
ans=cur; K++;
cur=get(x+b*K,y-a*K);
ans=min(ans,cur); K++;
cur=get(x+b*K,y-a*K);
ans=min(ans,cur); K=K-;
cur=get(x+b*K,y-a*K);
ans=min(ans,cur); K=K-;
cur=get(x+b*K,y-a*K);
ans=min(ans,cur); printf("%lld\n",ans); }
return ;
}

ZOJ 3593 One Person Game的更多相关文章

  1. ZOJ 3593 One Person Game(ExGcd + 最优解)题解

    思路:题意转化为求 (ax+by=dis) || (ax+cy=dis) || (bx+cy=dis) 三个式子有解时的最小|x| + |y|.显然求解特解x,y直接用扩展欧几里得,那么怎么求|x| ...

  2. ZOJ 3593 One Person Game(拓展欧几里得求最小步数)

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...

  3. ZOJ - 3593 One Person Game (扩展欧几里得)

    题意:一个人在坐标A,要前往坐标B的位置.可以往左或往右走a,b,a+b个单位,求到达B的最小步数. 分析:扩展欧几里得算法求解线性方程的套路不变.令C=fabs(A-B),c = a+b, 扩展gc ...

  4. ZOJ 3593.One Person Game-扩展欧几里得(exgcd)

    智障了,智障了,水一水博客. 本来是个水题,但是for循环遍历那里写挫了... One Person Game Time Limit: 2 Seconds      Memory Limit: 655 ...

  5. [数论]ZOJ3593 One Person Game

    题意:一个人要从A走到B  只能走a布.b步.(a+b)步,可以往左或右走   问 最少要走几步,不能走到的输出-1 可以列出方程 $ax+by=A-B$ 或者 $ax+(a+b)y=A-B$ 或者 ...

  6. One Person Game(zoj3593+扩展欧几里德)

    One Person Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status ...

  7. ZOJ Problem Set - 3593 拓展欧几里得 数学

    ZOJ Problem Set - 3593 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593 One Person ...

  8. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  9. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

随机推荐

  1. [原创]java WEB学习笔记56:Struts2学习之路---Struts 版本的 登录 demo

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. 访问网页时提示的503错误信息在IIS中怎么设置

    访问网页时提示的503错误信息在IIS中怎么设置 503是一种常见的HTTP状态码,出现此提示信息的原因是由于临时的服务器维护或者过载,服务器当前无法处理请求则导致了访问网页时出现了503错误.那么当 ...

  3. BJFU 1009

    描述 现在社会上的抽奖活动简直是太多了.前段时间中国联通就举办了一个很无聊的抽奖活动,规则是每人可以向中国联通的短信系统发送一个实数,系统每天会从这些数字中选择一个无重复(就是有且只有一个)且最小的数 ...

  4. oracle索引使用时注意

    1.使用不等于操作符(<>, !=)下面这种情况,即使在列dept_id有一个索引,查询语句仍然执行一次全表扫描 select * from dept where staff_num &l ...

  5. 夺命雷公狗ThinkPHP项目之----企业网站7之栏目的修改(主要用模型来验证字段)

    我们照老,在控制器里面先查出我们所需要用到的数据: 然后直接遍历到模版上即可: 然后再开始写提交过来的数据处理问题(注意一定要接收修改页面通过隐藏域)而且我们刚才已经写好我们的model层了,所以直接 ...

  6. 夺命雷公狗---微信开发53----网页授权(oauth2.0)获取用户基本信息接口(3)实现世界留言版

    前面两节课我们讲的是base型的授权了,那么现在我们开始Userinfo型授权, 先来看下我们的原理图 我们这节课来做一个 世界留言版 系统 1..首先我还是在微信测试平台那里设置好回调页面的域名 2 ...

  7. 夺命雷公狗---微信开发17----自定义菜单的事件推送,响应菜单的CLICK

    废话不多说,index.php 代码如下所示: <?php /** * wechat php test */ //define your token require_once "com ...

  8. 使用java访问 动态链接库(dll)

    在这个时候,我们可以使用的java技术有jni.jna.jnative,这个大部分都可以完成任务.但是有时候我们在实际情况中拿到的dll有变化,当我们需要用的函数是在dll中的类里面的话,我们再使用前 ...

  9. 《zw版·Halcon-delphi系列原创教程》cgal与opencv,Halcon

    <zw版·Halcon-delphi系列原创教程>cgal与opencv,Halcon opencv作为少有的专业开源图像软件,虽然功能,特别是几何计算方面,不如Halcon,不过因为开源 ...

  10. location.reload

    location.reload()-----重新加载缓存页面 location.reload(true)------重新加载服务器页面