ZOJ 3593 One Person Game(ExGcd + 最优解)题解
思路:题意转化为求 (ax+by=dis) || (ax+cy=dis) || (bx+cy=dis) 三个式子有解时的最小|x| + |y|。显然求解特解x,y直接用扩展欧几里得,那么怎么求|x| + |y|?xy关系为一条直线,那么|x| + |y|应该是在x取0或者y取0的时候,但是要整数,所以只能在周围取几个点。我们知道x=x1+b/gcd*t,那么x1+b/gcd*t = 0可以解得 t = -x1 * gcd / b。然后在附近取几个点。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LS(n) node[(n)].ch[0]
#define RS(n) node[(n)].ch[1]
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; ll ex_gcd(ll a, ll b, ll &x, ll &y){
ll d, t;
if(b == ){
x = ;
y = ;
return a;
}
d = ex_gcd(b, a%b, x, y);
t = x-a/b*y;
x = y;
y = t;
return d;
} ll dis;
//求|x|+|y|最小
ll solve(ll a, ll b){
ll x, y, d = ex_gcd(a, b, x, y);
if(dis % d != ) return INF;
x = x * dis / d;
y = y * dis / d;
a /= d, b /= d;
ll ans = abs(x) + abs(y); ll k;
k = -x / b - ;
for(int i = ; i <= ; i++){
ans = min(ans, abs(x + b * (k + i)) + abs(y - a * (k + i)));
} k = y / a - ;
for(int i = ; i <= ; i++){
ans = min(ans, abs(x + b * (k + i)) + abs(y - a * (k + i)));
} return ans;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
ll a, b, A, B;
scanf("%lld%lld%lld%lld", &A, &B, &a, &b);
dis = abs(A - B);
ll ans;
ans = min(solve(a, a + b), min(solve(a, b), solve(b, a + b)));
printf("%lld\n", ans == INF? - : ans);
}
return ;
}
ZOJ 3593 One Person Game(ExGcd + 最优解)题解的更多相关文章
- ZOJ 3593.One Person Game-扩展欧几里得(exgcd)
智障了,智障了,水一水博客. 本来是个水题,但是for循环遍历那里写挫了... One Person Game Time Limit: 2 Seconds Memory Limit: 655 ...
- ZOJ Monthly, June 2014 月赛BCDEFGH题题解
比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不 ...
- ZOJ 3593 One Person Game(拓展欧几里得求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- ZOJ - 3593 One Person Game (扩展欧几里得)
题意:一个人在坐标A,要前往坐标B的位置.可以往左或往右走a,b,a+b个单位,求到达B的最小步数. 分析:扩展欧几里得算法求解线性方程的套路不变.令C=fabs(A-B),c = a+b, 扩展gc ...
- ZOJ 3593 One Person Game
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- ZOJ 4027 Sequence Swapping(DP)题解
题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...
- ZOJ 3829 Known Notation(贪心)题解
题意:给一串字符,问你最少几步能变成后缀表达式.后缀表达式定义为,1 * 1 = 1 1 *,题目所给出的字串不带空格.你可以进行两种操作:加数字,交换任意两个字符. 思路:(不)显然,最终结果数字比 ...
- [数论]ZOJ3593 One Person Game
题意:一个人要从A走到B 只能走a布.b步.(a+b)步,可以往左或右走 问 最少要走几步,不能走到的输出-1 可以列出方程 $ax+by=A-B$ 或者 $ax+(a+b)y=A-B$ 或者 ...
- One Person Game(zoj3593+扩展欧几里德)
One Person Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status ...
随机推荐
- 十 js中forEach,for in,for of循环的用法
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i++) { console.log(i ...
- Rpgmakermv(7) Chronus插件介绍翻译
协议:MIT 作用:时间,有时间推进,可以设置速度,随着游戏中时间的推进,会发生昼夜改变和天气变化. ------------------------------------------------- ...
- Python2.6 升级2.7
一. Centos6 默认为python2.6且不可卸载(因为Centos6深度依赖Python),要想升级为2.7 只能通过全新升级 操作如下: 1.下载 Python2.7 网址 https:// ...
- Java多线程-----原子变量和CAS算法
原子变量 原子变量保证了该变量的所有操作都是原子的,不会因为多线程的同时访问而导致脏数据的读取问题 Java给我们提供了以下几种原子类型: AtomicInteger和Ato ...
- 原型设计模式 Prototype
参考1 http://www.cnblogs.com/libingql/p/3633377.html http://www.cnblogs.com/promise-7/archive/2012/06/ ...
- Win7 Python开发环境搭建
1. 下载Anaconda并安装 地址: https://www.anaconda.com/download/ Anaconda包括Python基础包与一系列科学计算包,安装后不用再单独安装Pyth ...
- Njinx配置
参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:http://www.nginx.c ...
- js相关(easyUI),触发器,ant,jbpm,hibernate二级缓存ehcache,Javamail,Lucene,jqplot,WebService,regex,struts2,oracle表空间
*********************************************js相关********************************************* // 在指 ...
- element ui里dialog关闭后清除验证条件
//vue <!--添加用户dialog begin--> <el-dialog title="编辑用户" :visible.sync="dialogF ...
- 基于SecureCRT的测试环境的克隆的linux/vi相关命令
#chmod -R 777*; 参数-R:对当前目录下的所有档案与子目录进行相同的权限 变更(即以递归的方式逐个变更). *:通配符,指的是当前目录下的所有文件及目录. 所有文件及其子目录的文件拥有者 ...