ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

A. Exciting Bets

2021-11-26 09:58:42  阅读:203  来源: 互联网

标签:Bets get ll excitement fans abs Exciting first


题目:https://codeforces.com/contest/1543/problem/A
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:

Increase both a and b by 1.
Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
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)=x for any x≥0.

Input
The first line of input contains a single integer t (1≤t≤5⋅103) — the number of test cases.

The first and the only line of each test case contains two integers a and b (0≤a,b≤1018).

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

If the fans can get infinite excitement, print 0 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.

Example
inputCopy
4
8 5
1 2
4 4
3 9
outputCopy
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.

For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don’t need to apply any operation.

For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.

For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.

解题思路:
仔细观察,发现当a=b时那么他们的gcd可以无限大,题目中给出gcd(0,x)=x,那么我们就可以推出abs(a-b)就是最大的,而且步数要最少,那么再仔细思考下会发现把b增加或减少凑成abs(a-b)的倍数,取其中最小的就是答案(b%abs(a-b),abs(a-b)-b%abs(a-b)),增加a也是一样的结果,因为a = abs(a-b) + b

解题遇到的问题:
证明最小步骤的结论,没想清楚

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll gcd(ll a,ll b){
    while(b>0){ll temp = a%b;a = b;b = temp;}
    return a;
}

int main(){

    ios_base::sync_with_stdio(0);

    int t;
    cin >> t;

    while(t--){
        ll a,b;
        cin >> a >> b;
        if(a == b){
            cout << 0 << ' ' << 0 << endl;
        }else{
            ll ans1 = abs(a-b);
            ll ans2 = min(b%ans1,ans1-b%ans1);
            cout << ans1 << ' ' << ans2 << endl;
        }

    }
    

    return 0;
}

标签:Bets,get,ll,excitement,fans,abs,Exciting,first
来源: https://blog.csdn.net/he_qingjun/article/details/121553131

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有