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

本来是个水题,但是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. f触发器、存储过程

    drop trigger trig_insert--删除触发器

  2. The DOM in JavaScript

    DOM : Document Object Model   D is for document :  The DOM cant work without a document . When you c ...

  3. P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  4. MySQL之Schema与数据类型优化

    选择优化的数据类型 MySQL支持的数据类型非常多,选择正确的数据类型对于获得高性能至关重要.不管存储哪种类型的数据,下面几个简单的原则都有助于做出更好的选择: 更小的通常更好一般情况下,应该尽量使用 ...

  5. TCP/IP网络编程之多进程服务端(二)

    信号处理 本章接上一章TCP/IP网络编程之多进程服务端(一),在上一章中,我们介绍了进程的创建和销毁,以及如何销毁僵尸进程.前面我们讲过,waitpid是非阻塞等待子进程销毁的函数,但有一个不好的缺 ...

  6. Js中的undefined和not defined

    1.undefined 已经声明,但未赋值 2.not defined 未声明,报错

  7. 使用html进行浏览器判断,浏览器条件注释

    下面来点今天写东西的时候查资料,收集的关于使用html进行浏览器判断的一些资料: 条件注释的基本格式: <!--[if expression]>注释内容<![endif]--> ...

  8. Java并发之(2):线程通信wait/notify(TIJ_21_5)

    简介: java中线程间同步的最基本的方式就是使用wait()&notify()&notifyAll(),它们是线程间的握手机制.除了上述方法,java5还在java.util.con ...

  9. windows phone UI吐槽---跑偏了就再也跑不回来了

    首先wp的ui灵感来自瑞士的平面设计:      先上两张图,嗯,确实不错,简洁明了,强调的是信息本身,而不是冗余的界面元素,传达准确. 在现实生活中这种突出信息的设计语言也不时见到:    可以总结 ...

  10. IOS开发学习笔记043-QQ聊天界面实现

    QQ聊天界面实现 效果如下: 实现过程: 1.首先实现基本界面 头像使用 UIImageView : 文字消息使用 UIButton 标签使用 UILable :水平居中 所有元素在一个cell中,在 ...