ZOJ One Person Game
目录
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相关推荐
- ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!
两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...
- zoj 1204 Additive equations
ACCEPT acm作业 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204 因为老师是在集合那里要我们做这道题.所以我很是天 ...
- 【HDU/POJ/ZOJ】Calling Extraterrestrial Intelligence Again (素数打表模板)
http://poj.org/problem?id=1411 POJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=168 ...
- 模拟 ZOJ 3878 Convert QWERTY to Dvorak
题目传送门 1 /* 2 模拟:手敲map一一映射,累! 3 除了忘记读入字符串不能用gets用getline外还是很顺利的AC了:) 4 */ 5 #include <cstdio> 6 ...
- 矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence
题目传送门 1 /* 2 题意:加上适当的括号,改变计算顺序使得总的计算次数最少 3 矩阵连乘积问题,DP解决:状态转移方程: 4 dp[i][j] = min (dp[i][k] + dp[k+1] ...
- ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)
ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...
- 九度OJ—题目1032:ZOJ
题目描写叙述: 读入一个字符串.字符串中包括ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出.当某个字符用完时,剩下的仍然依照ZOJ的顺序输出. 输入: 题目包括多组用例,每组用例占一行,包括ZOJ ...
- ZOJ 1410 题解
题目链接:http://acm.zju.edu.cn/show_problem.php?pid=1410 又是一道简单题,以前做过一次,结果放弃了,今天看了一下,觉得还是比较简单于是下手了. 题目的大 ...
- 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 ...
- poj 1436 zoj 1391 Horizontally Visible Segments (Segment Tree)
ZOJ :: Problems :: Show Problem 1436 -- Horizontally Visible Segments 用线段树记录表面能被看见的线段的编号,然后覆盖的时候同时把能 ...
最新文章
- WC前的颓废——带花树
- java restful netty_Java RESTful 框架的性能比较
- Java 导出excel表 POI
- 剑指offer:滑动窗口最大值
- Java 什么叫做实例化
- java缓冲流,BufferedReader,BufferedWriter 详解
- HTML表格,table,thead,tbody,tfoot,th,tr,td,的属性以及跨行,跨列
- 【图像处理】——Python+opencv实现提取图像的几何特征(面积、周长、细长度、区间占空比、重心、不变矩等)
- c语言如何随机获取1kb,基于VS2010+C语言实现播放器的顺序播放、随机播放
- cmd - 使用curl命令的注意点
- 并查集——小米笔试题求朋友圈个数,分小组个数
- JavaScript中数组交集的最简单代码
- mfc 固定编辑框输入上限和下限_S7200smart的模拟量输入输出
- Cesium:加载json数据
- [转] 怎么减少编程中的 bug?
- 网易云ncm,QQ音乐qmc,mgg,mflac,酷狗kgm解锁转换为flac格式
- 电机选型计算电机转动惯量、启动转矩和额定转速
- 微信小程序转发分享及好友点击进入传参
- 关于我发的这个PDF文件的理解
- 离散数学-2 命题逻辑等值演算