[数论]ZOJ3593 One Person Game
题意:一个人要从A走到B 只能走a布、b步、(a+b)步,可以往左或右走
问 最少要走几步,不能走到的输出-1
可以列出方程 $ax+by=A-B$
或者 $ax+(a+b)y=A-B$
或者 $bx+(a+b)y=A-B$
要取这三个方程的最小的$(x+y)$
根据$ax+by=gcd(a, b) $
当$A-B$不是$gcd$的倍数时 就不能走到
利用ex_gcd可以分别求出这三个方程的解,但求出的这组并非最小的
因此利用枚举斜率 得到的交点为最小的一组解
LL exgcd(LL a,LL b,LL &x,LL &y)
{
LL d=a;
if(b!=)
{
d=exgcd(b,a%b,y,x);
y-=(a/b)*x;
}
else x=,y=;
return d;
}
LL Abs(LL x)
{
return x<? -x:x;
}
LL A;
LL gao(LL a, LL b)
{
LL x, y;
LL g=exgcd(a, b, x, y);
x=x*(A/g), y=y*(A/g);
a/=g, b/=g; LL ans=Abs(x)+Abs(y);
for(int i=-;i<;i++)
ans=min(ans, Abs(x+(-x/b+i)*b)+Abs(y-(-x/b+i)*a));
for(int i=-;i<;i++)
ans=min(ans, Abs(x+(y/a+i)*b)+Abs(y-(y/a+i)*a));
return ans;
}
int main()
{
int t;
LL B,a,b;
cin>>t;
while(t--)
{
cin>>A>>B>>a>>b;
A=Abs(A-B);
if(!A)
{
puts("");
continue;
}
if(A%__gcd(a, b))
{
puts("-1");
continue;
}
cout<<min(gao(a, b), min(gao(a+b, a), gao(a+b, b)))<<endl;
}
return ;
}
ZOJ 3593
[数论]ZOJ3593 One Person Game的更多相关文章
- Codeforces Round #382 Div. 2【数论】
C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...
- NOIP2014 uoj20解方程 数论(同余)
又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...
- 数论学习笔记之解线性方程 a*x + b*y = gcd(a,b)
~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不 ...
- hdu 1299 Diophantus of Alexandria (数论)
Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- bzoj2219: 数论之神
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- hdu5072 Coprime (2014鞍山区域赛C题)(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...
- ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德
POJ 1061 青蛙的约会 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu Descr ...
- 数论初步(费马小定理) - Happy 2004
Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...
随机推荐
- 学习之spring属性文件注入
package com.my.proper; import org.springframework.beans.factory.annotation.Value; import org.springf ...
- spring aop配置及用例说明(3)
欢迎转载交流:http://www.cnblogs.com/shizhongtao/p/3476336.html 1.这里说一下aop的@Around标签,它提供了在方法开始和结束,都能添加用户业务逻 ...
- [java学习笔记]java语言基础概述之转义字符&break&continue
1.转义字符 \t:制表符 \n:回车 \b:退格 \r:回车 \":双引号 \\:反斜线(常用于文件路径的书写中) windows系统中回车符其实是由两个符号组成的,\r\n linu ...
- 在Mac OS X中使用VIM开发STM32(4)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在上三篇文章中,我们基本搭建好了开发STM32的IDE环境,当然vim.ctags.tagl ...
- Ubuntu Android Studio/IntelliJ IDEA 支持文件中文命名
Android Studio 默认字体无法使用中文命名文件,中文显示空心方块,使用思源字体,可解析 下载思源字体http://www.cnblogs.com/icgq/p/4195347.html 选 ...
- C# CRC32
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 解决java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, 系统找不到指定的文件 的错误
一.外部环境: 系统环境:Windows 8 磁盘分区:只有C盘 开发环境:IntelliJ IDEA Community Edition 2016.1.3(64) 执行代码:rdd.saveAsTe ...
- Mooncake (排序+贪心)
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- Linux下Mysql数据库备份
今天一同事的电脑无缘无故坏了,找了IT部门检测说是硬盘坏了,数据无法恢复.好悲剧.自己博客也写了好久不容易,要是突然间数据丢了那怎么办!于是写了个数据库自动备份脚本,并创建任务计划,实现每天22:30 ...
- 【Cardboard】 体验 - Google Cardboard DIY及完成后简单体验
体验 - Google Cardboard DIY及完成后简单体验 今年的Google I/O最让我感兴趣的除了Material Design以外就是这个Google Cardboard了.据说是Go ...