目录

1.题目

2.中文翻译

3.思路解析:

4.代码实现:


1.题目

One Person Game

Description 

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. ($-2^{31} ≤ A, B < 2^{31}, 0 < a, b < 2^{31}$)

Output
 For each test case, output the minimum number of steps. If it’s impossible to reach point B, output “-1” instead.

Examples
intput

2
0 1 1 2
0 1 2 4

output

1
-1

2.中文翻译

题目描述

有一个有趣而简单的单人游戏。假设你脚下有一个数字轴。你一开始是在点 A ,你的目标是点  B  。一步可以执行6种操作。也就是向左或向右移动 a、b 和 c ,这里 c 总是等于 a+b 。

你必须尽快到达B。请计算最小步数。

输入

有多个测试用例。输入的第一行是一个整数 T(0<T≤1000) 表示测试用例的数量。然后是T行测试案例。每个测试用例由一行表示,该行包含四个整数4个整数 A、B、a 和 b,用空格分隔。(-2^{31}≤A,B<2^{31},0<A,B<2^{3})

输出

对于每个测试用例,输出最小步骤数。如果无法到达B点,则输出“-1”。

样例输入

样例输出

3.思路解析:

1.题目其实就是求 C1*a + C2*b + C3*c + C4*(-a)+C5*(-b)+C6*(-c)| = |A-B|,根据题意化简之后就变成了 |C1a+C_2b| = |A-B| ,目的就是判断左边这个方程是否有解.

2.欧几里得扩展算法:我的算法专栏里面有讲解。

4.代码实现:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int N = 1e5+10;LL exgcd(LL a,LL b,LL &x,LL &y)
{if(b==0){x=1, y=0;return a;}LL ans=exgcd(b,a%b,x,y);LL tmp=x;x=y;y=tmp-a/b*y;return ans;
}int main()
{int t;scanf("%d", &t);while(t--){LL A, B, a, b, c, x, y;scanf("%lld %lld %lld %lld",&A, &B, &a, &b);if(A>B) swap(A,B);c=(B-A);LL g=exgcd(a,b, x, y);if(c%g!=0) puts("-1");else{a/=g, b/=g;x*=(c/g), y*=(c/g);LL k=(y-x)/(a+b);LL ans=10000000000ll;for(LL i=k-1;i<=k+1;i++){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));}cout<<ans<<endl;}}return 0;
}

ZOJ One Person Game相关推荐

  1. ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!

    两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...

  2. zoj 1204 Additive equations

    ACCEPT acm作业 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204 因为老师是在集合那里要我们做这道题.所以我很是天 ...

  3. 【HDU/POJ/ZOJ】Calling Extraterrestrial Intelligence Again (素数打表模板)

    http://poj.org/problem?id=1411  POJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=168 ...

  4. 模拟 ZOJ 3878 Convert QWERTY to Dvorak

    题目传送门 1 /* 2 模拟:手敲map一一映射,累! 3 除了忘记读入字符串不能用gets用getline外还是很顺利的AC了:) 4 */ 5 #include <cstdio> 6 ...

  5. 矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence

    题目传送门 1 /* 2 题意:加上适当的括号,改变计算顺序使得总的计算次数最少 3 矩阵连乘积问题,DP解决:状态转移方程: 4 dp[i][j] = min (dp[i][k] + dp[k+1] ...

  6. ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)

    ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...

  7. 九度OJ—题目1032:ZOJ

    题目描写叙述: 读入一个字符串.字符串中包括ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出.当某个字符用完时,剩下的仍然依照ZOJ的顺序输出. 输入: 题目包括多组用例,每组用例占一行,包括ZOJ ...

  8. ZOJ 1410 题解

    题目链接:http://acm.zju.edu.cn/show_problem.php?pid=1410 又是一道简单题,以前做过一次,结果放弃了,今天看了一下,觉得还是比较简单于是下手了. 题目的大 ...

  9. POJ 1201 amp; HDU1384 amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  10. poj 1436 zoj 1391 Horizontally Visible Segments (Segment Tree)

    ZOJ :: Problems :: Show Problem 1436 -- Horizontally Visible Segments 用线段树记录表面能被看见的线段的编号,然后覆盖的时候同时把能 ...

最新文章

  1. WC前的颓废——带花树
  2. java restful netty_Java RESTful 框架的性能比较
  3. Java 导出excel表 POI
  4. 剑指offer:滑动窗口最大值
  5. Java 什么叫做实例化
  6. java缓冲流,BufferedReader,BufferedWriter 详解
  7. HTML表格,table,thead,tbody,tfoot,th,tr,td,的属性以及跨行,跨列
  8. 【图像处理】——Python+opencv实现提取图像的几何特征(面积、周长、细长度、区间占空比、重心、不变矩等)
  9. c语言如何随机获取1kb,基于VS2010+C语言实现播放器的顺序播放、随机播放
  10. cmd - 使用curl命令的注意点
  11. 并查集——小米笔试题求朋友圈个数,分小组个数
  12. JavaScript中数组交集的最简单代码
  13. mfc 固定编辑框输入上限和下限_S7200smart的模拟量输入输出
  14. Cesium:加载json数据
  15. [转] 怎么减少编程中的 bug?
  16. 网易云ncm,QQ音乐qmc,mgg,mflac,酷狗kgm解锁转换为flac格式
  17. 电机选型计算电机转动惯量、启动转矩和额定转速
  18. 微信小程序转发分享及好友点击进入传参
  19. 关于我发的这个PDF文件的理解
  20. 离散数学-2 命题逻辑等值演算

热门文章

  1. 取代资源管理器的工具 Directory Opus
  2. Calendar介绍
  3. 网上 软件各种版本的定义和解释Beta、RC、Release
  4. 实例浅析:中小型旅游网站站内优化五大要点
  5. nodejs+vue社区重点人员户籍信息查询系统
  6. 100个python算法超详细讲解:冒泡排序
  7. Java增强for循环foreach
  8. 微型计算机原理实验报告系统认识,微机原理 实验报告——显示程序实验与8259应用编程实验...
  9. 华硕如何把黄静送进看守所?
  10. mysql修改学生信息语句,【MySQL】MySQL基础操作语句