欢迎光临散文网 会员登陆 & 注册

UP主的第一场竞赛——CodeForces Round#730(Div2)题目A

2021-07-11 12:36 作者:非知名科技区UP  | 我要投稿

作为信息竞赛人,我们需要学会完成“作业”和进行“考试”这两项技能。近期UP主和队内的某位hxd一起相约远翥楼八层教室,开展了那位hxd第N次,本UP主(cai gou)的第一次限时考试。其中有一道数学题让本UP印象深刻,下面请看题。



Welcome to Rockport City!

It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x,y) denotes the greatest common divisor (GCD) of integers x and y. To make the race more exciting, you can perform two types of operations:

  1. Increase both a and b by 1.

  2. Decrease both a and b by 1. This operation can only be performed if both aa and bb are greater than 0.

  3. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.

Note that gcd(x,0)=for any x0.

Input

The first line of input contains a single integer t (1%3C%3Dt%3C%3D5*10%5E3) — the number of test cases.

The first and the only line of each test case contains two integers a and b <10^18(%0A).

Output

For each test case, print a single line containing two integers.

If the fans can get infinite excitement, print 0.

Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.

简单解释一下,张三和李四正在押钱下赌注,而围观群众获得的兴奋度是张三和李四押钱数目的最大公约数。二人可以一次同时增加或减少一元的赌注,已知当下张三和李四押下的赌资数额,请你算出他们产生的最大兴奋值和产生最大兴奋值最少需要进行多少次赌注的增减。

如上,提到最大公约数,我想各位必然会先想到我们之前学过的一种快速取余算最大公约数的方法,但是如果加上了两人的操作,我们就必须进行搜索。而根据数据范围显示,进行搜索,一定是不能解决问题的,而且ACM赛制最大的特点就是一道题不能拿满分就是零分,所以,采用数学方法完成这个问题大概率是唯一的解法。

根据题目所给的信息,无论我们如何进行操作,二人赌注数额的差值是一定的,这也许就是一个突破口。在UP考试的时候就利用了这一点,而且我可以证明最大公约数绝对不会超过两个输入数据之间差的绝对值。

为什么?

因为如果不符合这个条件,那么若较小数再加上一个公约数就一定会大于较大的数,这个数就一定不是公约数了。所以,我可以大致上认定输出的最大兴奋值一定是两个数的差的绝对值。

那么将较小的数作为0和大数的中点岂不是就可以了?然后我们也就能够得到最少所需的操作次数。

其实这样也不是一个正确的选择,对于这一点我们可以参看一下样例输入中的第一条数据,仔细研究你就会发现,我们要找的本质上是较小的输入值和大小输入值差的整倍数的最小距离,如此一来就能够得到正确的解了。

文章的最后,放出AC代码


UP主的第一场竞赛——CodeForces Round#730(Div2)题目A的评论 (共 条)

分享到微博请遵守国家法律