ZOJ Problem Set - 3593

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593

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 integers ABa 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

题解:先求ax+by=A-B的解中|x|+|y|最小的一组x,y值,利用拓展欧几里得可求出ax'+by'=gcd(a,b)的x',y'值,仅在A-B是gcd(a,b)的整数倍时,方程有解,令k=(A-B)/gcd(a,b),则可以求出,当两条直线相交时|x|+|y|最小,求出交点对应的t,由于交点可能不是整数,所以将t-1,t,t+1各判断一次,若x,y同号,结果取绝对值大的(因为同向时x+y可以合并为1步),若x,y异号,结果取绝对值之和,通过ans记录所有结果中的最小值。

#include<iostream>
#include<cmath>
#include<cstdio>
#define ll long long
using namespace std;
void exgcd(ll a,ll b,ll &gcd,ll &x,ll &y){
if(!b){
gcd=a;x=1;y=0;
}else{
exgcd(b,a%b,gcd,x,y);
ll tmp=x;
x=y;
y=tmp-a/b*y;
}
}
int main(){
int T;
ll a,b,A,B,x,y,gcd,ans,t;
scanf("%d",&T);
while(T--){
scanf("%lld%lld%lld%lld",&A,&B,&a,&b);
exgcd(a,b,gcd,x,y);
//cout<<x<<" "<<y<<endl;
if((A-B)%gcd)
{printf("-1\n");continue;}
x=(A-B)/gcd*x;
y=(A-B)/gcd*y;
ans=9999999999;
a/=gcd,b/=gcd;
t=(y-x)/(a+b);
//直线x=x+bt与直线y=y-at的距离最近时对应的整数t
for(int i=t-1;i<=t+1;i++)//t不一定为整数,左右各取一次
if(abs(x+i*b)+abs(y-i*a)==abs(x+i*b+y-i*a))//同号
ans=min(ans,max(abs(x+i*b),abs(y-i*a)));
else//异号
ans=min(ans,abs(x+i*b)+abs(y-i*a));
printf("%lld\n",ans);
}
return 0;
}

ZOJ Problem Set - 3593 拓展欧几里得 数学的更多相关文章

  1. ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  2. [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)

    Power of Fibonacci Time Limit: 5 Seconds      Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...

  3. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  4. SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3

    141. Jumping Joe time limit per test: 0.25 sec. memory limit per test: 4096 KB Joe is a frog who lik ...

  5. 【lydsy1407】拓展欧几里得求解不定方程+同余方程

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1407 题意: 有n个野人,野人各自住在第c[i]个山洞中(山洞成环状),每年向前走p[i] ...

  6. NOIP2012拓展欧几里得

    拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...

  7. poj 1061 青蛙的约会 拓展欧几里得模板

    // poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...

  8. bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得

    这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...

  9. POJ1061 青蛙的约会-拓展欧几里得

    Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...

随机推荐

  1. 怎么用MATLAB产生FPGA所需的hamming窗系数

    需求 在FPGA处理中如果需要对待处理数据加窗,则需要窗系数存储在ROM中以供使用. 前言 MATLAB窗函数说明 流程 比如加个hamming窗,8192点,16bit放大,最终系数18bit位宽. ...

  2. 【THUSC2017】【LOJ2981】如果奇迹有颜色 DP BM 打表 线性递推

    题目大意 有一个 \(n\) 个点的环,你要用 \(m\) 中颜色染这 \(n\) 个点. 要求连续 \(m\) 个点的颜色不能是 $1 \sim m $ 的排列. 两种环相同当且仅当这两个环可以在旋 ...

  3. 【CSA35G】【XSY3318】Counting Quests DP 拉格朗日反演 NTT

    题目大意 zjt 是个神仙. 一天,zjt 正在和 yww 玩猜数游戏. zjt 先想一个 \([1,n]\) 之间的整数 \(x\),然后 yww 开始向他问问题. yww 每次给 zjt 一个区间 ...

  4. JavaScript - proxy

    Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等). 来看看常用的方法 handler.get() let o = { name: 'liwenchi', age: 1 ...

  5. [hosts]在hosts中屏蔽一级域名和二级域名的写法

    一级域名,如baidu: 0.0.0.0 baidu.com 二级域名 如有道公开课 0.0.0.0 ke.youdao.com 不带协议名,不带www. 用127.0.0.1也可以.

  6. python: c_char_p指向的bitmap图像数据,通过c_char_Array最终赋值给PIL的Image对象

    def GetCurrentImage(self): ok, bitmap, buff_len = self.GetCurrentFrameBitmap() #调用C函数,返回位图数据的指针. bit ...

  7. cocos2d中个类之间的关系

    1.Director类: (1)单例类Director::getInstance()  ,获取导演类对象 (2)设置游戏配置(OpenGL),推动游戏发展 runWithSence.replaceSe ...

  8. static_assert与assert

    C++0x中引入了static_assert这个关键字,用来做编译期间的断言,因此叫做静态断言. 其语法:static_assert(常量表达式,提示字符串). 如果第一个参数常量表达式的值为fals ...

  9. C语言的第一堂课

    感觉茂哥讲了很多,但是有些输入的语句还是没能记住 刚讲的都是概念,看来需要看一下表格,以及C语言的基础 有些意思还不能够理解 略显尴尬 回去把C语言书的各种概念理解一下……

  10. SQL两个事务update同一张表出现的死锁问题(waitfor delay)

    抄录网址:https://blog.csdn.net/qiumuxia0921/article/details/50574879 下面是我们的建表语句: SET ANSI_NULLS ON GO SE ...