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. HBuilderX——编译失败:HBuilderX 安装目录不能包括 ( 等特殊字符

    前言 编译小程序的时候报错,原因其实很简单,安装目录的问题! 解决 我安装到了D:\Program Files (x86),放到D盘的根目录下或者D:\Program Files只要不包含一些特殊字符 ...

  2. Custom DNS on Ubuntu 18.04LTS server

    1. Edit resolved config file nano /etc/systemd/resolved.conf 2. Replace #DNS into DNS DNS=9.9.9.9 1. ...

  3. github+jekyll个人博客搭建

    Thanks to https://blog.csdn.net/Hanghang_/article/details/78944672 跟着这个博客一步步搭建,从jekyll官网上找到自己喜欢的主题. ...

  4. LinkedBlockingQueue阻塞队列详解

    主要api java.util.concurrent包下的新类.LinkedBlockingQueue就是其中之一,是一个阻塞的线程安全的队列,底层采用链表实现.        LinkedBlock ...

  5. redis---------AOF文件异常导致的redis无法载入

    AOF损坏时的对策1.若在写AOF文件时Server崩溃则可能导致AOF文件损坏而不能被Redis载入.可通过如下步骤修复: 创建一个AOF文件的备份: cp appendonly.aof appen ...

  6. SimpleDateFormat 线程不安全及解决方案

    SimpleDateFormat定义 SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本).解析(文本 -> 日期 ...

  7. Java(18) 集合框架

    一.集合框架 Collectoin                   Map List           set              HashMap ArrayList LinkedList ...

  8. rsync + sersync 实现实时数据同步

    一.环境: 主机1:youzhuan-bak     centos 7.2 主机2:youzhuan-online   centos 7.2 二.部署过程: youzhuan-bak : [root@ ...

  9. Studio 5000 指针(间接寻址)编程

    ​前言:自动化控制系统是综合性.复杂性的,处于现场层的PLC控制器虽然进行各种控制,但最终还是对数据进行处理,对数据的处理,包含两种方式,一种是直接使用,第二种就是间接使用.针对第二种处理方式,就要用 ...

  10. js reduce()方法使用

    reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. 注意: reduce() 对 ...