ZOJ 3593 One Person Game

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


#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==0)
{
x=1;
y=0;
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<0)
{
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<0) c=-c;
if(c%g!=0)
{
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-3;
cur=get(x+b*K,y-a*K);
ans=min(ans,cur);

K=K-1;
cur=get(x+b*K,y-a*K);
ans=min(ans,cur);

printf("%lld\n",ans);

}
return 0;
}

ZOJ 3593 One Person Game,布布扣,bubuko.com

时间: 2024-08-26 05:31:53

ZOJ 3593 One Person Game的相关文章

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

思路:题意转化为求 (ax+by=dis) || (ax+cy=dis) || (bx+cy=dis) 三个式子有解时的最小|x| + |y|.显然求解特解x,y直接用扩展欧几里得,那么怎么求|x| + |y|?xy关系为一条直线,那么|x| + |y|应该是在x取0或者y取0的时候,但是要整数,所以只能在周围取几个点.我们知道x=x1+b/gcd*t,那么x1+b/gcd*t = 0可以解得 t  = -x1 * gcd / b.然后在附近取几个点. 代码: #include<iostream

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

One Person Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3593 Appoint description:  System Crawler  (2015-04-29) Description There is an interesting and simple one person game. Suppose ther

[数论]ZOJ3593 One Person Game

题意:一个人要从A走到B  只能走a布.b步.(a+b)步,可以往左或右走   问 最少要走几步,不能走到的输出-1 可以列出方程 ax+by=A-B 或者  ax+(a+b)y=A-B 或者 bx+(a+b)y=A-B 要取这三个方程的最小的(x+y) 根据ax+by=gcd(a, b) 当A-B不是gcd的倍数时 就不能走到 利用ex_gcd可以分别求出这三个方程的解,但求出的这组并非最小的 因此利用枚举斜率 得到的交点为最小的一组解 1 LL exgcd(LL a,LL b,LL &x,L

扩展欧几里德算法详解

扩展欧几里德算法 谁是欧几里德?自己百度去 先介绍什么叫做欧几里德算法 有两个数 a b,现在,我们要求 a b 的最大公约数,怎么求?枚举他们的因子?不现实,当 a b 很大的时候,枚举显得那么的na?ve ,那怎么做? 欧几里德有个十分又用的定理: gcd(a, b) = gcd(b , a%b) ,这样,我们就可以在几乎是 log 的时间复杂度里求解出来 a 和 b 的最大公约数了,这就是欧几里德算法,用 C++ 语言描述如下: 由于是用递归写的,所以看起来很简洁,也很好记忆.那么什么是扩

[数论]拓展欧几里得算法

欧几里得算法(辗转相除法) 用来求解最大公约数 1 int gcd(int a,int b){ 2 return b ? gcd(b,a%b) : a; 3 } 在 #include<algorithm> 中也可以直接调用 __gcd(a,b) 拓展欧几里得算法 求解不定方程: 引理:存在 x , y 使得 ax+by=gcd(a,b) 设a,b,c为任意整数,若方程ax+by=c的一组解是(x0,y0),则它的任意整数解都可以写成(x0+k*b/gcd(a,b),y0-k*a/gcd(a,b

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy