ZOJ Problem Set - 3593 拓展欧几里得 数学
ZOJ Problem Set - 3593
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593
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
题解:先求ax+by=A-B的解中|x|+|y|最小的一组x,y值,利用拓展欧几里得可求出ax'+by'=gcd(a,b)的x',y'值,仅在A-B是gcd(a,b)的整数倍时,方程有解,令k=(A-B)/gcd(a,b),则可以求出,当两条直线相交时|x|+|y|最小,求出交点对应的t,由于交点可能不是整数,所以将t-1,t,t+1各判断一次,若x,y同号,结果取绝对值大的(因为同向时x+y可以合并为1步),若x,y异号,结果取绝对值之和,通过ans记录所有结果中的最小值。
#include<iostream>
#include<cmath>
#include<cstdio>
#define ll long long
using namespace std;
void exgcd(ll a,ll b,ll &gcd,ll &x,ll &y){
if(!b){
gcd=a;x=1;y=0;
}else{
exgcd(b,a%b,gcd,x,y);
ll tmp=x;
x=y;
y=tmp-a/b*y;
}
}
int main(){
int T;
ll a,b,A,B,x,y,gcd,ans,t;
scanf("%d",&T);
while(T--){
scanf("%lld%lld%lld%lld",&A,&B,&a,&b);
exgcd(a,b,gcd,x,y);
//cout<<x<<" "<<y<<endl;
if((A-B)%gcd)
{printf("-1\n");continue;}
x=(A-B)/gcd*x;
y=(A-B)/gcd*y;
ans=9999999999;
a/=gcd,b/=gcd;
t=(y-x)/(a+b);
//直线x=x+bt与直线y=y-at的距离最近时对应的整数t
for(int i=t-1;i<=t+1;i++)//t不一定为整数,左右各取一次
if(abs(x+i*b)+abs(y-i*a)==abs(x+i*b+y-i*a))//同号
ans=min(ans,max(abs(x+i*b),abs(y-i*a)));
else//异号
ans=min(ans,abs(x+i*b)+abs(y-i*a));
printf("%lld\n",ans);
}
return 0;
}
ZOJ Problem Set - 3593 拓展欧几里得 数学的更多相关文章
- ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
Power of Fibonacci Time Limit: 5 Seconds Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3
141. Jumping Joe time limit per test: 0.25 sec. memory limit per test: 4096 KB Joe is a frog who lik ...
- 【lydsy1407】拓展欧几里得求解不定方程+同余方程
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1407 题意: 有n个野人,野人各自住在第c[i]个山洞中(山洞成环状),每年向前走p[i] ...
- NOIP2012拓展欧几里得
拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...
- poj 1061 青蛙的约会 拓展欧几里得模板
// poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...
- bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得
这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...
- POJ1061 青蛙的约会-拓展欧几里得
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
随机推荐
- 特殊计数序列——第二类斯特林(stirling)数
计算式 \[ S(n,m)=S(n-1,m-1)+mS(n,m) \] \(S(0,0)=1,S(i,0)=0(i>0)\) 组合意义 将\(n\)个不可分辨的小球放入\(m\)个不可分辨的盒子 ...
- 微信退款验证证书时报错:length too long
由于springboot文件加载时,默认会加载resources目录下的文件,而微信的证书刚好在它之下,加载时就会报这个错误.解决办法: 在pom.xml文件中,添加如下代码: <plugin& ...
- python学习day15 模块(重点)
模块(重点) python2,与py3的区别 py2:range() 在内存中立即把所有的值都创建,xrange() 不会再内存中立即创建,而是在循环时边环边创建. py3:range() 不会再内存 ...
- Educational Codeforces Round 62 (Rated for Div. 2)C
题目链接 :C. Playlist #include<bits/stdc++.h> using namespace std; #define maxn 300005 #define LL ...
- windows下网络编程UDP
转载 C++ UDP客户端服务器Socket编程 UDPServer.cpp #include<winsock2.h>#include<stdio.h>#include< ...
- C# activex开发中 axwebbrowser控件及 IE浏览器设置
<object type='application/x-vlc-plugin' id='vlc' events='True' codebase='../cab/axvlc.cab' classi ...
- Tomcat系列(8)——Tomcat运行模式连接数和线程池
Connector的主要功能,是接收连接请求,创建Request和Response对象用于和请求端交换数据:然后分配线程让Engine(也就是Servlet容器)来处理这个请求,并把产生的Reques ...
- axios传参
get //通过给定的ID来发送请求 axios.get('/user?ID=12345') .then(function(response){ console.log(response); }).c ...
- css中自定义字体
css代码如下: @font-face { font-family: 'HelveticaNeueLTPro-Th'; src: url('../fonts/HelveticaNeueLTPro-Th ...
- MySQL学习笔记(七)使用AutoMySQLBackup工具自动备份MySQL数据库
1.下载 wget https://nchc.dl.sourceforge.net/project/automysqlbackup/AutoMySQLBackup/AutoMySQLBackup%20 ...