ZOJ - 3593 One Person Game (扩展欧几里得)
题意:一个人在坐标A,要前往坐标B的位置。可以往左或往右走a,b,a+b个单位,求到达B的最小步数。
分析:扩展欧几里得算法求解线性方程的套路不变。令C=fabs(A-B),c = a+b, 扩展gcd分别求 ax+by=C ; ax+cy = C : bx+cy = C的最小|x|+|y|。求min{|x|+|y|}需要一点思考。
对于线性方程ax+by=c,设d = gcd(a,b) ,若方程有解,则必须d | c,特解为 (x0,y0) = ( xx*c/d,yy*c/d) 。设am = a/d, bm = b/d。
此时方程的通解为 x = x0+ k*bm ; y = y0 - k*am。则需要求的是 res = min{ |x0 + k*bm| + |y0 - k*am| }。
设直线L1:y1 = x0 + bm*k ; L2:y2 = y0 - am*k。
则|y1|+|y2| 的最小值一定出现在y1=0或y2=0,即k1=-x0/bm 或 k2 = y0/am 时(数形结合),但由于k是整数,所以不一定y1、y2能取到0。所以枚举区间[-x0/bm-1, -x0/bm+1]和[y0/am -1 , y0/am +1]的 k 对应值中的最小值。
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn = 1e5+; LL ABS(LL a)
{
return a>=?a :-a;
} LL Exgcd(LL a,LL b,LL &x,LL &y ) {
if ( b == ) {
x = ;
y = ;
return a;
}
LL d = Exgcd(b, a%b, x, y), temp = x;
x = y;
y = temp-a/b*y;
return d;
} LL gao(LL a,LL b,LL c) //ax+by=c
{
LL x,y;
LL d = Exgcd(a,b,x,y);
if(c%d) return -;
LL am = a/d, bm = b/d;
x *=c/d, y*= c/d; //特解
LL ans= ABS(x)+ABS(y);
for(int i=-x/bm-;i<=-x/bm+;i++){
LL X=x+bm*i;
LL Y=y-am*i;
if(i){
LL tmp=ABS(X)+ABS(Y);
if(tmp<ans) ans=tmp;
}
}
for(int i=y/am-;i<=y/am+;i++){
LL X=x+bm*i;
LL Y=y-am*i;
if(i){
LL tmp=ABS(X)+ABS(Y);
if(tmp<ans) ans=tmp;
}
}
return ans;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
LL a,b,A,B,k;
int T; scanf("%d",&T);
while(T--){
scanf("%lld %lld %lld %lld",&A, &B, &a, &b);
LL C = ABS(A-B),c = a+b;
LL t1 =gao(a,b,C), t2 = gao(a,c,C) ,t3 = gao(b,c,C);
if(t1==- && t2==- && t3==-){
puts("-1");
continue;
}
LL ans=;
if(t1!=-) ans = min(t1,ans);
if(t2!=-) ans = min(t2,ans);
if(t3!=-) ans = min(t3,ans);
printf("%lld\n",ans);
}
return ;
}
ZOJ - 3593 One Person Game (扩展欧几里得)的更多相关文章
- ZOJ 3593.One Person Game-扩展欧几里得(exgcd)
智障了,智障了,水一水博客. 本来是个水题,但是for循环遍历那里写挫了... One Person Game Time Limit: 2 Seconds Memory Limit: 655 ...
- ZOJ 3593 One Person Game(拓展欧几里得求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- ZOJ 3609 Modular Inverse(扩展欧几里得)题解
题意:求乘法逆元最小正正数解 思路:a*x≡1(mod m),则称x 是 a 关于 m 的乘法逆元,可以通过解a*x + m*y = 1解得x.那么通过EXGcd得到特解x1,最小正解x1 = x1 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- UVA 12169 Disgruntled Judge 枚举+扩展欧几里得
题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
- 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】
Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
随机推荐
- easyui 扩展 之 Tree的simpleData加载
实例化.这里增加了三个属性,可以指定idFiled,textFiled和parentField.所以这里的simpleData可以不严格转换成tree的数据格式. $(function(){ $('# ...
- 11个免费的Web安全测试工具
1.Netsparker Community Edition(Windows) 这个程序可以检测SQL注入和跨页脚本事件.当检测完成之后它会给你提供一些解决方案. 2.Websecurify(Wind ...
- Apollo 刨析:简介
Apollo是配置在IIS服务器上的一个Web站点,它使用了.NET4.0和ASP.Net的技术. 代码是C#编写的.是基于ASP.NET MVC3的Web开发框架上编写的一个应用. 它使用到了N ...
- SQL Server Replace函数
REPLACE用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法REPLACE ( 'string_expression1' , 'string_expression2' ...
- Python相对完美的URL拼接函数
首先说下什么叫URL拼接,我们有这么一个HTML片段: <a href="../../a.html">click me</a> 做为一只辛苦的爬虫,我们 ...
- 优化phpstorm运行卡顿问题
在PHPSTORM中点击导航菜单:Help -> Edit Custom VM Options 如果是第一次点击,会提示是否新建配置文件,点击“yse” 在弹出的编辑框末尾加上以下配置 -Daw ...
- JZOJ.5279【NOIP2017模拟8.15】香港记者
Description
- [HEOI2015]兔子与樱花[贪心]
4027: [HEOI2015]兔子与樱花 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1043 Solved: 598[Submit][Stat ...
- python拓展库whl下载网址集合:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
- 改变label中的某字体颜色
NSString *allString=@"你家在哪里,需要我送你么"; NSString *subString=@"在哪里"; UILabel *string ...