智障了,智障了,水一水博客。

本来是个水题,但是for循环遍历那里写挫了。。。

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

一开始没看懂题,后来懂了,c=a+b,所以就有3个方程,分别是a*x+b*y=r,a*x+c*y=r,b*x+c*y=r;

扩展欧几里得求出来gcd之后,通过判断r是不是gcd的倍数得到方程有没有解,然后 go on。。。

因为扩展欧几里得求出来的x和y并不是最优解,所以通过x和y的通式,x=x0+b/gcd*k;y=y0-a/gcd*k;(k为倍数)

本来自己想的是最优解和求出来的一般解应该差不了多少,就直接从-100到100遍历的,结果发现一直wa,学长说要找方程所在直线与源点距离最近的才是结果。

就是确定一下for的范围,不是瞎想的-100到100就以为能水过去。。。

首先x=x0+b/gcd*k,求出来k的范围,就是[-x0*gcd/b-1,-x0*gcd/b+1],在这个范围内for一遍,然后再通过y的通解,求出来的k的范围再for一遍就得到最优解了。

我智障,算y的k的时候,应该/a,我写成/b了_(:з」∠)_

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int inf=0x3f3f3f3f;
const int eps=1e-; ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==){
x=;y=;
return a;
}
ll r=exgcd(b,a%b,x,y);
ll t=y;
y=x-(a/b)*y;
x=t;
return r;
} ll fun(ll a,ll b,ll r){
ll x,y;
ll gcd=exgcd(a,b,x,y);
if(r%gcd!=)return -;
else{
x*=r/gcd;
y*=r/gcd;
ll k1=-1.0*x*gcd/b-,k2=-1.0*x*gcd/b+;
ll kk1=1.0*y*gcd/a-,kk2=1.0*y*gcd/a+;//y是除以a,智障了,写成除以b了。
ll ans=fabs(x)+fabs(y);
for(ll i=k1;i<=k2;i++){
if(fabs(x+i*b/gcd)+fabs(y-i*a/gcd)<ans)
ans=fabs(x+i*b/gcd)+fabs(y-i*a/gcd); }
for(ll i=kk1;i<=kk2;i++){
if(fabs(x+i*b/gcd)+fabs(y-i*a/gcd)<ans)
ans=fabs(x+i*b/gcd)+fabs(y-i*a/gcd); }
return ans; }
} int main(){
int t;
cin>>t;
while(t--){
ll A,B,a,b,c;
cin>>A>>B>>a>>b;
c=a+b;
ll r=fabs(A-B);
ll h1=fun(a,b,r);
ll h2=fun(a,c,r);
ll h3=fun(b,c,r);
if(h1==-&&h2==-&&h3==-)cout<<"-1"<<endl;
else{
ll ans=min(h1,h2);
ans=min(ans,h3);
cout<<ans<<endl;
}
}
}

水一水,溜了,慢慢补。去看主席树了。。。

