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 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
随机推荐
- 《java核心技术36讲》学习笔记-------杨晓峰(极客时间)
非常荣幸作为晓峰哥的同事,之前就看过这篇文章,重写读一遍,再学习学习. 一.开篇词 初级.中级:java和计算机科学基础.开源框架的使用:高级.专家:java io/nio.并发.虚拟机.底层源码.分 ...
- content-type 组件
content-type初识 什么是content-type ContentType是Django的内置的一个应用,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中 ...
- functools模块中partial的使用
一.简介 functools.partial(func,* args,**关键字) 返回一个新的部分对象,当被调用时,其行为类似于使用位置参数args 和关键字参数关键字调用的func.如果为调用提供 ...
- 使用BeautifulSoup
下载bs4,导入BeautifulSoup pip install bs4 from bs4 import BeautifulSoup BeautifulSoup 的使用 创建对象 r = reque ...
- HBase LSM树存储引擎详解
1.前提 讲LSM树之前,需要提下三种基本的存储引擎,这样才能清楚LSM树的由来: 哈希存储引擎. B树存储引擎. LSM树(Log-Structured Merge Tree)存储引擎. 2. 哈希 ...
- 阿里云ACA主要内容
阿里云 ACA,云计算助理工程师,是阿里云使用的一个入门级别课程.内容比较浅显,但都很很有意思的知识.课程的内容主要有7门,具体见下图: 课程的学习方式是视频+实验 先学习视频 再实际操作.阿里云为每 ...
- python小练习: 给定一个数组 按重复次数 降序排列输出 数组非空且为正整数
假设有个列表 a=[1,1,1,2,2,4,5,5,5,5] (非空且为正整数) 那么根据要求 最终输出的形式为 5,1,2,4 (按重复次数 降序排列输出) 代码实现及解释: a=[1,1,1 ...
- CTypes
参考:http://docs.pythontab.com/interpy/c_extensions/ctypes/ Python中的ctypes模块可能是Python调用C方法中最简单的一种.ctyp ...
- temp 和 tmp 文件
TMP和TEMP文件是各种软件或系统产生的临时文件,也就是常说的垃圾文件.Windows产生的临时文件,本质上和虚拟内存没什么两样,只不过临时文件比虚拟内存更具有针对性,单独为某个程序服务而已.而它的 ...
- sqlmap注入入门
sqlmap注入入门 sqlmap的用法: linux中: sqlmap [选项] Windows中: python sqlmap [选项] 常用的参数及含义: 目标 -d DIRECT ...