One Person Game(扩展欧几里德求最小步数)
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 A, B, a and b, separated by spaces. (-231 ≤ A, B < 231, 0 < a, b < 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
题解:不难列出线性方程a(x+z)+b(y+z)=B-A;即ax+by=C;
主要是中间找最小步数;//由于a+b可以用c来代替;所以,当x和y同号时, 34 //就可以用z=min(x,y)+max(x,y)-min(x,y)=max(x,y)来走,也就是一部分步数可以等于a+b 35 //所以还要找这种情况的步数。。。 36 //因为x和y越接近,(a+b)*step的越多,越优化, 37 //所以要在通解相交的点周围找;由于交点可能为小数,所以才在周围找的;
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3fffffff;
void e_gcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){
d=a;
x=;
y=;
}
else{
e_gcd(b,a%b,d,x,y);
LL temp=x;
x=y;
y=temp-a/b*y;
}
}
LL cal(LL a,LL b,LL c){
LL x,y,d;
e_gcd(a,b,d,x,y);
//printf("%lld %lld %lld\n",d,x,y);
if(c%d!=)return -;
x*=c/d;
y*=c/d;
//x=x0+b/d*t;y=y0-a/d*t;
b/=d;a/=d;
//由于a+b可以用c来代替;所以,当x和y同号时,
//就可以用z=min(x,y)+max(x,y)-min(x,y)=max(x,y)来走,也就是一部分步数可以等于a+b
//所以还要找这种情况的步数。。。
//因为x和y越接近,(a+b)*step的越多,越优化,
//所以要在通解相交的点周围找;由于交点可能为小数,所以才在周围找的;
LL mid=(y-x)/(a+b); // x0+bt=y0-at;
LL ans=(LL)INF*(LL)INF;
LL temp;
// printf("%lld\n",ans);
for(LL t=mid-;t<=mid+;t++){
if(abs(x+b*t)+abs(y-a*t)==abs(x+b*t+y-a*t))
temp=max(abs(x+b*t),abs(y-a*t));
else temp=abs(x+b*t)+abs(y-a*t);
ans=min(ans,temp);
// printf("%lld\n",temp);
}
return ans;
}
int main(){
LL T,A,B,a,b;
scanf("%lld",&T);
while(T--){
scanf("%lld%lld%lld%lld",&A,&B,&a,&b);
LL ans=cal(a,b,B-A);
printf("%lld\n",ans);
}
return ;
}
One Person Game(扩展欧几里德求最小步数)的更多相关文章
- POJ-1061青蛙的约会,扩展欧几里德求逆元!
青蛙的约会 以前不止一次看过这个题,但都没有去补..好吧,现在慢慢来做. 友情提示 ...
- ZOJ 3593 One Person Game(拓展欧几里得求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)
题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现 ...
- CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元
题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...
- 公钥密码之RSA密码算法扩展欧几里德求逆元!!
扩展欧几里得求逆元 实话说这个算法如果手推的话问题不大,无非就是辗转相除法的逆过程,还有一种就是利用扩展欧几里德算法,学信安数学基础的时候问题不大,但现在几乎都忘了,刷题的时候也是用kuangbin博 ...
- HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法
地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others) M ...
- POJ 3185 The Water Bowls (高斯消元 求最小步数)
题目链接 题意:有20个数字,0或1.如果改变一个数的状态,它左右两边的两个数的状态也会变反.问从目标状态到全0,至少需要多少次操作. 分析: 和上一题差不多,但是比上一题还简单,不多说了,但是在做题 ...
- poj 3009 冰球 【DFS】求最小步数
题目链接:https://vjudge.net/problem/POJ-3009 转载于:https://www.cnblogs.com/Ash-ly/p/5728439.html 题目大意: 要求把 ...
- 51Nod 3的幂的和(扩展欧几里德求逆元)
求:3^0 + 3^1 +...+ 3^(N) mod 1000000007 Input 输入一个数N(0 <= N <= 10^9) Output 输出:计算结果 Input示例 3 O ...
随机推荐
- apktool 反翻译错误
-出现 UndefinedResObject Exception : 这是因为被反编译的apk中有当前的framework不支持的属性:解决方式如下: 1.删除C:\Users\Administrat ...
- R与数据分析旧笔记(四)画地图练习
> library(maps) > library(geosphere) 载入需要的程辑包:sp > map("state")#画美国地图 > map(&q ...
- page cache和buffer cache
因为要优化I/O性能,所以要理解一下这两个概念,这两个cache着实让我迷糊了好久,通过查资料大概明白了两者的区别,试着说下. page cache:文件系统层级的缓存,从磁盘里读取的内容是存储到这里 ...
- Arduino Micro USB库
USBCore.cpp #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_co ...
- IIS启动网站
在启动一个网站前要先打开一个服务.可是Windows 的提示非常的奇怪: “除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法 ...
- 普通内存、ECC内存和REG ECC内存有什么不同
都知道,在INTEL平台,北桥负责与CPU的联系,并控制内存.AGP.PCI数据在北桥内部传输.基本上只要主板芯片组确定,那么其支持的内存类型也就确定了. INTEL芯片组划分的很清楚,865PE属于 ...
- 【转】android权限列表
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...
- rsyslog 走tcp通讯配置
发送端: local5.* @@192.168.32.76 front-end:/usr/local/nginx/logs# cat /etc/rsyslog.conf 日志服务器端配置: # Pro ...
- iOS中菊花。。。
其实系统的菊花除了功能有点单一,不具有刷新页面的功能以外,其他都还好吧,满足你一些正常的提示功能还是绰绰有余的:直接把项目里面的代码粘出来吧,其实也没有什么特别要注意的地方,一些设置属性也算留个备份 ...
- 第七届河南省赛A.物资调度(dfs)
10401: A.物资调度 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 95 Solved: 54 [Submit][Status][Web Bo ...