ZOJ 3593.One Person Game-扩展欧几里得(exgcd)的更多相关文章

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

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

  2. 同余问题(一)——扩展欧几里得exgcd

    前言 扩展欧几里得算法是一个很好的解决同余问题的算法,非常实用. 欧几里得算法 简介 欧几里得算法,又称辗转相除法. 主要用途 求最大公因数\(gcd\). 公式 \(gcd(a,b)=gcd(b,a ...

  3. 浅谈扩展欧几里得[exgcd] By cellur925

    关于扩展欧几里得从寒假时就很迷,抄题解过了同余方程,但是原理并不理解. 今天终于把坑填上了qwq. 由于本人太菜,不会用markdown,所以这篇总结是手写的(什么).(字丑不要嫌弃嘛) ****** ...

  4. 扩展欧几里得(exgcd)与同余详解

    exgcd入门以及同余基础 gcd,欧几里得的智慧结晶,信息竞赛的重要算法,数论的...(编不下去了 讲exgcd之前,我们先普及一下同余的性质: 若,那么 若,,且p1,p2互质, 有了这三个式子, ...

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

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

  6. ZOJ 3609 Modular Inverse(扩展欧几里得)题解

    题意:求乘法逆元最小正正数解 思路:a*x≡1(mod m),则称x 是 a 关于 m 的乘法逆元,可以通过解a*x + m*y = 1解得x.那么通过EXGcd得到特解x1,最小正解x1 = x1 ...

  7. 扩展欧几里得(exgcd)-求解不定方程/求逆元

    贝祖定理:即如果a.b是整数,那么一定存在整数x.y使得ax+by=gcd(a,b).换句话说,如果ax+by=m有解,那么m一定是gcd(a,b)的若干倍.(可以来判断一个这样的式子有没有解)有一个 ...

  8. 扩展欧几里得 exGCD

    Elementary Number Theory - Extended Euclid Algorithm Time Limit : 1 sec, Memory Limit : 65536 KB Jap ...

  9. 数论--扩展欧几里得exgcd

    算法思想 我们想求得一组\(x,y\)使得 \(ax+by = \gcd(a,b)\) 根据 \(\gcd(a,b) = \gcd(b,a\bmod b)\) 如果我们现在有\(x',y'\) 使得 ...

随机推荐

  1. 爬山算法 | Java版HA_TSP

    嗯哼,今天记录下采用Java编写的爬山算法(Hill Algorithm)求解TSP问题. 爬山算法与其他智能算法类似,是一种用来求解多峰函数最值的算法,爬山算法的基本思想是新解不劣于当前解则转移,否 ...

  2. [转载]C#中使用ADO.NET连接SQL Server数据库,自动增长字段用作主键,处理事务时的基本方法

    问题描述: 假设在数据库中存在以下两张数据表: User表,存放用户的基本信息,基本结构如下所示:   类型 说明 ID_User int 自动增长字段,用作该表的主键 UserName varcha ...

  3. Appium环境搭建及“fn must be a function”问题解决

    由于appium在线安装比较困难,大多数应该是由于FQ造成的吧,索性直接下载appium安装包:http://pan.baidu.com/s/1bpfrvjD nodejs下载也很缓慢,现提供node ...

  4. complex类的定义、实现

    复数类complex的定义.实现(求模.复数加法) #include <iostream> #include <cmath> using namespace std; clas ...

  5. Windows网络编程笔记6 --- WinSock I/O 控制方法

    Windows提供了两种方式“套接字模式”和“套接字I/O模型”,可对一个套接字上的I/O行为加以控制.套接字模式用于决定在随一个套接字调用时,那些 Winsock函数的行为.其中的模型包括括sele ...

  6. 接口测试之post和get的区别

    post和get都可以给服务器发送请求,在做接口测试的时候,我发现有些时候某些功能的接口文档中是用post请求发送的, 但是只要接口一致参数一致用post也能发送请求,并且获取到的返回也是正确的. 那 ...

  7. CentOS7安装Code::Blocks

    在CentOS7上安装Codelocks的过程. 1.安装gcc,需要c和c++两部分,默认安装下,CentOS不安装编译器的,在终端输入以下命令即可yum install gccyum instal ...

  8. 深入学习之mysql(三)单表操作

    1.创建表的结构和数据 CREATE TABLE `t_student`( `id` INT PRIMARY KEY, `stuName` VARCHAR(10) NOT NULL, `age` IN ...

  9. (转载)CentOS 6.5使用aliyun镜像来源

    (原地址:http://www.linuxidc.com/Linux/2014-09/106675.htm) 当我们把CentOS 6.5安装好以后,可以使用这个脚本来使用国内的阿里云镜像源 #!/b ...

  10. 纸上得来终觉浅,绝知此事要躬行——Spring boot任务调度

    前言:之前今日开讲项目的时候,用到了Quartz进行任务调度.后来做一个电商项目的时候,还用到了Quartz任务调度. 觉得挺简单的,a peace of cake.  忽略了总结,当时闭着眼睛都能捉 ...