Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.*; public class abc { static long[] inArray(int n,Scanner sc){ long a[]=new long[n]; for (int i = 0; i < n; i++) { a[i]=sc.nextLong(); } return a; } public static int gcd(int a, int b, int x, int y) { // Base Case if (a == 0) { x = 0; y = 1; return b; } int x1=1, y1=1; // To store results of recursive call int gcd = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return gcd; } static int in(Scanner sc){ return sc.nextInt(); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ long a=sc.nextLong(),b=sc.nextLong(); long x=Math.abs(a-b); if(x==1) System.out.println("1 0"); else if(x==0) System.out.println("0 0"); else { System.out.print(x+" "); long y=Math.min(a,b); if(y<x) System.out.print(Math.min(y,x-y)); else System.out.print(Math.min(a%x,x-a%x)); System.out.println(); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class Main { public static long gcd(long a, long b) { if(b==0) return a; return gcd(b,a%b); } public static void main(String[] args) throws java.io.IOException{ Scanner sc=new Scanner(System.in); int t =sc.nextInt(); while(t-->0) { long a = sc.nextLong(); long b = sc.nextLong(); if(a==b) { System.out.println("0 0"); continue; } if(a==0 || b==0) { long ans=a+b; System.out.println(ans+" 0"); continue; } long diff = Math.abs(a-b); long base = a/diff; long ans = a-(base*diff); base++; if(((base*diff)-a)<ans) ans=(base*diff)-a; System.out.println(diff+" "+ans); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> #define inf 100000000 #define int long long #define ll long long #define fr(a,b) for(int i = a; i < b; i++) #define rep(i,a,b) for(int i = a; i < b; i++) #define repn(i,a,b) for(int i = a-1; i >=0; i--) #define prDouble(x) cout << fixed << setprecision(10) << x #define endl "\n" #define yes cout<<"YES"<<"\n" #define no cout<<"NO"<<"\n" #define mod 1000000007 #define pb push_back #define all(x) (x).begin(), (x).end() #define vi vector<int> #define read(x) int x; cin >> x #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) #define pi pair < int,int > using namespace std; signed main() { fast_io; int t; cin>>t; while(t--) { int a,b; cin>>a>>b; int x=abs(a-b); if (x==0) cout<<0<<" "<<0<<endl; else cout<<abs(a-b)<<" "<<min((b%x),x-(b%x))<<endl; } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import sys def ints(): return list(map(int, sys.stdin.readline().strip().split())) tc = int(input()) while tc: tc-=1 n, m = map(int, input().split()) if abs(n-m) == 0: print(0, 0) continue # elif abs(n-m) == 1: # print(1, 0) # continue print( abs(n-m) , min( abs(n-m)-( min(n, m) % abs(n-m) ), (min(n, m) % abs(n-m) ) ))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.*; import java.lang.Math.*; import java.lang.Math.*; public class solution{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int l = 0;l<t;l++){ long a = sc.nextLong(); long b = sc.nextLong(); if(a == b){ System.out.println("0 0"); }else{ if(a>b){ long temp = a; a = b; b = temp; } long gcd = Math.abs(a-b); long mod = a%gcd; long steps = Math.min(mod,gcd-mod); System.out.println(gcd + " " + steps); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <iostream> #include<string> #include<algorithm> #include<climits> #include<cmath> #include<vector> #include<map> using namespace std; #define ll long long // void pg() // { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif // } int main(){ ll t; cin>>t; while(t--){ ll a,b; cin>>a>>b; if(a==b){ cout<<0<<" "<<0<<endl; continue; } ll c=abs(a-b); ll d=c-(a%c); d=min(d,c-d); if(a%c==0) d=0; cout<<c<<" "<<d<<endl; } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.Scanner; // problem 1543A - exciting bets public class cf4 { public static void main(String[] args) { Scanner kb = new Scanner(System.in); int num = kb.nextInt(); long a, b, dif, moves; for(int i = 0; i < num; i++) { a = kb.nextLong(); b = kb.nextLong(); dif = Math.abs(a-b); moves = dif == 0 ? 0 : Math.min(a % dif, dif - (a % dif)); System.out.println(dif + " " + moves); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back typedef long long ll; typedef pair<int,int> pii; const ll MOD = 1e9 + 7; const int N = 2e5 + 5; int n, t; ll gcd(ll a, ll b){ if(a % b) return gcd(b, a % b); return b; } int main(){ scanf("%d", &t); while(t--){ ll a, b; scanf("%lld %lld", &a, &b); if(a == b) printf("0 0\n"); else if(!a || !b) printf("%lld 0\n", max(a, b)); else printf("%lld %lld\n", abs(a - b), min(a % abs(a - b), abs(a - b) - a % abs(a - b))); } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
for _ in range(int(input())): a, b = map(int, input().split()) t = abs(a-b) if t==0: print(0,0) else: t2 = min(t-(a%t),a%t) print(t,t2)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> #include <algorithm> using namespace std; typedef long long ll; /*int gcd(ll n1,ll n2) { return _gcd(n1,n2); } */int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--) { ll a,b,c,d; cin>>a>>b; d=abs(a-b); if(d==0) { cout<<"0 0"<<endl; } else { c=min(a%d , d-(a%d)); cout<<d<<" "<<c<<endl; } } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import sys import math #sys.stdin=open('input.txt','r') #sys.stdout=open('output.txt','w') def solve(): #n=int(input()) a,b=map(int,input().split()) if(a==b): print("0 0") else: r=abs(a-b) #z=max(a,b) if(math.gcd(a,b)==r): print(r,"0") else: e=a//r y1=((e+1)*r)-a y2=abs(((e)*r)-a) print(abs(a-b),min(y1,y2)) t=int(input()) while(t!=0): solve() t-=1
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> //Policy Based Data Structure // using namespace __gnu_pbds; //Policy Based Data Structure using namespace std; // typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; //Policy Based Data Structure // #define gc getchar_unlocked // #define pqb priority_queue<int> // #define pqs priority_queue<int, vi, greater<int> > // #define mk(arr,n,type) type *arr = new type[n] #define fo(i,n) for(i=0;i<n;i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define int long long #define endl '\n' #define w(t) int t; cin>>t; while(t--) #define deb(x) cout << #x << "=" << x << endl #define deb2(x,y) cout << #x << "=" << x << "," << #y << "=" << y << endl void print(bool n) { if (n) { cout << "YES\n"; } else { cout << "NO\n"; } } #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x,val) memset(x, val, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it,a) for(auto it = a.begin(); it != a.end(); it++) #define ps(x,y) fixed<<setprecision(y)<<x #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define PI 3.1415926535897932384626 #define inf 1e18 // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //Random Shuffler typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpii; typedef vector<vi> vvi; typedef map<int, int> mii; int mpow(int base, int exp); void ipgraph(int m); void dfs(int u, int par); const int mod = 1000000007; // const int N = 3e5, M = N; // vi g[N]; // no of prime numbers in range : (70,19) , (1000,168), (100000,1229) , (sqrt(10^9),3409) //======================= void sol() { int a, b; cin >> a >> b; if (a - b == 0) { cout << 0 << " " << 0 << endl; return; } if (a < b) { swap(a, b); } int ans = a - b; int ans2 = inf; if (((a + ans - 1) / ans) * ans - a >= 0) { ans2 = min(ans2, ((a + ans - 1) / ans) * ans - a); } if ( a - (a / ans)*ans >= 0) { ans2 = min(ans2, a - (a / ans) * ans); } if (((b + ans - 1) / ans)*ans - b >= 0) { ans2 = min(ans2, ((b + ans - 1) / ans) * ans - b); } if (b - (b / ans)*ans >= 0) { ans2 = min(ans2, b - (b / ans) * ans); } cout << ans << " " << ans2 << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif w(t) sol(); return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % mod; base = (base * base) % mod; exp >>= 1; } return result; } // void ipgraph(int n, int m){ // int i, u, v; // while(m--){ // cin>>u>>v; // g[u-1].pb(v-1); // g[v-1].pb(u-1); // } // } // // void dfs(int u, int par){ // for(int v:g[u]){ // if (v == par) continue; // dfs(v, u); // } // }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.Scanner; public class ExcitingBets { public static void main(String[] args){ int numberofTest; Scanner sc = new Scanner(System.in); numberofTest = sc.nextInt(); long testCase [][] = new long[numberofTest][2]; for(int i=0; i<numberofTest; i++){ testCase[i][0] = sc.nextLong(); testCase[i][1] = sc.nextLong(); } for(int i=0; i<numberofTest; i++){ long maxExcitement = Math.abs(testCase[i][0]-testCase[i][1]); if(maxExcitement == 0){ System.out.println(""+0+" "+0); } else{ long reqMoves = Math.min(testCase[i][0] % maxExcitement, maxExcitement - testCase[i][0] % maxExcitement); System.out.println(""+maxExcitement+" "+reqMoves); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
def calc(): a, b = list(map(int, input().split(' '))) if a == b: print('0 0') return d = abs(b - a) x = a % d y = d - x print("{0} {1}".format(d, min([x, y]))) t = int(input()) for _i in range(t): calc()
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
///to serach //n to go forwards N to bakcwards //? to search backwards???? idr //bitwise #include <bits/stdc++.h> using namespace std; #define ll long long #define vi vector<long long> #define vpi vector<pair<ll, ll>> #define pb push_back #define pi pair<ll, ll> #define mp make_pair #define mod 4294967295 #define p pair #define int ll signed main(){ // freopen("test.in","r",stdin); int t = 1; cin>>t; while(t--){ ll a, b; cin>>a>>b; ll x = max(a, b)-min(a, b); cout<<x<<" "; if(x==0){ cout<<0<<endl; }else{ cout<<min(a%x, x-(a%x))<<endl; } } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
tests=int(input()) for _ in range(tests): a,b=map(int,input().split()) k=abs(a-b) if k==0: print(0,0) else: print(k, min(a%k,k-a%k))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
//Jai Shri Ram //Jai Shri Krishna #include<bits/stdc++.h> using namespace std; #define int long long typedef vector<int> vi ; typedef map<int,int> mii ; typedef pair<int,int> pii ; #define pb push_back #define all(V) V.begin(),V.end() #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define distinct(v) distance(v.begin(), unique(all(v))) //first sort then use #define ub upper_bound #define lb lower_bound #define ff first #define ss second #define fill(baxq,I) (memset(baxq,I,sizeof(baxq))) #define sz(x) (int)((x).size()) #define endl "\n" const int32_t mod=1e9+7; const long long inf=1e18; const int N = 1e5+5; void testCases() { int a,b; cin>>a>>b; if(a==b)cout<<0<<" "<<0<<endl; else { int x= abs(a-b); int y=max(a,b); int q=min(a,b); int h=x*((y+x-1)/x) - y; int k= q- x*((q)/x); cout<<abs(a-b)<<" "<<min(h,k)<<endl; } } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int tcs=1; cin>>tcs; while(tcs--) { testCases() ; } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
from math import * t = int(input()) while t != 0: t -= 1 tmp = input().split() a = max(int(tmp[0]), int(tmp[1])) b = min(int(tmp[0]), int(tmp[1])) if a == b: print(0, 0) continue x = a - b y = b - int(b/x)*x w = int(b/x + 1)*x - b print(x, min(y, w))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; import java.util.HashSet; import java.util.TreeSet; import java.util.Iterator; public class First { public static void main(String[] args) { FastScanner fs = new FastScanner(); int T = fs.nextInt(); for (int tt = 0; tt < T; tt++) { long a=fs.nextLong(), b=fs.nextLong(); if(a==b) { System.out.println("0 0"); continue; } if(a<b) { long t=a; a=b; b=t; } long g=a-b; long steps=g-(a%g); steps=Math.min(steps, g-steps); if(g==steps) { steps=0; } System.out.println(g+" "+steps); } } static int solve() { return 0; } static int MOD=(int)(1e9+7); static void debug1(int[] arr) { for(int i=0;i<arr.length;++i) System.out.print(arr[i]+" "); System.out.println(); } static void debug2(int[][] arr) { for(int i=0;i<arr.length;++i) for(int j=0;j<arr[0].length;++j) System.out.println(arr[i][j]+" "); System.out.println(); } static int[] take1(int n, FastScanner fs) { int[] arr=new int[n]; for(int i=0;i<n;++i) arr[i]=fs.nextInt(); return arr; } static int[][] take2(int m, int n, FastScanner fs) { int[][] arr=new int[m][n]; for(int i=0;i<m;++i) for(int j=0;j<n;++j) arr[i][j]=fs.nextInt(); return arr; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.Scanner; public class Bets { public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long a[] = new long[n]; long b[] = new long[n]; for(int i =0;i<n;i++) { int countplus = 0,countminus = 0; a[i] = sc.nextLong(); b[i] = sc.nextLong(); long dif = Math.abs(a[i]-b[i]); // System.out.println(gcd(a[i],b[i])); if(gcd(a[i],b[i])!=dif && dif>0) { long rem = a[i]%dif; long number = dif/2; if(number<rem) { long y = dif-rem; System.out.print(dif+" "); System.out.println(y); }else if(number>=rem) { System.out.print(dif+" "); System.out.println(rem); }else { System.out.println(dif+" "); System.out.println("0"); } // System.out.println(a[i]); // System.out.println(b[i]); }else { System.out.print(dif+" "); System.out.println("0"); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import math,sys #sys.stdin=open('input.txt','r') #sys.stdout=open('output.txt','w') def solve(): #print(math.gcd(7,5),"s") n,m=map(int,input().split()) if(n==m): print("0 0") else: diff=abs(n-m) if(n%diff==0): ans=0 else: f=n//diff diff1=n-(f*diff) diff2=(f+1)*diff-n ans=(min(diff1,diff2)) #print(diff1,diff2) print(diff,ans) t=int(input()) while(t!=0): solve() t-=1
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <cstdio> #include <cstring> #include <iostream> #include <set> #include <vector> #include <cmath> #include <map> #include <algorithm> #include <utility> #include <string> #include <cstdlib> #include <queue> #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3,"Ofast","inline") using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; const ll N = 2e5 + 7; ll t, ma, nm, cnt = 0, edx, mi = N, sum; ll num[N]; ll mm[N]; int main() { cin >> t; while (t --) { ll a, b; cin >> a >> b; if (a == b) { cout << "0 0" << endl; continue; } int c = max(a, b) - min(a, b); nm = 0; ll g = abs(a-b); ll m = min(a%g,g-a%g); cout << g << " " << m << endl; } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { long a = scanner.nextLong(); long b = scanner.nextLong(); if (a == b) { System.out.println(0 + " " + 0); } else { System.out.print(Math.abs(a-b) + " "); System.out.println(Math.min(a % Math.abs(a-b), Math.abs(a-b) - (a % Math.abs(a-b)))); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Code written by Aditya ;) || Codechef/codeforces: @adityaraj5200 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ #include<bits/stdc++.h> using namespace std; //#include<ext/pb_ds/assoc_container.hpp> //#include<ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; //template<class T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update >; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define endl "\n" #define mod 1000000007 #define mod2 998244353 #define PI 3.14159265358979323846 #define deb(u) cout<<'>'<<#u<<" = "<<u<<endl #define deb2(u, v) cout<<'>'<<#u<<" = "<<u<<" , "<<#v<<" = "<<v<<endl #define all(u) u.begin(), u.end() #define rall(v) v.rbegin(), v.rend() #define sortall(u) sort(all(u)) #define lcm(a,b) (a*b)/__gcd(a,b) #define gcd(a,b) __gcd(a,b) #define inclusive(a,b) (abs(b-a)+1) #define gap(a,b) max(0,(abs(b-a)-1)) #define summation(n) (((n)*(n+1))/2) #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define mp make_pair #define lb(u,val) lower_bound(all(u),val) #define ub(u,val) upper_bound(all(u),val) #define yes "YES" #define no "NO" #define chartoint(ch) ch-'0' #define digits(n) (1+floor(log10(n))) #define presum(p,a,n) int (p)[(n)];p[0]=a[0];for(int i=1;i<(n);i++)p[i]=a[i]+p[i-1]; #define cube(u) (u)*(u)*(u) #define sq(u) (u)*(u) #define fill0(a) memset(a,0,sizeof(a)) #define fillneg1(a) memset(a,-1,sizeof(a)) #define fillbig(a) memset(a,63,sizeof(a)) #define setbits(u) __builtin_popcount(u) #define ctz(u) __builtin_ctz(u) #define clz(u) __builtin_clz(u) #define checkbit(num,i) (num&(1<<i)) //select the bit of position i of val #define lowbit(u) ((u)&((u)^((u)-1))) //get the lowest bit of u #define trav(u,it) for(auto it = u.begin(); it != u.end(); it++) #define present(u,key) u.find(key) != u.end() #define notpresent(u,key) u.find(key) == u.end() #define in(u, a, b) (a <= u && u <= b) #define print(u) for(auto it=u.begin();it!=u.end();it++)\ cout<<*it<<' '; cout<<endl #define printii(u) for(auto it=u.begin();it!=u.end();it++)\ cout<<it->first<<' '<<it->second<<endl typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef long double lld; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<long long> vll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; ll mod_add(ll a, ll b, ll m) {return (a%m + b%m)%m;} ll mod_mul(ll a, ll b, ll m) {return (a%m * b%m)%m;} ll mod_sub(ll a, ll b, ll m) {return (a%m - b%m)%m;} // all 4 & 8- direction move int dx4[4]={1,0,-1,0}; int dy4[4]={0,1,0,-1}; int dx8[8]={1,1,1,0,0,-1,-1,-1}; int dy8[8]={0,1,-1,1,-1,0,1,-1}; /*/------------------------------ CODE BEGINS ------------------------------/*/ void solve(){ ll a,b; cin>>a>>b; if(a>b) swap(a,b); ll maxgcd = b-a; if(a==b) cout<<"0 0"; else cout<<maxgcd<<' '<<min(a%maxgcd, maxgcd-(a%maxgcd)); cout<<endl; } /*/------------------------------- CODE ENDS -------------------------------/*/ int main(){ fastio; // cout << setprecision(12) << fixed; int tc=1; cin>>tc; //precompute(); for(int t=1;t<=tc;t++){ // cout<<"Case #" << t << ": "; solve(); } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import sys input = sys.stdin.readline for _ in range(int(input())): a, b = map(int,input().split()) if a == b: print(0,0) continue a, b = sorted([a, b]) k = b-a print(k, min(b%k, k-b%k,a))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.awt.image.AreaAveragingScaleFilter; import java.io.*; import java.util.*; import java.lang.*; public class Main{ public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static long MOD = (long) (1e9 + 7); // static long MOD = 998244353; static long MOD2 = MOD * MOD; static FastReader sc = new FastReader(); static int pInf = Integer.MAX_VALUE; static int nInf = Integer.MIN_VALUE; static long ded = (long)(1e17)+9; public static void main(String[] args) throws Exception { int test = 1; test = sc.nextInt(); for (int i = 1; i <= test; i++){ // out.print("Case #"+i+": "); solve(); } out.flush(); out.close(); } static void solve(){ long a = sc.nextLong(); long b = sc.nextLong(); if(a==b){ out.println(0+" "+0); return; } long diff = a%Math.abs(a-b); long min = Math.min(diff,Math.abs(a-b)-diff) ; out.println(Math.abs(a-b)+" "+min); } static class Pair implements Comparable<Pair> { long x; long y; public Pair(long x, long y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o){ if(this.y==o.y){ return -1*Long.compare(this.x,o.x); } return Long.compare(this.y,o.y); } @Override public String toString() { // return "Pair{" + "x=" + x + ", y=" + y + '}'; return x+" "+y; } public boolean equals(Pair o){ return this.x==o.x&&this.y==o.y; } } public static long mul(long a, long b) { return ((a % MOD) * (b % MOD)) % MOD; } public static long add(long a, long b) { return ((a % MOD) + (b % MOD)) % MOD; } public static long c2(long n) { if ((n & 1) == 0) { return mul(n / 2, n - 1); } else { return mul(n, (n - 1) / 2); } } //Shuffle Sort static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n); int temp= a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } //Brian Kernighans Algorithm static long countSetBits(long n) { if (n == 0) return 0; return 1 + countSetBits(n & (n - 1)); } //Euclidean Algorithm static long gcd(long A, long B) { if (B == 0) return A; return gcd(B, A % B); } //Modular Exponentiation static long fastExpo(long x, long n) { if (n == 0) return 1; if ((n & 1) == 0) return fastExpo((x * x) % MOD, n / 2) % MOD; return ((x % MOD) * fastExpo((x * x) % MOD, (n - 1) / 2)) % MOD; } //AKS Algorithm static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i <= Math.sqrt(n); i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static long modinv(long x) { return modpow(x, MOD - 2); } public static long modpow(long a, long b) { if (b == 0) { return 1; } long x = modpow(a, b / 2); x = (x * x) % MOD; if (b % 2 == 1) { return (x * a) % MOD; } return x; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StringReader; import java.lang.reflect.Array; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int gcd(int no, int sum) { if (sum == 0) return no; return gcd(sum, no % sum); } static int lcm(int a , int b){ return (a / gcd(a , b))*b; } static void countSort(int[] arr) { int max = Arrays.stream(arr).max().getAsInt(); int min = Arrays.stream(arr).min().getAsInt(); int range = max - min + 1; int count[] = new int[range]; int output[] = new int[arr.length]; for (int i = 0; i < arr.length; i++) { count[arr[i] - min]++; } for (int i = 1; i < count.length; i++) { count[i] += count[i - 1]; } for (int i = arr.length - 1; i >= 0; i--) { output[count[arr[i] - min] - 1] = arr[i]; count[arr[i] - min]--; } for (int i = 0; i < arr.length; i++) { arr[i] = output[i]; } } static void RandomShuffleSort(int [] a , int start , int end){ Random random = new Random(); for(int i = start; i<end; i++){ int j = random.nextInt(end); int temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a , start , end); } public static void main(String[] args) throws IOException , ParseException{ BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0){ long a = sc.nextLong(); long b = sc.nextLong(); long g = Math.abs(a - b); if(a == b) { System.out.println("0 0"); } else System.out.println(g + " " +Math.min(b%g, (g - b%g))); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define ll long long int #define endl "\n" #define pb push_back #define mp make_pair #define ut unsigned int #define ss second #define ff first #define vi vector<int> #define vl vector<ll> #define lb lower_bound #define up upper_bound #define re return #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define sp(x) fixed << setprecision(x) ll inline power(ll a, ll b, ll p){ a %= p; ll ans = 1; while(b>0){ if(b & 1) ans = (ans*a)%p; a = (a*a)%p; b >>= 1; } return ans; } ll inv(ll n, ll p){ return power(n,p-2, p); } bool inline isprime(ll n) { if (n <= 1) return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false; for (ll i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } /*ll dp[1000001]; //memset(dp,0,sizeof(dp)); void seive() { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. memset(dp,0,sizeof(dp)); ll n=1000000,i; bool prime[n+1]; memset(prime, true, sizeof(prime)); for (ll p=2; p*p<=n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for ( ll i=p*p; i<=n; i += p) prime[i] = false; } } dp[1]=1; for (int p=2; p<=n; p++){ if (prime[p]==true) dp[p]=1+dp[p-1]; else dp[p]=dp[p-1]; } } bool issorted(int a[],int n){ int i=0,cnt=0; for(i=1;i<n;i++){ if(a[i]>=a[i-1]) cnt++; else return false; } return true; } // ll dp[1000001],powe[1000001]; int binary-search(int a[],int n,int target){ int low,high,mid; low=0; high=n-1; while(high-low>1){ mid=(low+high)/2; if(a[mid]==target){ return mid; } if(a[mid]>target) high=mid-1; else low=mid+1; } if(a[low]==target) return low; if(a[high]==target) return high; return 0; } */ void solve(){ ll a,b,ans=0; cin>>a>>b; if(a==b){ cout<<"0"<<" "<<"0"<<endl; re; } ans=abs(a-b); if(ans==1){ cout<<"1"<<" "<<"0"<<endl; re; } ll r=min(a,b),k=0,m; m=min(r%ans,ans-r%ans); cout<<ans<<" "<<m<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1 ; cin >> t; while(t--) { solve(); } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> #define cs(n) scanf("%d", &n) #define lcs(n) scanf("%lld", &n) #define F(i, j, k) for(int i = j; i <= k; ++i) using namespace std; using ll = long long; const int MAXN = 1000004; const int INF = 0x3f3f3f3f; int main() { int T; cs(T); while(T--) { ll a; lcs(a); ll b; lcs(b); if(a == b) { puts("0 0"); continue; } ll d = abs(a - b); if(a > b) swap(a, b); printf("%lld %lld\n", d, min(a % d, d - a % d)); } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#------------------------template--------------------------# import os import sys import math import collections import functools import itertools # from fractions import * import heapq import bisect from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcde' MOD = 10**9 + 7 EPS = 1e-6 def Ceil(a,b): return a//b+int(a%b>0) def INT():return int(input()) def STR():return input() def INTs():return tuple(map(int,input().split())) def ARRINT():return [int(i) for i in input().split()] def ARRSTR():return [i for i in input().split()] #-------------------------code---------------------------# for _ in range(INT()): a, b = INTs() if a == b: print('0 0') else: diff = abs(a - b) step = min(a%diff, diff - a%diff) print(str(diff) + ' ' + str(step))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
from __future__ import division, print_function import os import sys from io import BytesIO, IOBase def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") t = int(input().rstrip()) for _ in range(t): a, b = list(map(int, input().rstrip().split())) if a == b: print(0, 0) else: t1 = abs(b-a) t2 = b % t1 print(t1, min(t2, t1-t2))
PYTHON
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define fi first #define se second #define int long long typedef long long ll; typedef pair<ll, ll> pii; const int N = 2e5 + 10, MOD = 1e9 + 7; int n, m, t, ans, tmp, k1, k2, ans1, ans2; int a[N], b[N]; string s; void read_input() { cin >> n >> m; tmp = max(n, m) - min(n, m); cout << tmp << ' '; if (tmp == 0) { cout << 0 << '\n'; return; } k1 = n % tmp; k2 = tmp - k1; cout << min(k1, k2) << '\n'; } int32_t main () { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while(t--) { read_input(); } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
t = int(input()) for _ in range(t): a,b = map(int,input().split()) if a == b: print(0,0) else: ans = abs(a-b) moves = min(b%ans,((-b)%ans + ans)%ans) print(ans,moves)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import math t = int(input()) for i in range(t): a, b = map(int,input().split()) a, b = max(a, b), min(a, b) if a == b: print("0 0") elif b == 0: print(a, "0") elif b <= a/2: print(a-b, min(b, a-b*2)) else: if a%(a-b) == 0: print(a-b, 0) else: print(a-b, min(b%(a-b), (a-b)-b%(a-b)))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class R730_A { public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // public static Scanner br = new Scanner(System.in); public static void main(String[] args) { final int cases; try { cases = Integer.parseInt(br.readLine().trim()); for (int i = 0; i < cases; i++) { solve(); } } catch (Exception e) { e.printStackTrace(); } } public static void solve() throws IOException { String input[] = br.readLine().split(" "); long a = Long.parseLong(input[0]); long b = Long.parseLong(input[1]); long fix = Math.abs(a-b); long mini = Math.min(a, b); // System.out.print("ans:"); if(fix == 0) { System.out.print(fix+" 0\n"); } else { long q = mini/fix; System.out.print(fix+ " " + Math.min(Math.abs(q*fix - mini), Math.abs((q+1)*fix - mini)) + "\n"); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.Scanner; import java.lang.Math; public class ExcitingBets{ public static void main(String [] args){ long testCase, a, b, min, a1, c1, a2, c2; long maxEx, minMv; Scanner hero = new Scanner(System.in); //testCase Implimantation testCase = hero.nextLong(); // for testCase for(int start = 0; start < testCase; start++){ maxEx = 0; minMv = 0; a = hero.nextLong(); b = hero.nextLong(); if(a > b){ min = b; }else{ min = a; } //logic if(a == b){ maxEx = 0; minMv = 0; }else{ maxEx = Math.abs(b - a); a1 = min / maxEx; c1 = min - (a1 * maxEx); a2 = (min / maxEx) + 1; c2 = Math.abs(min - (a2 * maxEx)); if(c1 < c2){ minMv = c1; }else{ minMv = c2; } } System.out.println(maxEx + " " + minMv); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve() { ll a, b; cin>>a>>b; if(a == b){ cout<<0<<" "<<0<<"\n"; } else { ll k = max(a, b)-min(a, b); cout<<k<<" "<<min(k-(b%k), b%k)<<"\n"; } } int main() { int t; cin >> t; while (t--) { solve(); } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { int t; cin>>t; while(t--) { int a,b; cin>>a>>b; if(a==b) { cout<<"0 0"<<endl; } else { if(a<b) swap(a,b); int g=a-b; cout<<g<<" "; /*if(a%g==0) cout<<0<<endl; else*/ { //cout<<(a/g+1)*g-a<<endl; cout<<min(a-a/g*g,(a/g+1)*g-a)<<endl; } } } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.Scanner; /** * A_Exciting_Bets */ public class A_Exciting_Bets { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); for (int f = 0; f < t; f++) { long a = s.nextLong(); long b = s.nextLong(); if (a == b) { System.out.println(0 + " " + 0); } else { long diff = Math.abs(a - b); long rem = a % diff; long num = Math.min(diff - rem, rem); System.out.println(diff + " " + num); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); for(int m=0;m<t;m++){ long a=scn.nextLong(); long b=scn.nextLong(); long diff=Math.abs(a-b); int count=0; if(diff==0){ System.out.println("0 0"); }else if(diff==1){ System.out.println("1 0"); }else{ long temp1=diff-(a%diff); long temp2=a%diff; System.out.println(diff+" "+Math.min(temp1,temp2)); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class Template { static int mod = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); int yo = sc.nextInt(); while (yo-- > 0) { long a = sc.nextLong(); long b = sc.nextLong(); if(a == b) { System.out.println("0 0"); continue; } long max = abs(b-a); if(a < b) { long min = min(a%max,max-a%max); System.out.println(max + " " + min); } else { long min = min(b%max,max-b%max); System.out.println(max + " " + min); } } } public static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for (int x : arr) ls.add(x); Collections.sort(ls); for (int i = 0; i < arr.length; i++) arr[i] = ls.get(i); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int N) { boolean[] sieve = new boolean[N + 1]; for (int i = 2; i <= N; i++) sieve[i] = true; for (int i = 2; i <= N; i++) { if (sieve[i]) { for (int j = 2 * i; j <= N; j += i) { sieve[j] = false; } } } return sieve; } public static long power(long x, long y, long p) { long res = 1L; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; } public static void print(int[] arr) { //for debugging only for (int x : arr) out.print(x + " "); out.println(); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] readInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] readDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
# import sys,os,io # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline PI = 3.141592653589793238460 INF = float('inf') MOD = 1000000007 # MOD = 998244353 def bin32(num): return '{0:032b}'.format(num) def add(x,y): return (x+y)%MOD def sub(x,y): return (x-y+MOD)%MOD def mul(x,y): return (x*y)%MOD def gcd(x,y): if y == 0: return x return gcd(y,x%y) def lcm(x,y): return (x*y)//gcd(x,y) def power(x,y): res = 1 x%=MOD while y!=0: if y&1 : res = mul(res,x) y>>=1 x = mul(x,x) return res def mod_inv(n): return power(n,MOD-2) def prob(p,q): return mul(p,power(q,MOD-2)) def ii(): return int(input()) def li(): return [int(i) for i in input().split()] def ls(): return [i for i in input().split()] for t in range(ii()): t+=1 l = li() l.sort() a = l[0] b = l[1] if a == b: print(0,0) continue diff = b - a re = a % diff print(diff , min(diff - re , re))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class codeforces { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { solve(); } static FastReader sc = new FastReader(); static void solve() { long n = sc.nextLong(); while (n-- > 0) { long a = sc.nextLong(), b = sc.nextLong(); long excitement = Math.abs(a - b); if (excitement == 0) { System.out.println(excitement + " " + 0); continue; } long diff = a % excitement; if (diff > excitement / 2) diff = excitement - diff; System.out.println(excitement + " " + diff); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class b729 { public static void main(String[] args) { //System.out.println(1); FastReader sc = new FastReader(); int t = sc.nextInt(); for(int o = 0 ; o<t;o++) { long a = sc.nextLong(); long b = sc.nextLong(); long x = Math.abs(a-b); if(x == 0) { System.out.println(0 + " " + 0); continue; } System.out.print(x + " "); // System.out.print(Math.min(Math.abs(x-a), Math.abs(x-b))); if(a%x==0) { System.out.print(0); }else { long v = a %x; System.out.println(Math.min(v, x-v)); } System.out.println(); } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define mset(x,y) memset(x,y,sizeof(x)) using namespace std; void DBG(){ cerr<<")\n";} template<class H, class... T > void DBG( H h, T... t){cerr << h;if( sizeof...(t))cerr<<", ";DBG(t...);} #define dbg(...) cerr <<" values[ "<< #__VA_ARGS__ << " ] = ( ", DBG(__VA_ARGS__) template <class T > void dbgv(vector < T > &x){cerr<<"[ ";for( auto a: x) cerr<<a<<","[a==x.back()]<<" "; cerr<<"]\n"; } typedef long double ld; typedef long long int ll; typedef unsigned long long int ul; constexpr int MAXN = 1e6+2; constexpr int inf = 2e9; ///aqui puede ir algo int main() { ios::sync_with_stdio(false); cin.tie(NULL); /*freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);*/ int t; cin>>t; while(t--) { ul a,b; cin>>a>>b; if( a == b ) cout<<"0 0"; else { ul maximo = max(a,b) - min(a,b); /// la diferencia es el max gcd /// buscar que ambos sean multiplos de la diff /// respuesta minima if( a % maximo == 0 && b % maximo == 0 ) cout<<maximo<<" "<<0; else { ul aux = min(a,b); ul aux2 = max( a,b); b = aux; a = aux2; // dbg( b % maximo, maximo); if( b < maximo) { if( b < ( maximo - b)) cout<<maximo<<" "<<b; else cout<<maximo<<" "<<maximo - b; } else { ul divi = ( b / maximo); ul atras = (divi * maximo); ul enfrente = ( (divi + 1) * maximo); if( (enfrente - b ) < ( b - atras)) cout<<maximo<<" "<< (enfrente - b); else cout<<maximo<<" "<< b - atras; } } } cout<<"\n"; } cout<<"\n"; ///uwu - vrm } /* [°-°] <- tss [./../] <- este mensaje puede cambiar by Benqi COSAS QUE DEBERIAS BUSCAR * desbordamientos de int, rango de los arreglos * casos especiales ( n = 1? ) * haz algo en lugar de nada, mantente organizado * ESCRIBE COSAS E IDEAS ABAJO * NO TE CASES CON UNA IDEA O ENFOQUE */
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main () { ll tt; cin >> tt; while (tt --) { ll a, b, one, two; cin >> a >> b; if (a == b) { cout << "0 0" << '\n'; continue; } one = abs (a - b); two = min (a % one, one - a % one); cout << one << ' ' << two << '\n'; } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
def main(): A, B = map(int, input().split()) if A == B: return print(0, 0) C = abs(A - B) A %= C print(C, min(A, C - A)) if __name__ == '__main__': T = int(input()) for _ in range(T): main()
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
t = int(input()) for _ in range(t): a, b = map(int, input().split()) if a == b: print(0, 0) else: x = max(a, b) y = min(a, b) print(x - y, min(y % (x - y), x-y - y%(x-y)))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): a, b = [int(x) for x in input().split()] d = abs(a - b) if d == 0: print(0, 0) continue minus_count = a % d plus_count = d - minus_count if min(a, b) - minus_count < 0: print(d, plus_count) continue if minus_count < plus_count: print(d, minus_count) else: print(d, plus_count)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
n = int(input()) for m in range(n): [a, b] = list(map(int, input().split())) if a>b: k = a-b elif a<b: k = b-a else: print("0 0") continue if a%k>k/2: x = k - (a%k) else: x = a%k print(str(k)+" "+str(x))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.*; public class codeforces1 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t; t = sc.nextInt(); while(t>0) { long a, b; a = sc.nextLong(); b = sc.nextLong(); if(a==b) System.out.println(0 + " " + 0); else { long g = Math.abs(a-b); long m = Math.min(a%g, g-a%g); System.out.println(g+" "+m); } t--; } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
// package com.prituladima; import java.io.*; import java.util.StringTokenizer; /** * Provide prove of correctness before implementation. Implementation can cost a lot of time. * Anti test that prove that it's wrong. * <p> * Do not confuse i j k g indexes. Do extra methods. * <p> * Will program ever exceed limit? * Try all approaches with prove of correctness if task is not obvious. * Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute force/Symmetric data * Number theory * Game theory (optimal play) that consider local and global strategy. */ public class A { private void solve_() { long a = nextLong(); long b = nextLong(); if (a == b) { println("0 0"); } else if (b == 0) { println(a + " 0"); } else if (a == 0) { println(b + " 0"); } else { println(ans(a, b)); } } String ans(long a, long b){ if(a < b) { return ans(b, a); } long diff = a - b; long upper = divF(a, diff) * diff; long lower = div(a, diff) * diff; long min = Math.min(upper - a, a - lower); return diff + " " + min; } long div(long a, long b) { return a / b; } long divF(long a, long b) { return (a + b - 1) / b; } private void solve() { int t = nextInt(); for (int i = 0; i < t; i++) { solve_(); } } public static void main(String[] args) { new A().run(); } private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter writer; private void run() { try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out))) { this.reader = reader; this.writer = writer; solve(); } catch (Exception e) { throw new RuntimeException(e); } } private int nextInt(int radix) { return Integer.parseInt(nextToken(), radix); } private int nextInt() { return Integer.parseInt(nextToken()); } private long nextLong(int radix) { return Long.parseLong(nextToken(), radix); } private long nextLong() { return Long.parseLong(nextToken()); } private double nextDouble() { return Double.parseDouble(nextToken()); } private int[] nextIntArr(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = nextInt(); } return arr; } private long[] nextLongArr(int size) { long[] arr = new long[size]; for (int i = 0; i < size; i++) { arr[i] = nextLong(); } return arr; } private double[] nextDoubleArr(int size) { double[] arr = new double[size]; for (int i = 0; i < size; i++) { arr[i] = nextDouble(); } return arr; } private String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private void printf(String format, Object... args) { writer.printf(format, args); } private void print(Object o) { writer.print(o); } private void println() { writer.println(); } private void println(Object o) { writer.println(o); } private void flush() { writer.flush(); } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Problem { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); }catch (IOException e){ e.printStackTrace(); } return str; } } public static void main(String[] args) throws Exception{ long mod = (long)(1e9) + 7; BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512); FastReader in = new FastReader(); int t = in.nextInt(); while(t-- != 0){ // long n = in.nextLong(); // String s = in.next(); long a = in.nextLong(), b = in.nextLong(); if(a == b){ out.write("0 0\n"); }else if(Math.abs(a-b) == 1){ out.write("1 0\n"); }else{ long d = Math.abs(a - b); long r1 = a%d; long r2 = d - (a % d); out.write(d + " " + Math.min(r1, r2) + "\n"); } } out.flush(); } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> using namespace std; typedef long long int ll; void s() { ll a,b; cin>>a>>b; if(a==b) { cout<<"0"<<" "<<"0"; return; } ll dif = abs(a-b); b=min(a,b); if(b%dif==0)cout<<dif<<" "<<"0"; else { ll y = min(b%dif,(dif-b%dif)); cout<<dif<<" "<<y; } } int main() { int t; cin>>t; while(t) { t-=1; s(); cout<<endl; } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <iostream> #include<math.h> using namespace std; int main() { long long int t,x,y; cin>>t; for(int i=0;i<t;++i) { cin>>x>>y; if(x==y) cout<<0<<" "<<0<<endl; else if(abs(x-y)==1) cout<<1<<" "<<0<<endl; else { if(x<y) swap(x,y); long long int dif=x-y; long long int index1,index2; index1=y%dif; index2=dif-(y%dif); if(index1<index2) cout<<dif<<" "<<index1<<endl; else cout<<dif<<" "<<index2<<endl; } } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.Scanner; import java.math.BigInteger; import java.util.*; public class Test2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t; t=in.nextInt(); for (int i=0;i<t;i++){ long a,b; a=in.nextLong(); b=in.nextLong(); if (a==0){ System.out.println(b+" 0"); continue; } else if (b==0){ System.out.println(a+" 0"); continue; } long sub=Math.abs(a-b); if (sub==0){ System.out.println("0 0"); continue; } else { long c=a,d=b; long x,y; long min=Math.min(a,b); long max=Math.max(a,b); long div=max/sub; x=div*sub; y=(div+1)*sub; x=max-x; y=y-max; min=Math.min(x, y); System.out.println(sub+" "+min); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define en "\n"; #define all(x) (x).begin(), (x).end() ll mod = 1000000007; //ull n = 1e8; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin >> t; //cout<<"as.size()"<<en; while (t--) { ll n,m; cin>>n>>m; ll d = abs(n-m); if (n==m) { cout<<"0"<<" 0"<<en; continue; } ll k= n/d; if (n%d!=0) { k++; } // if (n==1) // { k--; // ll p = abs(d*k-n); // cout<<d<<" "<<"0"<<en; // continue; // } ll p = abs(d*k-n); if (p>d/2) { p = d-p; } //cout<<p<<"."<<en; cout<<d<<" "<<p<<en; } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import sys import math #sys.stdin=open('input.txt','r') #sys.stdout=open('output.txt','w') def solve(): #n=int(input()) a,b=map(int,input().split()) if(a==b): print("0 0") else: r=abs(a-b) z=max(a,b) if(math.gcd(a,b)==r): print(r,"0") else: z=max(a,b) e=z//r y1=((e+1)*r)-z y2=abs(((e)*r)-z) print(abs(a-b),min(y1,y2)) t=int(input()) while(t!=0): solve() t-=1
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.*; import java.lang.*; import java.io.*; // Created by @thesupremeone on 07/07/21 public class A { void solve() throws IOException { int ts = getInt(); for (int t = 1; t <= ts; t++){ long a = getLong(); long b = getLong(); long max = Math.max(a, b); long min = Math.min(a, b); long diff = max-min; if(diff==0){ println(0+" "+0); continue; } if(min==0){ println(max+" "+0); continue; } long steps = Math.min(min%diff,diff-(min%diff)); println(diff+" "+steps); } } public static void main(String[] args) throws Exception { if (isOnlineJudge()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); new A().solve(); out.flush(); } else { localJudge = new Thread(); in = new BufferedReader(new FileReader("input.txt")); out = new BufferedWriter(new FileWriter("output.txt")); localJudge.start(); new A().solve(); out.flush(); localJudge.suspend(); } } static boolean isOnlineJudge(){ try { return System.getProperty("ONLINE_JUDGE")!=null || System.getProperty("LOCAL")==null; }catch (Exception e){ return true; } } // Fast Input & Output static Thread localJudge = null; static BufferedReader in; static StringTokenizer st; static BufferedWriter out; static String getLine() throws IOException{ return in.readLine(); } static String getToken() throws IOException{ if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(getLine()); return st.nextToken(); } static int getInt() throws IOException { return Integer.parseInt(getToken()); } static long getLong() throws IOException { return Long.parseLong(getToken()); } static void print(Object s) throws IOException{ out.write(String.valueOf(s)); } static void println(Object s) throws IOException{ out.write(String.valueOf(s)); out.newLine(); } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
""" Author : Ashish Sasmal Python3 / PyPy3 """ from sys import stdin as sin def aint():return int(input()) def amap():return map(int,sin.readline().split()) def alist():return list(map(int,sin.readline().split())) def astr():return input() from math import gcd,ceil,floor for _ in range(aint()): a,b = amap() k = abs(a-b) if k==0: print(0,0) else: if a<b: x = abs(k-a) w = abs(b-int(floor(b/k)*k)) z = abs(b-int(ceil(b/k)*k)) print(k, min(x,w,z)) else: x = abs(k-b) w = abs(a-int(floor(a/k)*k)) z = abs(a-int(ceil(a/k)*k)) print(k, min(x,w,z))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; using lint = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tc; cin >> tc; while (tc--) { lint a, b; cin >> a >> b; if (a < b) swap(a, b); if (a == b) cout << 0 << " " << 0 << '\n'; else { cout << (a - b) << " " << min(a % (a - b), (a - b) - (a % (a - b))) << '\n'; } } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
def s_a(): t = int(input()) arr = [] for i in range(t): a, b = tuple([int(x) for x in input().split()]) a1, b1 =(a, b) d = abs(a - b) if (d == 0): arr.append("0 0") elif (d == 1): arr.append("1 0") else: i = 0 m = a % d if(m <= d//2): i = m else: i = d - m arr.append("{} {}".format(d,i)) for el in arr: print(el) s_a()
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
t=int(input()) while t>0: t=t-1 a,b=map(int,input().split()) if a==b: print("0 0") elif a<b: temp=int(b-a) ans=min(a%temp,temp-a%temp) print(temp,ans) else: temp=int(a-b) ans=min(b%temp,temp-b%temp) print(temp,ans)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main() { int t; scanf("%d",&t); while(t--) { long long a,b,c,d,ans; scanf("%lld%lld",&a,&b); if(a==b) printf("0 0\n"); else { c=abs(a-b); d=a%c; if(d<=c-d) ans=d; else ans=c-d; printf("%lld %lld\n",c,ans); } } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 * 100 + 10; int main(){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); int t; cin >> t; while (t--){ long long a, b; cin >> a >> b; long long r = abs(a - b); if (!r){ cout << 0 << ' ' << 0 << '\n'; continue; } cout << r << ' ' << min((a % r), (r - (a % r))) << '\n'; } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
for a0 in range(int(input())): a,b=[int(x) for x in input().split()] v=abs(a-b) if v==0: print(0,0) else: if a>b: m=b else: m=a v1=m%v if v1>v-v1: v1=v-v1 print(v,v1)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
//package goldman1; import java.util.*; public class ExcitingBets { public static void main(String[] args) { Scanner in=new Scanner(System.in); int t=in.nextInt(); for(int i=0;i<t;i++){ long a=in.nextLong(); long b=in.nextLong(); if(a==b){ System.out.println("0 0"); }else if(a==0){ System.out.println(b+" 0"); }else if(b==0){ System.out.println(a+" 0"); }else{ long diff=Math.abs(a-b); long div=a/diff; long small=diff*div; long big=diff*(div+1); long steps=Math.min(big-a, a-small); System.out.println(Math.abs(a-b)+" "+steps); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
for _ in range(int(input())): a,b=map(int,input().split()) x=min(a,b) y=max(a,b) if x==y: print("0 0") else: print(y-x, end=" ") b=y-x a=x%b print(min(a, b-a))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import os, sys from io import BytesIO, IOBase from types import GeneratorType from bisect import * from collections import defaultdict, deque, Counter import math, string from heapq import * from operator import add from itertools import accumulate BUFSIZE = 8192 sys.setrecursionlimit(10 ** 5) class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") inf = float("inf") en = lambda x: list(enumerate(x)) ceil_ = lambda a, b: (a + b - 1) // b ii = lambda: int(input()) r = lambda: map(int, input().split()) rr = lambda: list(r()) # -------------------------- def solve(): a, b = r() if a == b: print(0, 0) return x = abs(a - b) a = a % x print(x, min(a, x - a)) for _ in " " * ii(): solve()
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
from math import ceil for i in range(int(input())): a,b=map(int,input().split()) if a==b: print(0,0) else: mgcd=max(a,b)-min(a,b) temp=min(a,b) x=ceil(temp/mgcd) z=temp//mgcd y=(x*mgcd)-temp y2=temp-(z*mgcd) ans=min(temp,y,y2) #print(temp,mgcd,x,y) print(mgcd,ans)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
//package codeforces; import java.io.*; import java.util.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws java.lang.Exception { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fr.ni(); while(t-->0) { long a = fr.nl(); long b = fr.nl(); long diff = Math.abs(a-b); if(diff == 0) { out.println(0 + " " + 0); continue; } long temp = a%diff; long temp2 = diff - temp; out.println(diff + " " + Math.min(temp2, temp)); } out.close(); } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
def main(): t = int(input()) # Número de casos for _ in range(t): a, b = map(int, input().split()) # Apuestas de los corredores gcd = abs(a-b) if gcd == 0: print("0 0") else: print(gcd, min(a%gcd, gcd-a%gcd)) if __name__ == "__main__": main()
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.*; public class ExcitingBets { public static void main(String args[]) { Scanner sc=new Scanner(System.in); ExcitingBets it=new ExcitingBets(); int t=sc.nextInt(); while(t--!=0) { long a=sc.nextLong(); long b=sc.nextLong(); long max=Math.abs(a-b); if(a==b) { System.out.println("0 0"); } else { long c=Math.min(a%max,max-a%max); System.out.println(max+" "+c); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
def excitement(a,b): ans=abs(a-b) diff1=diff2=0 if ans==0: return 0,0 if a%ans!=0: diff1=min((ans-(a%ans)),a%ans) if b%ans!=0: diff2=min((ans-(b%ans)),b%ans) diff=min(diff1,diff2) return ans,diff if __name__ == '__main__': t=int(input()) for _ in range(t): a,b=map(int,input().split()) max_gcd,diff=excitement(a,b) print(max_gcd,diff)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#pragma GCC target ("avx2,fma") #pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops") #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int IOS = []() {ios::sync_with_stdio(0); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }(); #define pb push_back #define po pop_back #define YES cout<<"YES\n" #define Yes cout<<"Yes\n" #define NO cout<<"NO\n" #define No cout<<"No\n" #define for00 for(int i=1;i<=n;i++) cin>>a[i]; #define for0 for(int i=0;i<n;i++) #define for1 for(int i=1;i<=n;i++) #define for2(i,a,b) for(int i=a; i<=b;i++) #define for3(i,a,b) for(int i=a; i>=b;i--) #define cn change_number_tostring #define cs change_string_tonumber #define pri init_prime_distance #define all(a) begin(a), end(a) #define SUM(a) accumulate(all(a), 0LL) #define MIN(a) (*min_element(all(a))) #define MAX(a) (*max_element(all(a))) #define lb(a, x) distance(begin(a), lower_bound(all(a), (x))) #define ub(a, x) distance(begin(a), upper_bound(all(a), (x))) #define gcd __gcd template<class T> void _W(const T &x) { cout << x; } template<class T> void _W(T &x) { cout << x; } template<class T,class U> void _W(const pair<T,U> &x) {_W(x.first); putchar(' '); _W(x.second);} template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) cout<<' '; } void W() {} template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); cout<<", "; W(tail...); } #ifdef CF_DEBUG #define debug(...) { cout << "debug : [ "; cout << #__VA_ARGS__; cout << " ] = [ "; W(__VA_ARGS__); cout<<"\b\b ]\n"; } #else #define debug(...) (0) #endif const int mod = 1e9+7; const int sp = 1e9; const int ma = 1e5+10; //int f[ma]; //inline int find(int k) { return f[k]==k? k : f[k]=find(f[k]);} // f[find(j)] = find(k); inline int digit(int i) { stringstream ss; ss<<i; return ss.str().size(); } inline string change_number_tostring(int i) { stringstream ss; ss<<i; return ss.str(); } inline int change_string_tonumber(string s) { int num; stringstream ss(s); ss>>num; return num; } inline int quick(int a, int b) { a%=mod; int res = 1; while(b) { if(b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } inline int C(int n, int m) { int resdw1 = 1; for2(i, 1, n) resdw1 = resdw1 * i % mod; int resdw2 = 1; for2(i, 1, m - n) resdw2 = resdw2 * i % mod; int resup = 1; for2(i, 1, m) resup = resup * i % mod; return resup * quick(resdw1, mod - 2) % mod * quick(resdw2, mod - 2) % mod; } inline void read(int &x) { int f = 1; x = 0; char op = getchar(); while(op < '0' || op > '9') { if(op == '-') f = -1; op = getchar(); } while(op >= '0' && op <= '9') { x = x * 10 + op - '0'; op = getchar(); } x *= f; } inline void print(int x) { if(x < 0) putchar('-'), x = -x; if(x > 9) print(x / 10); putchar(x % 10 + '0'); } const int man=1e7+5; bool vis[man]={0}; int prime[man]; inline void init_prime_distance() {int cnt= 0; for (int i=2;i<=man;i++) { if (!vis[i]) prime[cnt++]=i; for(int j=0;j<cnt&&i*prime[j]<=man;j++) { vis[i*prime[j]] = 1; if (i%prime[j]== 0) break; } } } // double time=clock(); printf("%lf\n",clock()-time); const int N = 2e5+10; void solve() { int a,b; cin>>a>>b; if(a==b) cout<<0<<" "<<0<<endl; else{ if(a-b==1||a-b==-1) cout<<1<<" "<<0<<endl; else { int q,w; if(max(a,b)%abs(a-b) <= abs(a-b)/2) q=max(a,b)%abs(a-b); else q = abs(a-b)-max(a,b)%abs(a-b); cout<<abs(a-b)<<" "<<q<<endl; } } } signed main() { int tt=1; cin>>tt; while(tt--) { solve(); } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
from __future__ import division, print_function import bisect import math import heapq import itertools import sys from collections import deque from atexit import register from collections import Counter from functools import reduce sys.setrecursionlimit(100000) if sys.version_info[0] < 3: from io import BytesIO as stream else: from io import StringIO as stream if sys.version_info[0] < 3: class dict(dict): """dict() -> new empty dictionary""" def items(self): """D.items() -> a set-like object providing a view on D's items""" return dict.iteritems(self) def keys(self): """D.keys() -> a set-like object providing a view on D's keys""" return dict.iterkeys(self) def values(self): """D.values() -> an object providing a view on D's values""" return dict.itervalues(self) input = raw_input range = xrange filter = itertools.ifilter map = itertools.imap zip = itertools.izip def sync_with_stdio(sync=True): """Set whether the standard Python streams are allowed to buffer their I/O. Args: sync (bool, optional): The new synchronization setting. """ global input, flush if sync: flush = sys.stdout.flush else: sys.stdin = stream(sys.stdin.read()) def input(): return sys.stdin.readline().rstrip('\r\n') sys.stdout = stream() register(lambda: sys.__stdout__.write(sys.stdout.getvalue())) def dd(): return map(int, input().split()) def arr(): return list(map(int, input().split())) def twinSort(X, Y): # sorting X wrt Y return [x for _, x in sorted(zip(Y, X))] def solve(): a,b=dd() ans=abs(a-b) if ans==0: print(0,0) else: print(ans,min(a%ans,ans-(a%ans))) def main(): testCase = 1 if testCase: for _ in range(int(input())): solve() else: solve() if __name__ == '__main__': sync_with_stdio(False) main()
PYTHON
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--) { long long a,b; cin >> a >> b; if(a==b) cout << 0 << " " << 0 << '\n'; else { long long g = abs(a-b); long long m = min(a%g,g-a%g); cout << g << " " << m << '\n'; } } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.*; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.util.Collections; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.*; public class Main1 { public static void main(String[] args) throws IOException { // try { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt() ; while (t-- > 0){ long a = in.nextLong() ; long b = in.nextLong() ; if (a == b){ System.out.println("0 0"); continue; } long diff = Math.abs(a - b) ; System.out.println( (diff) + " " + Math.min( a%diff , diff - a%diff ) ); } out.flush(); out.close(); // } // catch (Exception e){ // return; // } } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static ArrayList<Integer> list = new ArrayList<>(); static boolean A[] = new boolean[2 * 90000001]; static void seive(int n) { int maxn = n; //int maxn = 1000000 ; A[0] = A[1] = true; for (int i = 2; i * i <= maxn; i++) { if (!A[i]) { for (int j = i * i; j <= maxn; j += i) A[j] = true; } } for (int i = 2; i <= maxn; i++) if (!A[i]) list.add(i); } static int findLCA(int a, int b, int par[][], int depth[]) { if (depth[a] > depth[b]) { a = a ^ b; b = a ^ b; a = a ^ b; } int diff = depth[b] - depth[a]; for (int i = 19; i >= 0; i--) { if ((diff & (1 << i)) > 0) { b = par[b][i]; } } if (a == b) return a; for (int i = 19; i >= 0; i--) { if (par[b][i] != par[a][i]) { b = par[b][i]; a = par[a][i]; } } return par[a][0]; } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void formArrayForBinaryLifting(int n, int par[][]) { for (int j = 1; j < 20; j++) { for (int i = 0; i < n; i++) { if (par[i][j - 1] == -1) continue; par[i][j] = par[par[i][j - 1]][j - 1]; } } } static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private final Writer os; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(long c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println() { return append(System.lineSeparator()); } public FastOutput flush() { try { // boolean success = false; // if (stringBuilderValueField != null) { // try { // char[] value = (char[]) stringBuilderValueField.get(cache); // os.write(value, 0, cache.length()); // success = true; // } catch (Exception e) { // } // } // if (!success) { os.append(cache); // } os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Exiting_bets { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static void main(String[] args) { FastReader sc = new FastReader(); long t=sc.nextLong(); while(t-->0) { long a=sc.nextLong(); long b=sc.nextLong(); long max=Math.abs(a-b); if(a==b) System.out.println(0+ " "+0); else { long x=a%max; long y=max-a%max; if(x<y) System.out.println(max+" "+x); else System.out.println(max+" "+y); } } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
t = int(input()) for _ in range(t): a,b = list(map(int,input().split())) if(a==b): print(0,0) else: max_exi = abs(a-b) min_mov = min(a,b)//max_exi c = abs(min_mov*max_exi - min(a,b)) d = abs((min_mov+1)*max_exi - min(a,b)) print(max_exi,min(c,d))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.*; import java.lang.*; import java.io.*; public final class Solution{ public static void main (String[] args) throws Exception { BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out)); Reader sc= new Reader(); int t=sc.nextInt(); while(t-->0){ long a=sc.nextLong(); long b=sc.nextLong(); long ans1=Math.abs(a-b); if(ans1==0){ op.write(0+" "+0); op.write("\n"); continue; } long one= ((a/ans1)+1)*ans1; // System.out.println(one); long ans2= Math.min((one-a), (a%ans1)); op.write(ans1+" "+ans2); op.write("\n"); } op.flush(); } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } // BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out));
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class A { static FastScanner sc = new FastScanner(); static long gcb(long a,long b){ if(b == 0) return a; return gcb(b,a % b); } static void solve(){ long a = sc.nextLong(); long b = sc.nextLong(); long ans = Math.abs(a - b); if(ans == 0){ System.out.println(0 + " " + 0); return; } long gcb = gcb(a, b); if(gcb == ans){ System.out.println(gcb + " " + 0); return; } long cur = Math.min(a,b); long move = ans - (cur % ans); if((cur % ans) < move) move = (cur % ans); System.out.println(ans + " " + move); } public static void main(String[] args) { int n = sc.nextInt(); for (int i = 0; i < n; i++) { solve(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> using namespace std; #define ll long long #define FAST1 ios_base::sync_with_stdio(false); #define FAST2 cin.tie(NULL); void solve() { ll a,b; cin>>a>>b; ll ans=abs(a-b); if(ans==0) { cout<<"0 0"; } else cout<<ans<<" "<<min(a%ans,ans-a%ans); } int main(){ FAST1; FAST2; int t; cin>>t; while(t--) { solve(); cout<<endl; } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import sys import math def read_ints(): inp = input().split() inp = [int(x) for x in inp] return inp def read_strings(): inp = input().split() s = [inp[i] for i in range(len(inp))] return s t = int(input()) for _ in range(t): ab = read_ints() a = ab[0] b = ab[1] maxe = 0 minm = 0 if a == b: maxe = 0 minm = 0 else: maxe = abs(a-b) minm = min(a % maxe, maxe - (a % maxe)) ans = str(maxe) + " " + str(minm) print(ans)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class CODECHEF { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception e) { e.printStackTrace(); } return str; } } static long MOD=1000000000+7; public static void main(String[] args) throws java.lang.Exception { FastReader fs=new FastReader(); // StringBuilder sb=new StringBuilder(); PrintWriter out=new PrintWriter(System.out); int t=fs.nextInt(); while(t-->0) { long a=fs.nextLong(); long b=fs.nextLong(); long max_excitement=Math.abs(a-b); long moves=0; if(max_excitement!=0){ moves=Math.min(a%max_excitement,max_excitement-a%max_excitement); } out.println(max_excitement+" "+moves); } out.close(); } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class CF1543AExcitingBets { static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter output = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int t = Integer.parseInt(input.readLine()); // while (t > 0) { // t -= 1; // String[] bets = input.readLine().split("\\s"); // int a = Integer.parseInt(bets[0]); // int b = Integer.parseInt(bets[1]); // int k = Math.abs(a - b); // if (k == 0) { // output.print("0 0\n"); // } else { // int tmp = a % k; // output.print(k + " " + Math.min(tmp, Math.abs(k - tmp)) + "\n"); // } // } // output.close(); while (t > 0) { t--; String[] bets = input.readLine().split("\\s"); long a = Long.parseLong(bets[0]); long b = Long.parseLong(bets[1]); long s = Math.abs(a - b); if (a == b) { output.print("0 0\n"); continue; } if (a == 0) { output.print(s + " 0\n"); continue; } if (s == 1) { output.print("1 0\n"); continue; } long s1 = a % s; if (s1 != 0) { long m = s1; long n = s - s1; s1 = Math.min(m, n); } output.print(s + " " + s1 + "\n"); } output.close(); } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> #define ll long long using namespace std; int main(){ ios::sync_with_stdio(false);cout.tie(0);cin.tie(0); int te; cin>>te; while(te--){ ll a,b; cin>>a>>b; if(a<b) swap(a,b); if(a==b){ cout<<"0 0\n"; }else{ ll t=a-b; cout<<t<<' '<<min(b%t,t-b%t)<<'\n'; } } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.Scanner; import java.lang.Math; public class Exciting_Bets { static long[] rock(long a, long b){ long out[] = new long[2]; if (a==b){ out[0] = 0; out[1] = 0; return out; } else if (Math.abs(a-b)==1){ out[0] = 1; out[1] = 0; return out; } else{ long kk = Math.abs(a-b); out[0] = Math.abs(a-b); if (a%kk==0 || b%kk==0){ out[1] = 0; } else{ long g = Math.max(a, b); long g2 = g/kk; long stf = kk*(g2 + 1) - g; long stb = g - kk*(g2); if (a-stb>=0 && b-stb>=0){ out[1] = Math.min(stb, stf); } else{ out[1] = stf; } } } return out; } public static void main(String[] args){ Scanner scn = new Scanner(System.in); int t = scn.nextInt(); scn.nextLine(); for (int i=0; i<t; i++){ long a = scn.nextLong(); long b = scn.nextLong(); scn.nextLine(); long[] out = rock(a, b); System.out.println(out[0] + " " + out[1]); } scn.close(); } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.util.*;import java.io.*;import java.math.*; public class CF1543A { static boolean memory = true; static long mod = 998244353; static long MOD = 1000000007; static void ruffleSort(int[] a) { int n = a.length; ArrayList<Integer> lst = new ArrayList<>(); for(int i : a) lst.add(i); Collections.sort(lst); for(int i = 0; i < n; i++) a[i] = lst.get(i); } static void ruffleSort(long[] a) { int n = a.length; ArrayList<Long> lst = new ArrayList<>(); for(long i : a) lst.add(i); Collections.sort(lst); for(int i = 0; i < n; i++) a[i] = lst.get(i); } public static void process()throws IOException { long a = nl(), b = nl(); if(a == b){ pn("0 0"); return; } long p = Math.abs(a-b); long q = Math.min(p - (a%p), a%p); pn(p+" "+q); } static AnotherReader sc; static PrintWriter out; public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new CF1543A().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 32).start(); else new CF1543A().run(); } void run()throws Exception { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} long s = System.currentTimeMillis(); int t=1; t=ni(); //int k = t; while(t-->0) {/*p("Case #"+ (k-t) + ": ")*/;process();} out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); out.close(); } static long power(long k, long c, long mod){ long y = 1; while(c > 0){ if(c%2 == 1) y = y * k % mod; c = c/2; k = k * k % mod; } return y; } static int power(int x, int y, int p){ int res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static int log2(int N) { int result = (int)(Math.log(N) / Math.log(2)); return result; } static void pn(Object o){out.println(o);} static void pn(int[] a){for(int i : a)out.print(i+" ");pn("");} static void pn(long[] a){for(long i : a)out.print(i+" ");pn("");} static void p(Object o){out.print(o);} static void YES(){out.println("YES");} static void NO(){out.println("NO");} static void yes(){out.println("yes");} static void no(){out.println("no");} static void p(int[] a){for(int i : a)out.print(i+" ");} static void p(long[] a){for(long i : a)out.print(i+" ");} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static String ns()throws IOException{return sc.next();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
aa=int(input()) for i in range(aa): temp=0 x,y=map(int,input().split()) if(x<y): temp=x x=y y=temp if(x==y): print('0 0') continue else: g_c_d= abs(x-y) z=x%g_c_d mini=min(z,g_c_d-z) print(g_c_d,mini)
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <stdio.h> int main() { int t; scanf("%d", &t); while(t--) { long long int a, b; scanf("%lld", &a); scanf("%lld", &b); if(a == 0) { printf("%lld 0\n", b); }else if(b==0) { printf("%lld 0\n", a); }else if(a == b) { printf("0 0\n"); } else{ long long int ans = a-b; if(ans < 0) ans *= -1; long long int ans1 = (ans - (a%ans)); long long int ans2 = (a%ans); if(a % ans == 0) printf("%lld 0\n", ans); else if(ans2 < ans1) printf("%lld %lld\n", ans, ans2); else printf("%lld %lld\n", ans, ans1); } } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; long long otv[t][2]; for (long long i=0; i<t; i++) { long long a,b; cin >> a >> b; if (a==b) { otv[i][0]=0; otv[i][1]=0; } else { if (a>b) { long long v=a; a=b; b=v; } long long ras=b-a; if (ras==1) { otv[i][0]=1; otv[i][1]=0; } else { long long res=min(a%ras,ras-(a%ras)); otv[i][0]=ras; otv[i][1]=res; } } } for (int i=0; i<t; i++) { cout << otv[i][0] << " " << otv[i][1] << endl; } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ long long int a,b; cin>>a>>b; long long int diff = abs(a-b); long long int moves = 0; if(a==b) cout<<0<<" "<<0<<"\n"; else{ if(a>diff) moves = a%diff; else moves = b%diff; if(moves>diff/2) moves = diff-moves; if(a==0){ diff = b; moves = 0; } else if(b==0){ diff = a; moves = 0; } else if(a%b==0 && b>=diff){ diff = b; moves = 0; } else if(b%a==0 && a>=diff){ diff = a; moves = 0; } cout<<diff<<" "<<moves<<"\n"; } } return 0; }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
""" from sys import stdin, stdout import math from functools import reduce import statistics import numpy as np import itertools import operator from sys import stdin, stdout import math from functools import reduce import statistics import numpy as np import itertools import sys import operator from collections import Counter import decimal """ import math import os import sys from math import ceil, floor, sqrt, gcd, factorial from io import BytesIO, IOBase from collections import Counter BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def prog_name(): a, b = map(int, input().split()) if a == b : print(0, 0) else: g = abs(a - b) ans = max(a, b) % g print(abs(a - b), min(min(ans, g - ans) , min(a, b))) def main(): # init = time() T = int(input()) for unique in range(T): # print("Case #"+str(unique+1)+":",end = " ") prog_name() # print(time() - init) if __name__ == "__main__": main()
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
t = int(input()) ans = [] for i in range(t): ip = input() ip = ip.split(' ') x,y = int(ip[0]), int(ip[1]) x1,y1 = x,y gcd = abs(x-y) if gcd == 0: ans.append('{} {}'.format(0,0)) continue c = 0 mul = int(x/gcd) ans.append('{} {}'.format(gcd,min(abs(gcd*mul-x),abs(gcd*(mul+1)-x)))) for i in range(t): print(ans[i])
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Scanner; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author valeesi */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); AExcitingBets solver = new AExcitingBets(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class AExcitingBets { public void solve(int testNumber, InputReader in, PrintWriter out) { long a = in.nextLong(), b = in.nextLong(); if (a == b) { out.println("0 0"); return; } if (a < b) { long tmp = a; a = b; b = tmp; } long diff = a - b; long add = ((b + diff) / diff) * diff - b; long minus = b - (b / diff) * diff; long count = Math.min(add, minus); if (IntegerUtils.gcd(a, b) == diff) { count = 0; } out.println(diff + " " + count); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public Scanner scanner; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); scanner = new Scanner(reader); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } } static class IntegerUtils { public static long gcd(long a, long b) { a = Math.abs(a); b = Math.abs(b); while (b != 0) { long temp = a % b; a = b; b = temp; } return a; } } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
from math import gcd for _ in range(int(input())): a, b = (map(int, input().split())) if a == b: print(0, 0) else: diff = abs(a - b) ans1 = min(a, b) % diff ans2 = diff - min(a, b) % diff print(diff, min(ans1, ans2))
PYTHON3
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import java.io.*; import java.util.*; public class A { public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t; t = Integer.parseInt(br.readLine()); while(t --> 0) { String[] xy = br.readLine().split(" "); long a = Long.parseLong(xy[0]); long b = Long.parseLong(xy[1]); long diff = Math.abs(a - b); if(diff == 0) { pw.println("0 0"); }else if(a == 0 || b == 0) { pw.println(Math.max(a, b) + " 0"); }else { pw.println(diff + " " + Math.min(a%diff, diff - a%diff)); } } pw.flush(); pw.close(); } }
JAVA
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
#include <bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ long long a, b; cin>>a>>b; if(a==b){ cout<<0<<' '<<0<<endl; }else{ long long c=abs(a-b); long long d=min(a%c, c-a%c); cout<<c<<' '<<d<<endl; } } }
CPP
1543_A. Exciting Bets
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)](https://en.wikipedia.org/wiki/Greatest_common_divisor) 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 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⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). 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 Input 4 8 5 1 2 4 4 3 9 Output 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.
2
7
import random import collections import string import math import copy import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip("\r\n") # mo = 998244353 mo = int(1e9+7) def exgcd(a, b): if not b: return 1, 0 y, x = exgcd(b, a % b) y -= a//b * x return x, y def getinv(a, m): x, y = exgcd(a, m) return -1 if x == 1 else x % m def comb(n, b): res = 1 b = min(b, n-b) for i in range(b): res = res*(n-i)*getinv(i+1, mo) % mo # res %= mo return res % mo def quickpower(a, n): res = 1 while n: if n & 1: res = res * a % mo n >>= 1 a = a*a % mo return res def quickpowerm(a, n, mo): res = 1 while n: if n & 1: res = res * a % mo n >>= 1 a = a*a % mo return res def dis(a, b): return abs(a[0]-b[0]) + abs(a[1]-b[1]) def getpref(x): if x > 1: return (x)*(x-1) >> 1 else: return 0 def orafli(upp): primes = [] marked = [False for i in range(upp+3)] prvs = [i for i in range(upp+3)] for i in range(2, upp): if not marked[i]: primes.append(i) for j in primes: if i*j >= upp: break marked[i*j] = True prvs[i*j] = j if i % j == 0: break return primes, prvs def lower_ord(c: str) -> int: return ord(c)-97 def upper_ord(c: str) -> int: return ord(c) - 65 def read_list(): return [int(i) for i in input().split()] def read_int(): s = input().split() if len(s) == 1: return int(s[0]) else: return map(int, s) def ask(s): print(f"? {s}", flush=True) def answer(s): print(f"{s}", flush=True) import functools # primes, prvs = orafli(100010) import itertools from fractions import Fraction # A = list(map(list,(zip(*A)))) def solve(): a, b = read_int() if a == b: print(0,0) return dvd = abs(a-b) am = a % dvd ans2 = min(am, dvd - am) print(dvd, ans2) # fi = open('C:\\cppHeaders\\CF2020.12.17\\test.data', 'r') # def input(): return fi.readline().rstrip("\r\n") # primes, prv = orafli(10001) # solve() T = int(input()) # t = 1 for ti in range(T): solve() # except: # traceback.print_exc()
PYTHON3