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

本来是个水题,但是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;
}
}
}

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

最新文章

  1. C#多线程之线程池篇2
  2. favicon.ico文件简介
  3. linq多表join与group
  4. 用JQuery给图片添加鼠标移入移出事件
  5. paper 30 :libsvm的参数说明
  6. python datetime
  7. 从零开始学ios开发(十五):Navigation Controllers and Table Views(中)
  8. POJ3764 The xor-longest path Trie树
  9. poj1013
  10. yii2 访问控制
  11. SQL Server 数据库状态选项
  12. 《JS权威指南学习总结--8.3 函数的实参和形参》
  13. SpringMVC 的重定向时,参数非法问题
  14. SharedPreferences的使用
  15. ( 转 ) WebApiTestClient 的使用
  16. XBMC源代码分析 6:视频播放器(dvdplayer)-文件头(以ffmpeg为例)
  17. java 对一个字符串进行加减乘除的运算
  18. Spring AOP 详解[转]
  19. Python中的 __all__和__path__ 解析
  20. python编程之socket编程基础

热门文章

  1. (转)CocoaPods
  2. Codeforces Round #460 (Div. 2)-C. Seat Arrangements
  3. Codeforces Round #460 (Div. 2)-A. Supermarket
  4. poj 3045 叠罗汉问题 贪心算法
  5. 洛谷P4231 三步必杀
  6. Diycode开源项目 TopicContentActivity分析
  7. js 实现5秒倒计时后跳转页面
  8. leetcode 【 Two Sum 】python 实现
  9. mongo命令
  10. Python-S9-Day128——算法基础Algorithm