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 math n=int(input()) for g in range(n): a,b=map(int,input().split()) diff=abs(a-b) x=min(a,b) if a==b: print(0,0) else: y=x%diff z=min(y,diff-y) print(diff,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
/*package whatever //do not write package name here */ import java.util.*; public class GFG { public static void main (String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); while(n!=0) { long diff=0; long count =0; long ans=0; long a=scan.nextLong(); long b=scan.nextLong(); diff= Math.abs(a-b); System.out.println(diff); if(a!=b) { count=Math.min(a%diff,diff-a%diff); } else { count=0; } System.out.println(count); 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
#include <iostream> #include <math.h> #include <algorithm> 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 exc = abs(a-b); long long num = min(a%exc, exc - a%exc); cout << exc << " " << num << 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 java.io.*; import java.util.*; public class test3 { static long MOD = (int)1e9 +7; static long mod(long n) { return (n%MOD +MOD)%MOD; } public static void main(String[] args) throws IOException { FastReader f = new FastReader(); int t = f.nextInt(); while(t-->0) { long a = f.nextLong(),b = f.nextLong(); long diff = Math.abs(a-b); if(diff==0) { System.out.println(0+" "+0); }else { long left = (a/diff)*diff; long right = left+diff; System.out.println(diff+" "+Math.min(Math.abs(left-a),Math.abs(right-a)));; } } } static long fp(long a, long b) { if (b == 0) return 1; long t = fp(a, b / 2); if (b % 2 == 0) return mod(t * t); else return mod(mod(t * t) * a); } static long GCD(long a,long b) { if(a%b==0)return b; else return GCD(b,a%b); } static ArrayList<Long> primeFectors(long n){ ArrayList<Long> ret = new ArrayList<>(); ret.add(1L); while (n%2==0) { ret.add(2L); n /= 2; } for (long i = 3; i <= Math.ceil(Math.sqrt(n)); i+= 2) { // While i divides n, print i and divide n while (n%i==0) { ret.add(i); n /= i; } } if (n > 2) ret.add(n); return ret; } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static void print(int x,int y,int d,int n) { int i = 0; System.out.print(x+" "+y+" "); for(int j = x+d;j<y;j+=d) { System.out.print(j+" "); i++; } for(int j = x-d;j>0;j-=d) { if(i==n)return; System.out.print(j+" "); i++; } for(int j = y+d;j<1000000000;j+=d) { if(i==n)return; System.out.print(j+" "); i++; } } static int prime(int n){ int ret = 0; while(n%2==0){ ret++; n/=2; } for(int i=3;i<=Math.sqrt(n);i+=2){ while(n%i==0){ ret++; n/=i; } } if(n>2)ret++; return ret; } static long nCr(int n, int r) { if(n<r) { return 0; } long[] C=new long[r+1]; C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = Math.min(i, r); j > 0; j--) C[j] = (C[j] + C[j-1]); } return C[r]; } static ArrayList<Integer> readArray(FastReader f,int size){ ArrayList<Integer> ret = new ArrayList<Integer>(); for(int i=0;i<size;i++) { ret.add(f.nextInt()); }return ret; } 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.util.*; import java.io.*; public class CodeChef{ public static void main(String[] args) { FastReader in = new FastReader(); int t = in.nextInt(); while(t-->0) { long a = in.nextLong(); long b = in.nextLong(); if(a-b==0) System.out.println(0+" "+0); else { long count =0; long d = Math.abs(a-b); long f = a/d; long x = Math.abs(a-(f*d)); long y = Math.abs(a-((f+1)*d)); System.out.println(d+" "+ Math.min(x, y)); } } } 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; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static long gcd(Long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } static long fastpower(long a,long b,int n ) { long res =1; while(b>0) { if((b&1)!=0) { res = (res* a % n)%n; } a = (a % n*a % n) % n; b = b >> 1; } return res; } static int catalan(int n) { int res = 0; // Base case if (n <= 1) { return 1; } for (int i = 0; i < n; i++) { res += catalan(i) * catalan(n - i - 1); } return res; } static int mod(int a, int m) { return (a%m + m) % m; } }
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 sys import stdin, stdout def arrin(): return list(map(int, stdin.readline().split())) def num1in(): return int(stdin.readline()) def num2in(): a, b = map(int, stdin.readline().split()) return a, b def num3in(): a, b, c = map(int, stdin.readline().split()) return a, b, c def num4in(): a, b, c, d = map(int, stdin.readline().split()) return a, b, c, d def num5in(): a, b, c, d, e = map(int, stdin.readline().split()) return a, b, c, d, e t=num1in() for test in range(t): a,b=num2in() diff = abs(a-b) if(diff == 0): print(0,0) continue n1 = (a//diff) n2 = n1+1 n1 = n1*diff n2 = n2*diff moves = min(abs(a-n1),abs(a-n2)) print(diff,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 java.math.*; import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { long a=sc.nextLong(); long b=sc.nextLong(); long c; if(a<b){ c=a; a=b; b=c; } if(a==b) System.out.println("0 0"); else System.out.println(Math.abs(a-b)+" "+Math.min(Math.abs(a%(a-b)),Math.abs(a-b-a%(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
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define PB push_back #define MP make_pair #define PPB pop_back int gcd(int a, int b){ if(b==0) return a; return gcd(b,a%b); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; cin>>t; while (t--){ ll a,b; cin>>a>>b; if(a==b){ cout<<"0 0"<<endl; continue; } ll m=abs(a-b); ll n=min(a%m,m-a%m); cout<<m<<" "<<n<<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.io.*; import java.util.*; public class Main { static 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(); } } public static void main(String[] args) throws IOException { try{ //Your Solve Reader s = new Reader(); int t = s.nextInt(); for(int p = 0;p < t;p++){ long a = s.nextLong(); long b = s.nextLong(); long max = Math.max(a,b); long min = Math.min(a,b); if(a==b){ System.out.println("0 0"); }else if(min==0){ System.out.println(max + " 0"); }else{ long h = max-min; long upcap = (long)Math.ceil((double)min/h); long lowcap = min/h; System.out.println(h + " " + Math.min(upcap*h-min,min-lowcap*h)); } } }catch(Exception e){ return; } } }
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<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define MOD 1000000007 // void func(){ // } void solution(){ ll a,b; cin>>a>>b; ll val = abs(a-b); if(val==0){ cout<<"0 0"<<endl; return; } ll count = min(val-(a%val),a%val); cout<<val<<" "<<count<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t; cin>>t; // func(); while(t--){ solution(); } 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.*; public class ExcitingBets { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); long T = sc.nextLong(); //sc.nextLong(); for(int t=0;t<T;t++) { long a = sc.nextLong(); long b = sc.nextLong(); if(a==b) { System.out.println(0 + " "+ 0); } else { long val = a-b; long max_exit = Math.abs(val); long moves = 0; long mod1 = a%max_exit; long mod2 = max_exit*(a/max_exit+1)-a; moves = Math.min(mod1, mod2); System.out.println(max_exit + " " + 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> #define ll long long using namespace std; int main() { // freopen("file.in", "r", stdin); int t; cin >> t; while(t--){ ll a, b; cin>>a>>b; if(a == b) { cout<<0<<' '<<0<<'\n'; continue; } cout<<abs(a-b)<<' '; cout<<min(a%abs(a-b), abs(a-b) - a%abs(a-b))<<'\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.util.*; public class Div2_730A { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0){ long a=sc.nextLong(); long b=sc.nextLong(); long gcd=Math.abs(a-b); if(a>b){ long temp=a; a=b; b=temp; } long ans=0; if(gcd!=0){ long prev=gcd * (a/gcd); long next=gcd * ((a/gcd)+1); ans=Math.min(Math.abs(prev-a), Math.abs(next-a)); } System.out.println(gcd + " "+ans); 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
#include<bits/stdc++.h> using namespace std; const int N=100200; typedef long long ll; int main(){ ll t; ll a, b, k, mi; cin>>t; while(t--){ cin>>a>>b; if(b>a)swap(a,b); if(a==b)cout<<"0 0"<<endl; else{ k=a-b; mi=min(a%k,k-a%k); cout<<k<<" "<<mi<<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
def main(): test = int(input()) for _ in range(test): a, b = map(int, input().split()) if a == b: print("0 0") continue if a > b: a, b = b, a diff = abs(a - b) if b % diff == 0: print(f"{diff} {0}") continue mul = a // diff s1 = (mul + 1) * diff - a s2 = min(a, a - mul * diff) print(f"{diff} {min(s1, s2)}") 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("")) import math i , it, a,b = 0,0, t*[0], t*[0] while i<t: a[i], b[i] = map(int, input().split()) i += 1 for i in range(0,t): diff = abs(a[i] - b[i]) if diff != 0: lol = a[i] % diff if lol < diff/2: print(diff, lol) else: print(diff, diff - lol) else: print(0, 0)
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): x,y=[int(x) for x in input().split(' ')] a,b=min(x,y),max(x,y) if b==a: print(0,0) elif a*b==0: print(b,0) else: print(b-a,min(a%(b-a),b-a-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
#include<bits/stdc++.h> #define int long long #define pb push_back #define MAX INT_MAX #define MIN INT_MIN #define fr(a,b) for(int i = a; i < b; i++) #define rep(i,a,b) for(int i = a; i < b; i++) #define mod 1000000007 #define all(x) (x).begin(), (x).end() #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) using namespace std; void solve(){ int a,b; cin >> a >> b; int ans=abs(a-b); if(a==b){ cout << "0 0\n"; } else{ if(a%ans==0 && b%ans==0){ cout << ans << " 0\n"; } else{ cout << ans << " " << min(a%ans,ans-a%ans) << "\n"; } } } signed main() { fast_io; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif 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
import math import sys input = sys.stdin.readline #Ceil check function def chk(p, a): if p % a == 0: return 0 return 1 for _ in range(int(input())): a,b = map(int,input().split()) if a == b: sys.stdout.write(str(0)+" "+str(0)+"\n") continue ans = max(a,b)-min(a,b) num1 = 0 num2 = ans e1 = min(a,b)-((min(a,b)//ans)*ans) e2 = (((min(a,b)//ans)+chk(min(a,b),ans))*ans)-min(a,b) sys.stdout.write(str(ans) + " " + str(min(e1,e2,ans)) + "\n")
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.*; import java.math.BigInteger; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.util.List; import java.util.Map; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; public class h { static 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(); } } public static void main(String args[]) throws NumberFormatException, IOException ,java.lang.Exception { Reader reader = new Reader(); //BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); //BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //int cases=Integer.parseInt(br.readLine()); //int cases=1; int cases=reader.nextInt(); while (cases-->0){ //String[] first=br.readLine().split(" "); //int N=Integer.parseInt(first[0]); //int L=Integer.parseInt(first[1]); long N=reader.nextLong(); long M=reader.nextLong(); //HashMap<Integer,Integer> x=new HashMap<Integer,Integer>(); //HashMap<String,Integer> y=new HashMap<String,Integer>(); long diff= Math.abs(N-M); if(N-M!=0) { long k=diff*(N/diff+1)-N; long mo=-1; if(k>=0) { mo=Math.min(k, N%diff); } else { mo=N%diff; } System.out.println(diff+" "+mo);} else System.out.println(0+" "+0); } // output.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 math d=int(input()) for p in range(0,d): m=0 n=0 a,b= list(map(int, input().split())) if a==b: print(0,0) continue x=math.gcd(a,b) if a>b: l=(a-b) else: l=(b-a) if x==l: print(x,0) continue q=a%l w=l-q print(l,min(q,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
kol = int(input()) for i in range(kol): a, b = [int(i) for i in input().split()] if a == b: print(0, 0) else: g = abs(a - b) m = min(a % g, g - a % g) print(g, 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
#!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def main(): t = int(input()) for _ in range(t): a, b = map(int, input().split()) diff = abs(a - b) if diff: print(diff, min(a % diff, -a % diff)) else: print(0, 0) # 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") # endregion if __name__ == "__main__": 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; #define ll long long int main(){ ll t; cin>>t; while(t--){ ll n,k; cin>>n>>k; if(abs(n-k)==0){ cout<<0<<" "<<0<<endl; continue; } ll p=abs(n-k); cout<<p<<" "<<min({n%p, k%p, p-(n%p), p-(k%p)})<<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 <fstream> #include <map> #include <vector> #include <algorithm> #include <queue> #include <cmath> #define MAX_N 10002 #define mp make_pair #define pb push_back #define pii pair<int,int> typedef long long ll; using namespace std; int main(){ int t; cin >> t; while(t--){ ll a,b; cin >> a >> b; if(a==b){ cout << "0 0" << endl; continue; } if(a<b) swap(a,b); ll c = a-b; cout << c << " " << min(a%c,c-a%c) << 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
//created by Whiplash99 import java.io.*; import java.util.*; public class A { private static long diff(long A, long B){return (A>=B)?A-B:B-A;} public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i,N; int T=Integer.parseInt(br.readLine().trim()); StringBuilder sb=new StringBuilder(); while(T-->0) { String[] s=br.readLine().trim().split(" "); long A=Long.parseLong(s[0]); long B=Long.parseLong(s[1]); long D=diff(A,B); sb.append(D).append(" "); if(D==0) sb.append(0); else { long X=(A/D)*D; long Y=((A+D)/D)*D; X=Math.min(diff(A,X),diff(A,Y)); sb.append(X); } sb.append("\n"); } System.out.println(sb); } }
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> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; if (b == 0) { return a; } if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } int main() { int t; cin>>t; while(t--) { long long int a,b; cin>>a>>b; if(a==b) { cout<<0<<" "<<0<<endl; }else if (a>b) { long long int k = a-b; long long int n = a%k; if(n==0) { cout<<gcd(a,b)<<" "<<0<<endl; }else if(k-n>n) { cout<<gcd(a-n,b-n)<<" "<<n<<endl; }else{ cout<<gcd(a+k-n,b+k-n)<<" "<<k-n<<endl; } }else{ long long int k = b-a; long long int n = b%k; if(n==0) { cout<<gcd(a,b)<<" "<<0<<endl; }else if(k-n>n) { cout<<gcd(a-n,b-n)<<" "<<n<<endl; }else{ cout<<gcd(a+k-n,b+k-n)<<" "<<k-n<<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, os.path if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") ############################################ def gcd(a,b): if(a%b==0): return b return gcd(b, a%b) for t in range(int(input())): a , b = list(map(int,input().split())) if(a==b): print(0,0) else: v1 = max(a,b) v2 = min(a,b) val = v1-v2 p1 = v1%val value = min(p1, val-p1) print(val,value)
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 i in range (t): a,b=map(int,input().strip().split()) if a==b: print(0,0) elif abs(a-b)==1: print(1,0) else: gcd=abs(a-b) minimum=min(a,b) temp=minimum//gcd prv=(minimum-(temp*gcd)) nxt=((temp+1)*gcd)-minimum print(gcd,min(nxt,prv))
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> using namespace std; #include<bits/stdc++.h> #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin>>t; while(t--) { ll a,b,d; cin>>a>>b; if(a==b) { cout<<0<<" "<<0<<"\n"; } else if(abs(a-b)==1) { cout<<1<<" "<<0<<"\n"; } else { d=abs(a-b); ll prv=(a/d)*d; ll next=((a/d)+1)*d; ll ans=min((next-a),(a-prv)); cout<<d<<" "<<ans<<"\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
#include <iostream> using namespace std; #include<bits/stdc++.h> #define ll long long ll closestMultiple(ll n, ll x) { if(x>n) return x; n = n + x/2; n = n - (n%x); return n; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin>>t; while(t--) { ll a,b,d; cin>>a>>b; if(a==b) { cout<<0<<" "<<0<<"\n"; } else if(abs(a-b)==1) { cout<<1<<" "<<0<<"\n"; } else { d=abs(a-b); ll p=min(abs(a-closestMultiple(a,d)),abs(b-closestMultiple(b,d))); cout<<d<<" "<<min(p,min(a,b))<<"\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
I=lambda: map(int, raw_input().split()) t = input() for _ in xrange(t): a, b = I() d = abs(a-b) if d==0: print '0 0' continue q = min(a%d, (-a)%d) print str(d)+' '+str(q)
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
import java.util.*; import java.util.Map.Entry; import java.lang.*; import java.io.*; import java.math.BigInteger; public class CF { private static FS sc = new FS(); private static class FS { 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } private static class extra { static int[] intArr(int size) { int[] a = new int[size]; for(int i = 0; i < size; i++) a[i] = sc.nextInt(); return a; } static long[] longArr(int size) { long[] a = new long[size]; for(int i = 0; i < size; i++) a[i] = sc.nextLong(); return a; } static long intSum(int[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static long longSum(long[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static LinkedList[] graphD(int vertices, int edges) { LinkedList<Integer>[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); } return temp; } static LinkedList[] graphUD(int vertices, int edges) { LinkedList<Integer>[] temp = new LinkedList[vertices+1]; for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>(); for(int i = 0; i < edges; i++) { int x = sc.nextInt(); int y = sc.nextInt(); temp[x].add(y); temp[y].add(x); } return temp; } static void printG(LinkedList[] temp) { for(LinkedList<Integer> aa:temp) System.out.println(aa); } static long cal(long val, long pow) { if(pow == 0) return 1; long res = cal(val, pow/2); long ret = (res*res)%mod; if(pow%2 == 0) return ret; return (val*ret)%mod; } static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); } } static int mod = (int) 1e9 + 7; // static int mod = (int) 998244353; static int max = (int) 1e5 + 5; // static LinkedList<Integer>[] fl, fr; public static void main(String[] args) { int t = sc.nextInt(); // int t = 1; StringBuilder ret = new StringBuilder(); while(t-- > 0) { long a = sc.nextLong(); long b = sc.nextLong(); long diff = Math.abs(a-b); long gcd = extra.gcd(a, b); if(diff == 0) { ret.append(0 + " " + 0 + "\n"); } else if(a == 0 || b == 0){ ret.append(diff + " " + 0 + "\n"); } else if(gcd == diff) { ret.append(gcd + " " + 0 + "\n"); } else { long div = a/diff; long next = (div+1)*diff; long prev = div*diff; long min = Math.min(next-a, a-prev); ret.append(diff + " " + min + "\n"); } } System.out.println(ret); } }
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 loopf(i,a,b) for(int i=a;i<b;i++) #define loopb(i,a,b) for(int i=a;i>b;i--) #define pb push_back #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); #define ff first #define ss second #define vc vector #define vii vector<int> #define vll vector<long long> #define pii pair<int,int> #define pll pair<ll,ll> //General defs #define umap unordered_map #define uset unordered_set //Along with data types #define mapii map<int,int> #define mapll map<ll,ll> #define seti set<int> #define setll set<ll> #define umapii unordered_map<int,int> #define useti unordered_set<int> #define umapll unordered_map<ll,ll> #define usetll unordered_set<ll> #define all(x) x.begin(),x.end() #define endl "\n" #define ld long double const ll M=1e9+7; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define imax INT_MAX #define imin INT_MIN #define lmax LLONG_MAX #define lmin LLONG_MIN void solve(){ ll a,b; cin>>a>>b; if(a==b) { cout<<"0 0\n"; return; } if(a>b) swap(a,b); if(a==0) { cout<<b<<" "<<0<<endl; return; } cout<<abs(a-b)<<" "<<min(a%abs(a-b),min(a,abs(a-b)-a%(b-a)))<<endl; } int main() { bool take_test_cases=1; fast; if(take_test_cases) { int t; cin>>t; while(t--) solve(); } else 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
import sys input=sys.stdin.readline for _ in range(int(input())): a,b=map(int,input().split()) if(a==b): print(0,0) else: dif=abs(a-b) mi=min(a,b) rem=mi%dif print(dif,min(rem,abs(dif-rem)))
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 contest { static long gcd(long a,long b){ if(a==0){ return b; } return gcd(b%a,a); } static final int p = 1000000007; static class edge{ int u, v; edge(int i,int x){ this.u = i; this.v = x; } } public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); out:while(t-- >0) { long a = sc.nextLong(); long b = sc.nextLong(); if(a==b){ System.out.println("0 0"); continue ; } long diff = Math.abs(a-b); long gcd = gcd(a,b); if(diff==gcd){ System.out.println(diff+" 0"); } else{ long e = a/diff; long f = a/diff+1; long x = Math.min(Math.abs(e*diff-a),Math.abs(f*diff-a)); System.out.println(diff+" "+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
#include <iostream> using namespace std; using ll = long long; void solve() { ll a, b; cin >> a >> b; if (a == b ) { cout << "0 0\n"; return; } ll dif = abs(a-b); ll up = a%dif; if (up > dif - up) { cout << dif << " " << dif - up << endl; } else { cout << dif << " " << up << endl; } } int main() { ll 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 x first #define y second #define pb push_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { ll a, b; cin >> a >> b; if (a == b) cout << "0 0\n"; else { ll n = abs(a - b); cout << n << ' '; if (a % n == 0) cout << "0\n"; else cout << min(a % n, n - a % n) << '\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
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define pf push_front #define pll pair<ll,ll> #define pii pair<int,int> #define vll vector<ll> #define vi vector<int> #define sz(v) (ll)v.size() #define all(v) v.begin(),v.end() #define pq_max priority_queue<ll> #define pq_min priority_queue <ll, vector<ll>, greater<ll> > #define debug(x) cout<<#x<<"->"<<x<<endl #define debug2(x,y) cout<<#x<<"->"<<x<<", "<<#y<<"->"<<y<<endl #define debug3(x,y,z) cout<<#x<<"->"<<x<<", "<<#y<<"->"<<y<<","<<#z<<"->"<<z<<endl #define endl '\n' #define ff first #define ss second const ll INF = 4000000000000000000; const ll mod = 1000000007; const ll prime1 = 1999999973, prime2 = 1999999943; void read(vll& arr,ll n){ for(int i=0;i<n;i++) cin>>arr[i]; } ll solve(){ ll a,b; cin>>a>>b; if(a>b) swap(a,b); if(a==b){ cout<<"0 0\n"; return 0; } if(a==0){ cout<<b<<" 0\n"; return 0; } b-=a; ll ans1,ans2; if(a<=b){ ans1=b; ans2=min(b-a,a); } else{ ans1=b; ans2=min(a%b,b-(a%b)); } cout<<ans1<<" "<<ans2<<endl; return 0; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); // freopen("minima.in","r",stdin); // freopen("minima.out","w",stdout); ll T=1; cin>>T; for(int i=1;i<=T;i++){ //cout<<"Case #"<< i <<":"; solve(); //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 <bits/stdc++.h> #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } #define sz(x) (int)x.size() #define all(x) x.begin(),x.end() using namespace std; using ll = long long; void err(istream_iterator<string> it) {cerr << endl;} template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args) {cerr << *it << " = " << a << endl;err(++it, args...);} const int dx[] = {0, -1, 1, 0, 1, -1, 1, -1}; const int dy[] = {1 , 0, 0, -1, 1, -1, -1, 1}; const int mod = 1e9 + 7; const int N = 1e5 + 5; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> q; while(q--){ ll a , b; cin >> a >> b; ll ans = abs(a-b); if(a == b){ cout << 0 << ' ' << 0 << '\n'; continue; } ll c = (a / ans); ll d = ((a + ans - 1) / ans); if(d * ans - a <= a - c * ans){ cout << ans << ' ' << (d * ans - a) << '\n'; }else{ cout << ans << ' ' << (a - c * ans) << '\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.io.*; import java.util.*; public class MainClass { public static void main(String[] args) { Reader in = new Reader(System.in); int t = in.nextInt(); StringBuilder stringBuilder = new StringBuilder(); while (t-- > 0) { long a = in.nextLong(), b = in.nextLong(); long c = Math.max(a, b) - Math.min(a, b); if (a == b) { stringBuilder.append("0 0\n"); continue; } long min = Math.min(a, b); long d = Math.min((min + c - 1) / c * c - min, min - min / c * c); stringBuilder.append(c).append(" ").append(d).append("\n"); } System.out.println(stringBuilder); } } class Reader { public BufferedReader reader; public StringTokenizer tokenizer; public Reader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); 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 int nextInt() { return Integer.parseInt(next()); } public 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<iostream> using namespace std; #define long long long int main() { long tt; cin>>tt; while(tt--) { long a,b; cin>>a>>b; long h1,m1; h1=abs(a-b); if(h1==0) { cout<<0<<" "<<0<<endl; } else if(a==0 || b==0) { cout<<max(a,b)<<" "<<0<<endl; } else { m1=min(a%h1,h1-a%h1); cout<<h1<<" "<<m1<<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 #Library Info(ACL for Python/Pypy) -> https://github.com/not522/ac-library-python def input(): return sys.stdin.readline().rstrip() from math import gcd def main(): t = int(input()) for i in range(t): a,b = map(int,input().split()) if a == b: print(0,0) else: d = abs(a - b) r = a % d if r == 0: print(d,0) else: print(d,min(r,d - r)) return 0 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
#include <bits/stdc++.h> #define endl '\n' //pls remove while debugging #define ll long long int using namespace std; void solve() { ll a, b; cin>>a>>b; if (a == b) { cout<<0<<" "<<0<<endl; return; } ll dif = abs(a-b); cout<<abs(a - b)<<" "<<min(a%dif, dif- a%dif)<<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
import java.util.*; import java.io.*; public class Div730 { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// ///////// //////// ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// ///////// //////// ///////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { long a = sc.nextLong(); long b = sc.nextLong(); long diff = Math.abs(a - b); if (diff == 0) { pw.println("0 0"); } else { long ans = Math.min(a % diff, diff - (a % diff)); ans = Math.min(ans, Math.min(b % diff, diff - (b % diff))); pw.println(diff+" "+ans); } } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] nextIntArr(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
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 = [int(x) for x in input().split()] if(a<b): temp=a a=b b=temp diff = a-b if(diff==0): print('0 0') elif(diff==1): print('1 0') else: low = a//diff if(a - low*diff > 0.5*diff): low=low+1 c = low*diff d = (low-1)*diff steps = abs(c-a) print(diff,steps)
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.ArrayList; import java.util.HashMap; import java.util.PriorityQueue; import java.util.Set; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while(t-- > 0) { // one option is diff long a = s.nextLong() , b = s.nextLong(); long diff = Math.max(a, b) - Math.min(a , b); if(diff == 0) System.out.println("0 0"); else if(a % diff == 0) System.out.println(diff + " 0"); else System.out.println(diff + " " + Math.min(Math.min(a, b), Math.min(Math.max(a, b)%diff, (diff - Math.max(a, b)%diff)))); } } public static int gcd(int a , int b) { if(b == 0) return a; return gcd(b , a%b); } public static int factors(int n){ int re = 0; for(int i = 2; i * i <= n; i++) { while (n % i == 0) { n /= i; re += 1; } } if (n > 1) re += 1; return re; } } 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> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops") #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define inf 1e18+5 #define mod 1000000007 #define MAXN 100005 #define ll long long #define pb push_back #define sortv(v) sort(v.begin(),v.end()) #define tc(t) int t;cin>>t;while(t--) #define copy copy_n #define fr(i,a,b) for(long long i=a;i<b;i++) #define frr(i,a,b) for(long long i=a;i>=b;i--) #define frn(i,a,b,n) for(long long i=a;i<=b;i=i+n) #define gcd(x,y) __gcd(x,y) #define lcm(x,y) (x*y)/gcd(x,y) #define ld long double #define um unordered_map #define mkp make_pair #define b_s binary_search #define nob __builtin_popcount #define des greater<ll>() #define fi first #define se second ll mul(ll a, ll b, ll p = mod) {return ((a % p) * (b % p)) % p;} ll add(ll a, ll b, ll p = mod) {return (a % p + b % p) % p;} void input(ll a[], ll sz) {fr(i, 0, sz) cin >> a[i];} void print(ll a[], ll sz) {fr(i, 0, sz) {if (i == sz - 1) cout << a[i] << "\n"; else cout << a[i] << " ";}} ll maxr(ll a[], ll sz) {ll ma; fr(i, 0, sz) {if (i == 0) ma = a[i]; else if (a[i] > ma) ma = a[i];} return ma;} ll minr(ll a[], ll sz) {ll mi; fr(i, 0, sz) {if (i == 0) mi = a[i]; else if (a[i] < mi) mi = a[i];} return mi;} ll isprm(ll n) { if (n <= 1) return 0; if (n <= 3) return 1; if (n % 2 == 0 || n % 3 == 0) return 0; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return 0; return 1; } /*ll spf[MAXN]; void sieve() { spf[1]=1; for (ll i=2; i<MAXN; i++) spf[i] = i; for (ll i=4; i<MAXN; i+=2) spf[i] = 2; for(ll i=3;i*i<MAXN;i+=2) { if (spf[i]==i) { for (int j=i*i;j<MAXN;j+=i) if (spf[j]==j) spf[j]=i; } } }*/ ll power(ll x, ll y, ll p = mod) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } ll modInverse(ll n, ll p = mod) { return power(n, p - 2, p); } /*ll fac[MAXN]; ll inv[MAXN]; void fact(ll p=mod) { fac[0] = 1; for (ll i = 1 ; i < MAXN; i++) fac[i] = fac[i - 1] * i % p; for(ll i=0;i<MAXN;i++) inv[i]=modInverse(fac[i]); } ll ncrMod(ll n, ll r, ll p = mod) { if (r == 0) return 1; return (fac[n] * inv[r] % p * inv[n-r] % p) % p; }*/ //a+b=xor(a,b)+2*(a&b)// struct comp { bool operator()(const pair<ll, ll> &a, const pair<ll, ll> &b)const { if (a.fi == b.fi) { return (a.se > b.se); //second elem in descending } return (a.first < b.first);//first elem in ascending } }; int main() { fio; tc(t) { ll a,b; cin>>a>>b; ll d=abs(b-a); if(d==0) { cout<<"0 0\n"; continue; } ll r=a%d; cout<<d<<" "<<min(r,d-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
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class A { static long mod=(long)1e9+7; static long mod1=998244353; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t= in.nextInt(); while(t-->0) { long a = in.nextLong(); long b = in.nextLong(); long diff = Math.abs(a-b); if(diff == 0) out.println(0+" "+0); else { long rem = a % diff; long step2 = diff - rem; out.println(diff + " " + Math.min(rem, step2)); } } out.close(); } 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), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long gcd(long x, long y){ if(x==0) return y; if(y==0) return x; long r=0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while(a % b != 0){ r = a % b; a = b; b = r; } return r; } static long modulo(long a,long b,long c){ long x=1,y=a%c; while(b > 0){ if(b%2 == 1) x=(x*y)%c; y = (y*y)%c; b = b>>1; } return x%c; } public static void debug(Object... o){ System.err.println(Arrays.deepToString(o)); } static int upper_bound(int[] arr,int n,int x){ int mid; int low=0; int high=n; while(low<high){ mid=low+(high-low)/2; if(x>=arr[mid]) low=mid+1; else high=mid; } return low; } static int lower_bound(int[] arr,int n,int x){ int mid; int low=0; int high=n; while(low<high){ mid=low+(high-low)/2; if(x<=arr[mid]) high=mid; else low=mid+1; } return low; } static String printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.00000000000"); return String.valueOf(ft.format(d)); } static int countBit(long mask){ int ans=0; while(mask!=0){ mask&=(mask-1); ans++; } return ans; } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); 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 int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] readArray(int n) { int[] arr=new int[n]; for(int i=0;i<n;i++) arr[i]=nextInt(); return arr; } } }
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
#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; #define DEBUG #ifdef DEBUG template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } ostream &operator<<(ostream &os, const set<int> &se){ os << '{'; int now = 0; for(auto x : se){ if(now) { os << ','; } os << x; now++; } os << '}'; return os; } void debugg() { cerr << endl; } template <class T, class... Args> void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll,ll> P; typedef pair<int,int> pii; typedef vector<P> vpl; typedef tuple<ll,ll,ll> tapu; #define rep(i,n) for(int i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = (1<<30)-1; const ll linf = 1LL<<61; const int MAX = 510000; int dy[8] = {0,1,0,-1,1,-1,-1,1}; int dx[8] = {-1,0,1,0,1,-1,1,-1}; const double pi = acos(-1); const double eps = 1e-7; template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){ if(a<b){ a = b; return true; } else return false; } template<typename T> inline void print(T &a){ int sz = a.size(); for(auto itr = a.begin(); itr != a.end(); itr++){ cout << *itr; sz--; if(sz) cout << " "; } cout << "\n"; } template<typename T1,typename T2> inline void print2(T1 a, T2 b){ cout << a << " " << b << "\n"; } template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){ cout << a << " " << b << " " << c << "\n"; } void mark() {cout << "#" << "\n";} ll pcount(ll x) {return __builtin_popcountll(x);} const int mod = 1e9 + 7; //const int mod = 998244353; void solve(){ ll a,b; cin >> a >> b; if(a > b) swap(a,b); ll ans = b-a; if(ans == 0){ cout << "0 0\n"; return; } ll r = a % ans; cout << ans << " " << min(r,ans-r) << "\n"; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int t = 1; 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 ll long long int main(){ ll t; cin >> t; while (t--){ ll a, b; cin >> a >> b; if(a == b) cout << "0 0" << endl; else{ ll mE = abs(a - b); ll mA; ll x1 = (a/mE + 1) * mE - a; ll x2 = a - (a/mE) * mE; mA = min(x1, x2); cout << mE << " " << mA << 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 <bits/stdc++.h> using namespace std; #define ll long long #define int long long #define all(x) x.begin() , x.end() #define ld long double #define debug(x) cout << #x << " = " << x << "\n" const long long inf = 1e18 + 5LL; const int inf32 = INT_MAX; const int mod = 998244353 ; //1e9 + 7LL; const int N = (1e6 + 10); void solve(int t); void solve(); void ITO(); int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ITO(); int t = 1; cin >> t; for (int i = 0; i < t; i++) solve(i + 1); return 0; } void solve(int TcNumber) { int a , b; cin >> a >> b; if (a < b) swap(a , b); int gcd = a - b; int m = 0; if (gcd > 0) { int up = (b + gcd - 1) / gcd; int down = b / gcd; m = min(up * gcd - b , b - (down * gcd)); } cout << gcd << " " << m; cout << "\n"; return ; } void solve() {} void ITO() { #ifndef ONLINE_JUDGE freopen("inputf.in", "r", stdin); freopen("output.txt", "w", stdout); #endif }
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 i in[*open(0)][1:]: a,b=map(int,i.split()) if a==b: print(0,0) else: g=e=abs(a-b) c=min(a,b) while g<c: g+=e if g-c < abs(g-e-c): d=g-c else: d=abs(g-e-c) print(e,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
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll t; cin>>t; while(t--) { ll a,b; cin>>a>>b; if(a==b) { cout<<"0"<<" "<<"0"<<endl; } else{ ll g=abs(a-b); cout<<g<<" "; cout<<min(a%g,g-a%g)<<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
from math import gcd for _ in range(int(input())): a,b=map(int,input().split()) if a==b:print(0,0) elif abs(a-b)==1:print(1,0) else:print(abs(a-b),min(a%abs(a-b),abs(a-b)-a%abs(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.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import java.io.*; public class abc { static PrintWriter pw; /* * static long inv[]=new long[1000001]; static long dp[]=new long[1000001]; */ /// MAIN FUNCTION/// public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); pw = new PrintWriter(System.out); // use arraylist as it use the concept of dynamic table(amortized analysis) // Arrays.stream(array).forEach(a -> Arrays.fill(a, 0)); /* List<Integer> l1 = new ArrayList<Integer>(); */ int tst = sc.nextInt(); while (tst-- > 0) { long a=sc.nextLong(); long b=sc.nextLong(); if(a==b) pw.println("0 0"); else { long dif=Math.abs(a-b); long c=0; if(a%dif==0 && b%dif==0) pw.println(dif+" "+0); else { long div=a/dif+1; long rem=a%dif; div*=dif; div-=a; pw.println(dif+" "+Math.min(rem,div)); } } } pw.flush(); } public static int helper(int b[][], int k, int m, int n) { int res = 0, f = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (k == b[i][j]) { res = j; f = -1; break; } } if (f == -1) break; } return res; } public static void debugger() { Random rand = new Random(); int tst = (int) (Math.abs(rand.nextInt()) % 2 + 1); pw.println(tst); while (tst-- > 0) { int n = (int) (Math.abs(rand.nextInt()) % 5 + 1); pw.println(n); for (int i = 0; i < n; i++) { pw.print((int) (Math.abs(rand.nextInt()) % 6 + 1) + " "); } pw.println(); } } static void recursion(int n) { if (n == 1) { pw.print(n + " "); return; } // pw.print(n+" "); gives us n to 1 recursion(n - 1); // pw.print(n+" "); gives us 1 to n } // ch.charAt(i)+"" converts into a char sequence 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; } /* CREATED BY ME */ int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static boolean isPrime(long n) { if (n == 2) return true; long i = 2; while (i * i <= n) { if (n % i == 0) return false; i++; } return true; } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static double max(double x, double y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static class pair { long x; long y; public pair(long a, long b) { x = a; y = b; } } public static class Comp implements Comparator<pair> { public int compare(pair a, pair b) { long ans = a.x - b.x; if (ans > 0) return 1; if (ans < 0) return -1; return 0; } } // modular exponentiation public static long fastExpo(long a, int n, int mod) { if (n == 0) return 1; else { if ((n & 1) == 1) { long x = fastExpo(a, n / 2, mod); return (((a * x) % mod) * x) % mod; } else { long x = fastExpo(a, n / 2, mod); return (((x % mod) * (x % mod)) % mod) % mod; } } } public static long modInverse(long n, int p) { return fastExpo(n, p - 2, p); } /* * public static void extract(ArrayList<Integer> ar, int k, int d) { int c = 0; * for (int i = 1; i < k; i++) { int x = 0; boolean dm = false; while (x > 0) { * long dig = x % 10; x = x / 10; if (dig == d) { dm = true; break; } } if (dm) * ar.add(i); } } */ public static int[] prefixfuntion(String s) { int n = s.length(); int z[] = new int[n]; for (int i = 1; i < n; i++) { int j = z[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) j = z[j - 1]; if (s.charAt(i) == s.charAt(j)) j++; z[i] = j; } return z; } public static void dfs(int index, boolean vis[], int a[], int b[], int n) { vis[index] = true; for (int i = 0; i < n; i++) { if (!vis[i] && (a[i] == a[index] || b[i] == b[index])) dfs(i, vis, a, b, n); } } // counts the set(1) bit of a number public static long countSetBitsUtil(long x) { if (x <= 0) return 0; return (x % 2 == 0 ? 0 : 1) + countSetBitsUtil(x / 2); } //tells whether a particular index has which bit of a number public static int getIthBitsUtil(int x, int y) { return (x & (1 << y)) != 0 ? 1 : 0; } public static void swap(long x, long y) { x = x ^ y; y = y ^ x; x = x ^ y; } /* * static long mod_inverse(long a, long b) { long mod=(long) (1000000007); long * ans=1; while(b>0) { if((b%2)==0) ans=(ans*a) %mod; b/=2; a=(a*a) % mod; * * } return ans; } */ public static double decimalPlaces(double sum) { DecimalFormat df = new DecimalFormat("#.00"); String angleFormated = df.format(sum); double fin = Double.parseDouble(angleFormated); return fin; } //use collections.swap for swapping static boolean isSubSequence(String str1, String str2, int m, int n) { int j = 0; for (int i = 0; i < n && j < m; i++) if (str1.charAt(j) == str2.charAt(i)) j++; return (j == m); } static long sum(long n) { long s2 = 0, max = -1, min = 10; while (n > 0) { s2 = (n % 10); min = min(s2, min); max = max(s2, max); n = n / 10; } return max * min; } static long pow(long base, long power) { if (power == 0) { return 1; } long result = pow(base, power / 2); if (power % 2 == 1) { return result * result * base; } return result * result; } // return the hash value of a string static long compute_hash(String s) { long val = 0; long p = 31; long mod = (long) (1000000007); long pow = 1; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); val = (val + (int) (ch - 'a' + 1) * pow) % mod; pow = (pow * p) % mod; } return val; } // return a prefix sum array of hash values /* * static void prefix_array_value(String s) { long p=31; long mod=(long) * (1000000007); long pow=1; inv[0]=1; dp[0]=(long)(s.charAt(0)-'a'+1); * * for(int i=1;i<s.length();i++) { char ch=s.charAt(i); pow= ( pow*p ) % mod; * inv[i]=mod_inverse(pow, mod-2); dp[i]=(dp[i-1]+ (long)(ch- 'a' +1)*pow)%mod; * } } */ /* * static long substringHash(int L , int R ) { long result = dp[R]; long * mod=(long) (1000000007); if(L > 0) result = (result - dp[L-1] + mod) % mod; * result= (result * inv[L]) % mod; return result; } */ }
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 floor, gcd, fabs, factorial, fmod, sqrt, inf, log for _ in range(int(input())): a,b=map(int,input().split()) if a==b: print(0,0) else: print(abs(a-b),min(a%abs(a-b),(max(a,b)-b%abs(b-a))+abs(b-a)-max(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 sys, os from io import BytesIO, IOBase from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log from collections import defaultdict as dd, deque from heapq import merge, heapify, heappop, heappush, nsmallest from bisect import bisect_left as bl, bisect_right as br, bisect # 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") stdin, stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) mod = pow(10, 9) + 7 mod2 = 998244353 def inp(): return stdin.readline().strip() def iinp(): return int(inp()) def out(var, end="\n"): stdout.write(str(var)+"\n") def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end) def lmp(): return list(mp()) def mp(): return map(int, inp().split()) def smp(): return map(str, inp().split()) def ceil(a,b): return (a+b-1)//b def lcm(a, b): return a*b//gcd(a, b) S1 = 'abcdefghijklmnopqrstuvwxyz' S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def isprime(x): if x<=1: return False if x in (2, 3): return True if x%2 == 0: return False for i in range(3, int(sqrt(x))+1, 2): if x%i == 0: return False return True from math import gcd def solve(): a, b = lmp() excitement = abs(a-b) if excitement == 0: print('0 0') else: print(f'{excitement} {min(a%excitement, excitement-a%excitement)}') if __name__ == '__main__': t = iinp() while(t): 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
def gcd(a,b): if(b==0): return a return gcd(b,a%b) for t in range(int(input())): a,b = map(int,input().split()) maxi = abs(a-b) gd = gcd(a,b) if(a==b): print(0,0) else: diff = abs(a-b) a=min(a,b) moves = min(a%diff,diff-a%diff) print(maxi,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
for _ in range(int(input())): a, b = map(int, input().split()) excitement = abs(a - b) if excitement != 0: moves = min(a % excitement, excitement - a % excitement) else: moves = 0 print(excitement, 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 java.util.*; import java.io.*; public class CR730A { public static void main(String[] args) { FastIO io = new FastIO(); int t = io.nextInt(); while (t --> 0) { long a, b, c, d; a = io.nextLong(); b = io.nextLong(); if (a > b) { c = a; d = b; } else { c = b; d = a; } long e = c - d; io.print(e); io.print(" "); if (e == 0) { io.print(0); } else { long x = d/e; long y = d/e + 1; io.print(Math.min(y * e - d, d - x * e)); } io.println(); } io.close(); } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { super(System.out); br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } super.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; typedef long long ll; void testcase () { ll a, b; cin >> a >> b; if (a < b) swap (a, b); if (a == b) { puts ("0 0"); return; } ll diff = a - b; cout << diff << ' ' << min (a % diff, diff - (a % diff)) << "\n"; return; } int main () { int t = 1; cin >> t; while (t--) { testcase(); } 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 ll long long #define int ll #define pb push_back #define INF 1e18 #define MOD 1000000007 #define mp make_pair #define REP(i,n) for (int i = 0; i < n; i++) #define FOR(i,a,b) for (int i = a; i < b; i++) #define REPD(i,n) for (int i = n-1; i >= 0; i--) #define FORD(i,a,b) for (int i = a; i >= b; i--) #define umap unordered_map #define pii pair<int,int> #define F first #define S second #define mii map<int,int> #define vi vector<int> #define vvi vector<vi> #define itr :: iterator it #define all(v) v.begin(),v.end() #define WL(t) while(t--) #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) ((a)*(b))/gcd((a),(b)) #define out(x) cout << #x << " is " << x <<"\n" #define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); using namespace std; void solve() { int a, b; cin>>a>>b; if(a==b) { cout<<"0 0"; return; } int c=abs(a-b); cout<<c<<" "<<min(a%c, c-a%c); } signed main() { FastIO int t=1; cin>>t; WL(t){ solve(); if(t!=0) cout<<"\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
#include <bits/stdc++.h> using namespace std; #define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef long long ll; typedef long double ld; typedef vector<ll> vll; typedef pair<ll,ll> pll; #define F first #define S second #define PB push_back #define MP make_pair #define all(x) x.begin(),x.end() #define foru(i, a, b) for(ll i=a; i < (b) ; i++) #define fore(i, a, b) for(ll i=a ; i <= (b); i++) const ll mod = 1e9+7, N = 2e6+7, M = 2e6+7, INF = INT_MAX/10; ll power(ll x, ll y){ x = x%mod, y=y%(mod-1);ll ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;} ll str_to_int(string s){stringstream asteya(s); ll x=0; asteya>>x; return x;} string int_to_str(ll n){return to_string(n);} void testcase(){ ll a,b,ans,moves,sub; cin>>a>>b; if(a==0 and b==0) cout<<0<<" "<<0<<endl; else{ ans = abs(a-b); if(ans == 0) sub=0; else sub = min(a,b)%ans; if(ans > 0 && ans !=1) moves = min(sub, (ans-(a%ans))%ans); else moves = 0; cout<<ans<<" "<<moves<<endl; } } int main(){ fast; int t=1; cin >> t; while(t--){ testcase(); } 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; #define int long long void solve(){ int a, b; cin >> a >> b; if (a == b) { cout << "0 0\n"; return; } if (a < b) swap(a, b); if (!b) { cout << a << ' ' << 0 << '\n'; return; } int d = a - b; int x = b % d; cout << d << ' ' << min(x, d - x) << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifdef local freopen("inp.txt","r",stdin); freopen("out.txt","w",stdout); #endif int test; cin >> test; while (test--) 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import static java.lang.System.*; public class Main { static boolean[] isNotPrime = new boolean[20_100]; static { for (int i = 2; i <= Math.sqrt(isNotPrime.length); i++) { if (!isNotPrime[i]) { for (int j = 2 * i; j > 0 && j < isNotPrime.length; j += i) { isNotPrime[j] = true; } } } } public static int gcd(int a, int b){ if(b == 0){ return a; } return gcd(b, a%b); } public static void main(String[] args) { FastReader fs = new FastReader(); int t = fs.nextInt(); while(t-- != 0) { long a = fs.nextLong(); long b = fs.nextLong(); if(a == b){ out.println(0+" "+0); }else{ long x = Math.abs(a - b); long y = Math.min(a%x, x-a%x); out.println(x+" "+y); } } } } 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 sys input=sys.stdin.readline for _ in range(int(input())): # n=int(input()) a,b=map(int,input().split()) # s=input().strip() # a=(list(map(int,input().split()))) if a==b: print(0,0) continue if b<a: a,b=b,a print(b-a,min(b-a-b%(b-a),b%(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 sys lines = sys.stdin.read().strip().split('\n') for line in lines[1:]: a, b = map(int, line.strip().split()) if b<a: a, b = b, a if a==b: print('0 0') else: dif = b-a r = a % dif move = r if r*2 < dif else dif-r print(f'{dif} {move}')
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 Main{ static void main() throws Exception{ long a=sc.nextLong(),b=sc.nextLong(); if(a==b) { pw.println("0 0"); return; } long dif=Math.abs(a-b); pw.print(dif+" "); pw.println(Math.min(Math.min((dif-(a%dif))%dif, (dif-(b%dif))%dif), Math.min(a%dif, b%dif))); } public static void main(String[] args) throws Exception{ sc=new MScanner(System.in); pw = new PrintWriter(System.out); int tc=1; tc=sc.nextInt(); for(int i=1;i<=tc;i++) { // pw.printf("Case #%d: ", i); main(); } pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void dbg(int[]in) { System.out.println(Arrays.toString(in)); } static void dbg(long[]in) { System.out.println(Arrays.toString(in)); } static void sort(int[]in) { shuffle(in); Arrays.sort(in); } static void sort(long[]in) { shuffle(in); Arrays.sort(in); } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
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(): alpha = 'abcdefghijklmnopqrstuvwxyz' ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' inf = 1e17 mod = 10 ** 9 + 7 # Max = 10 ** 1 # primes = [] # prime = [True for i in range(Max + 1)] # p = 2 # while (p * p <= Max + 1): # # # If prime[p] is not # # changed, then it is a prime # if (prime[p] == True): # # # Update all multiples of p # for i in range(p * p, Max + 1, p): # prime[i] = False # p += 1 # # for p in range(2, Max + 1): # if prime[p]: # primes.append(p) # # print(primes) def factorial(n): f = 1 for i in range(1, n + 1): f = (f * i) % mod # Now f never can # exceed 10^9+7 return f def ncr(n, r): # initialize numerator # and denominator num = den = 1 for i in range(r): num = (num * (n - i)) % mod den = (den * (i + 1)) % mod return (num * pow(den, mod - 2, mod)) % mod def solve(a,b): if a == b: print(0,0) return GCD = max(a,b)-min(a,b) print(GCD,min(min(a,b)%GCD,GCD-min(a,b)%GCD)) pass t = int(input()) ans = [] for _ in range(t): #n = int(input()) a,b = map(int, input().split()) # s = input() #arr = [int(x) for x in input().split()] # a2 = [int(x) for x in input().split()] # grid = [] # for i in range(n): # grid.append([int(x) for x in input().split()][::-1]) ans.append(solve(a,b)) # for answer in ans: # print(answer) if __name__ == "__main__": import sys, threading import bisect import math import itertools from sys import stdout # Sorted Containers import heapq from queue import PriorityQueue # Tree Problems #sys.setrecursionlimit(2 ** 32 // 2 - 1) #threading.stack_size(1 << 27) # fast io input = sys.stdin.readline thread = threading.Thread(target=main) thread.start() thread.join()
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
// 1543A.cpp // Created by Ashish Negi /******** All Required Header Files ********/ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> #include <queue> #include <deque> #include <bitset> #include <iterator> #include <list> #include <stack> #include <map> #include <set> #include <functional> #include <numeric> #include <utility> #include <limits> #include <time.h> #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include "bits/stdc++.h" #include <numeric> // contains inbuilt gcd(a, b) function // #include "ext/pb_ds/assoc_container.hpp" // #include "ext/pb_ds/tree_policy.hpp" using namespace std; /******* All Required define Pre-Processors and typedef Constants *******/ const string nl = "\n"; #define scd(t) scanf("%d",&t) #define scld(t) scanf("%ld",&t) #define sclld(t) scanf("%lld",&t) #define scc(t) scanf("%c",&t) #define scs(t) scanf("%s",t) #define scf(t) scanf("%f",&t) #define sclf(t) scanf("%lf",&t) #define inp(t) cin>>t #define inpp(t,u) cin>>t>>u #define inppp(t,u,v) cin>>t>>u>>v #define out(t) cout<<t<<nl #define outt(t,u) cout<<t<<" "<<u<<nl #define outtt(t,u,v) cout<<t<<" "<<u<<" "<<v<<nl #define outf(t, p) cout << fixed; cout << setprecision(p); out(t); #define mems(a, b) memset(a, (b), sizeof(a)) #define lpj(i, j, k) for (int i=j ; i<k ; i+=1) #define rlpj(i, j, k) for (int i=j ; i>=k ; i-=1) #define lp(i, j) lpj(i, 0, j) #define rlp(i, j) rlpj(i, j, 0) #define inpv(a,n) lp(i, n) inp(a[i]) #define inpvv(a,n) lp(i, n)lp(j, n) inp(a[i][j]); #define outv(a,n) lp(i, n) { if(i != 0 ){ cout << " ";} cout << a[i]; } cout<<nl; #define outvv(a,n) lp(i, n){ lp(j, n){ if(j != 0 ){ cout << " ";} cout << a[i][j]; } cout<<nl; } #define outy() out("YES") #define outn() out("NO") #define all(cont) cont.begin(), cont.end() #define rall(cont) cont.end(), cont.begin() #define each(it, l) for (auto it = l.begin(); it != l.end(); it++) #define IN(A, B, C) assert( B <= A && A <= C) #define CNT(a, x) count(all(a), x) #define mpr make_pair #define pbk push_back #define INF (int)1e9 #define EPS 1e-9 #define PI 3.1415926535897932384626433832795 // #define MOD 1000000007 #define read(type) readInt<type>() #define clk_start() time_req = clock(); #define clk_end() cout << "time taken to solve (in seconds) = "; outf((float)(clock() - time_req)/(float)CLOCKS_PER_SEC, 6); #define ROTL(a, i) rotate(a.begin(), a.begin() + i, a.end()) #define ROTR(a, i) rotate(a.begin(), a.begin() + a.size() - i, a.end()) const double pi = acos(-1.0); typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<unsigned long long int> vi64; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<vi> vvi; typedef map<int, int> mpii; typedef set<int> seti; typedef multiset<int> mseti; typedef long int int32; typedef unsigned long int uint32; typedef long long int int64; typedef unsigned long long int uint64; /****** Template of some basic operations *****/ template<typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template<typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } /**********************************************/ /****** Template of Fast I/O Methods *********/ template <typename T> inline void write(T x) { int i = 20; char buf[21]; // buf[10] = 0; buf[20] = '\n'; do { buf[--i] = x % 10 + '0'; x /= 10; } while (x); do { putchar(buf[i]); } while (buf[i++] != '\n'); } template <typename T> inline T readInt() { T n = 0, s = 1; char p = getchar(); if (p == '-') s = -1; while ((p < '0' || p > '9') && p != EOF && p != '-') p = getchar(); if (p == '-') s = -1, p = getchar(); while (p >= '0' && p <= '9') { n = (n << 3) + (n << 1) + (p - '0'); p = getchar(); } return n * s; } /************************************/ /*/---------------------------RNG----------------------/*/ mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int64_t random_long(long long l = LLONG_MIN, long long r = LLONG_MAX) { uniform_int_distribution<int64_t> generator(l, r); return generator(rng); } struct custom_hash { // Credits: https://codeforces.com/blog/entry/62393 static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } template<typename L, typename R> size_t operator()(pair<L, R> const& Y) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(Y.first * 31 + Y.second + FIXED_RANDOM); } }; /*/-----------------------Modular Arithmetic---------------/*/ const int mod = 1e9 + 7; template<const int MOD> struct modular_int { int x; static vector<int> inverse_list ; const static int inverse_limit; const static bool is_prime; modular_int() { x = 0; } template<typename T> modular_int(const T z) { x = (z % MOD); if (x < 0) x += MOD; } modular_int(const modular_int<MOD>* z) { x = z->x; } modular_int(const modular_int<MOD>& z) { x = z.x; } modular_int operator - (const modular_int<MOD>& m) const { modular_int<MOD> U; U.x = x - m.x; if (U.x < 0) U.x += MOD; return U; } modular_int operator + (const modular_int<MOD>& m) const { modular_int<MOD> U; U.x = x + m.x; if (U.x >= MOD) U.x -= MOD; return U; } modular_int& operator -= (const modular_int<MOD>& m) { x -= m.x; if (x < 0) x += MOD; return *this; } modular_int& operator += (const modular_int<MOD>& m) { x += m.x; if (x >= MOD) x -= MOD; return *this; } modular_int operator * (const modular_int<MOD>& m) const { modular_int<MOD> U; U.x = (x * 1ull * m.x) % MOD; return U; } modular_int& operator *= (const modular_int<MOD>& m) { x = (x * 1ull * m.x) % MOD; return *this; } template<typename T> friend modular_int operator + (const T &l, const modular_int<MOD>& p) { return (modular_int<MOD>(l) + p); } template<typename T> friend modular_int operator - (const T &l, const modular_int<MOD>& p) { return (modular_int<MOD>(l) - p); } template<typename T> friend modular_int operator * (const T &l, const modular_int<MOD>& p) { return (modular_int<MOD>(l) * p); } template<typename T> friend modular_int operator / (const T &l, const modular_int<MOD>& p) { return (modular_int<MOD>(l) / p); } int value() const { return x; } modular_int operator ^ (const modular_int<MOD>& cpower) const { modular_int<MOD> ans; ans.x = 1; modular_int<MOD> curr(this); int power = cpower.x; if (curr.x <= 1) { if (power == 0) curr.x = 1; return curr; } while ( power > 0) { if (power & 1) { ans *= curr; } power >>= 1; if (power) curr *= curr; } return ans; } modular_int operator ^ (long long power) const { modular_int<MOD> ans; ans.x = 1; modular_int<MOD> curr(this); if (curr.x <= 1) { if (power == 0) curr.x = 1; return curr; } // Prime Mods if (power >= MOD && is_prime) { power %= (MOD - 1); } while ( power > 0) { if (power & 1) { ans *= curr; } power >>= 1; if (power) curr *= curr; } return ans; } modular_int operator ^ (int power) const { modular_int<MOD> ans; ans.x = 1; modular_int<MOD> curr(this); if (curr.x <= 1) { if (power == 0) curr.x = 1; return curr; } while ( power > 0) { if (power & 1) { ans *= curr; } power >>= 1; if (power) curr *= curr; } return ans; } template<typename T> modular_int& operator ^= (T power) { modular_int<MOD> res = (*this)^power; x = res.x; return *this; } template<typename T> modular_int pow(T x) { return (*this)^x; } pair<long long, long long> gcd(const int a, const int b) const { if (b == 0) return {1, 0}; pair<long long, long long> c = gcd(b , a % b); return { c.second , c.first - (a / b)*c.second}; } inline void init_inverse_list() const { vector<int>& dp = modular_int<MOD>::inverse_list; dp.resize(modular_int<MOD>::inverse_limit + 1); int n = modular_int<MOD>::inverse_limit; dp[0] = 1; if (n > 1) dp[1] = 1; for (int i = 2; i <= n; ++i) { dp[i] = (dp[MOD % i] * 1ull * (MOD - MOD / i)) % MOD; } } modular_int<MOD> get_inv() const { if (modular_int<MOD>::inverse_list.size() < modular_int<MOD>::inverse_limit + 1) init_inverse_list(); if (this->x <= modular_int<MOD>::inverse_limit) { return modular_int<MOD>::inverse_list[this->x]; } pair<long long, long long> G = gcd(this->x, MOD); return modular_int<MOD>(G.first); } modular_int<MOD> operator - () const { modular_int<MOD> v(0); v -= (*this); return v; } modular_int operator / (const modular_int<MOD>& m) const { modular_int<MOD> U(this); U *= m.get_inv(); return U; } modular_int& operator /= (const modular_int<MOD>& m) { (*this) *= m.get_inv(); return *this; } bool operator==(const modular_int<MOD>& m) const { return x == m.x; } bool operator < (const modular_int<MOD>& m) const { return x < m.x; } template<typename T> bool operator == (const T& m) const { return (*this) == (modular_int<MOD>(m)); } template<typename T> bool operator < (const T& m) const { return x < (modular_int<MOD>(m)).x; } template<typename T> bool operator > (const T& m) const { return x > (modular_int<MOD>(m)).x; } template<typename T> friend bool operator == (const T& x, const modular_int<MOD>& m) { return (modular_int<MOD>(x)).x == m.x; } template<typename T> friend bool operator < (const T& x, const modular_int<MOD>& m) { return (modular_int<MOD>(x)).x < m.x; } template<typename T> friend bool operator > (const T& x, const modular_int<MOD>& m) { return (modular_int<MOD>(x)).x > m.x; } friend istream& operator >> (istream& is, modular_int<MOD>& p) { int64_t val; is >> val; p.x = (val % MOD); if (p.x < 0) p.x += MOD; return is; } friend ostream& operator << (ostream& os, const modular_int<MOD>& p) {return os << p.x;} }; using mint = modular_int<mod>; template<const int MOD> vector<int> modular_int<MOD>::inverse_list ; template<const int MOD> const int modular_int<MOD>::inverse_limit = -1; template<const int MOD> const bool modular_int<MOD>::is_prime = true; template<> //-> useful if computing inverse fact = i_f[i-1] / i; const int modular_int<mod>::inverse_limit = 1000; /******* Debugging Class Template *******/ #ifdef DEBUG #define debug(args...) (Debugger()) , args #define dbg(var1) cerr<<#var1<<" = "<<(var1)<<nl; #define dbg2(var1,var2) cerr<<#var1<<" = "<<(var1)<<", "<<#var2<<" = "<<(var2)<<nl; #define dbg3(var1,var2,var3) cerr<<#var1<<" = "<<(var1)<<", "<<#var2<<" = "<<(var2)<<", "<<#var3<<" = "<<(var3)<<nl; #define dbg4(var1,var2,var3,var4) cerr<<#var1<<" = "<<(var1)<<", "<<#var2<<" = "<<(var2)<<", "<<#var3<<" = "<<(var3)<<", "<<#var4<<" = "<<(var4)<<nl; class Debugger { public: Debugger(const std::string& _separator = " - ") : first(true), separator(_separator) {} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if (!first) std: cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std: cerr << nl;} private: bool first; std::string separator; }; #else #define debug(args...) // Just strip off all debug tokens #define dbg(args...) // Just strip off all debug tokens #define dbg2(args...) // Just strip off all debug tokens #define dbg3(args...) // Just strip off all debug tokens #define dbg4(args...) // Just strip off all debug tokens #endif /************** Macros ****************/ #ifndef ONLINE_JUDGE #define ONLINE_JUDGE #endif /* ONLINE_JUDGE */ // #define SUBLIME_TEXT // #define DEBUG // #define CLOCK #define MULT_TC /** Conditional variables/ constants **/ #ifdef CLOCK clock_t time_req; #endif /* CLOCK */ /***** Global variables/constants *****/ const int NMAX = 3e5; uint64 n, m; /******* User-defined Functions *******/ /**************************************/ void solve() { inpp(n, m); if (n == m) { outt(0, 0); return; } if (n > m) { swap(n, m); } uint64 diff = m - n; if (diff == n) { outt(diff, 0); } // else if (n < diff) { // if (diff - n > n) { // outt(diff, n); // } // else { // outt(diff, diff - n); // } // } else { outt(diff, min(n % diff, diff - (n % diff))); } return; } /********** Main() function **********/ int main() { #if !defined(ONLINE_JUDGE) || defined(SUBLIME_TEXT) freopen("../IO/input.txt", "r", stdin); freopen("../IO/output.txt", "w", stdout); freopen("../IO/log.txt", "w", stderr); #endif std::ios::sync_with_stdio(false); cin.tie(0); #ifdef MULT_TC int tc; inp(tc); while (tc--) { #endif /* MULT_TC */ #ifdef CLOCK clk_start(); #endif /* CLOCK */ solve(); #ifdef CLOCK clk_end(); #endif /* CLOCK */ #ifdef MULT_TC } #endif /* MULT_TC */ return 0; } /******** Main() Ends Here *************/
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.StringTokenizer; public class TaskA { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); Solver sol = new Solver(); int testCount = 1; testCount = in.nextInt(); for (int i = 1; i <= testCount; ++i) { sol.solve(in, out); } out.close(); } public static class Solver { public void solve(InputReader in, PrintWriter out) { long a = in.nextLong(), b = in.nextLong(); if (a == b) { out.println(0 + " " + 0); return ; } long dff = Math.max(a, b) - Math.min(a, b); //if (gcd(a, b) == dff) { out.println(dff + " " + 0); return; } long fir = dff - (a % dff), sec = a % dff; out.println(dff + " " + Math.min(fir, sec)); } private long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } } public static class InputReader{ private BufferedReader in; private StringTokenizer stk; public InputReader(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); this.stk = null; } public String next() { while (stk == null || !stk.hasMoreTokens()) { try { this.stk = new StringTokenizer(in.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return stk.nextToken(); } public boolean hasNext() { while (stk == null || !stk.hasMoreTokens()) { String str = null; try { str = in.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (str == null) { return false; } this.stk = new StringTokenizer(str); } return true; } public int nextInt(){ return Integer.parseInt(next()); } public 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.*; import java.lang.*; import java.util.*; public class answer { static Scanner s=new Scanner(System.in); static long mod=1000000007; // static boolean[] visited; // static ArrayList<Integer>[] edges; // static long[] ans; // static long[] weights; public static void main(String[] args) { int testcases=s.nextInt(); while(testcases-->0) { long a=s.nextLong(); long b=s.nextLong(); long dif=Math.abs(a-b); if(dif==0) { System.out.println(0+" "+0); continue; } if(a<b) { long temp=a; a=b; b=temp; } long mod=b%dif; System.out.println(dif +" "+ Math.min(dif-mod,mod)); } } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static long pow(long a,long b){ long ans=1; if(a==0)return 1; for(int i=0;i<b;i++) { ans*=a; ans%=mod; } return 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 ll long long #define all( x ) x.begin() , x.end() using namespace std ; void solve() { // If there is only one test case make t = 1 ll a , b ; cin >> a >> b ; ll req = abs( a - b ) ; ll moves = 1e18 ; if( req ) { ll x = min( a , b ) % req ; if( req - x > 0 ) moves = x ; moves = min( moves , req - x ) ; } else moves = 0 ; cout << req << " " << moves << endl ; } int main() { ios_base :: sync_with_stdio( false ) ; cin.tie( NULL ) ; 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
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) input = lambda: sys.stdin.readline().rstrip("\r\n") ########################################################## from collections import Counter, defaultdict import math import heapq for _ in range(int(input())): #n=int(input()) a,b=map(int, input().split()) #arr =list(map(int, input().split())) e=abs(a-b) if a==b: print(0, 0) else: if a>=e and b>=e: if (a%e==0 and b%e==0): print(e, 0) else: x = a //e y1 = e * x l = abs(a - y1) y = b / e x +=1 x1 = e * x d = abs(x1 - a) c1 = min(a, b) d = min(d, c1) d = min(l, d) print(e, d) else: x = a //e y1 = e * x l = abs(a - y1) y = b //e x+=1 x1 = e * x d = abs(x1 - a) c1 = min(a, b) d = min(d, c1) d = min(l, d) print(e, d) '''v=max(a, b) - min(a, b) m=math.ceil(a/v) print(v, min(min(a,b),(v*m-a),a-(m-1)*v))'''
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.PrintStream; import java.util.Arrays; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; public class A { public static void main(String[] args) throws Throwable { Thread thread = new Thread (null, new TaskAdapter (), "", 1 << 29); thread.start (); thread.join (); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput (inputStream); FastOutput out = new FastOutput (outputStream); Solution solver = new Solution (); solver.solve (1, in, out); out.close (); } } static class Solution { static final Debug debug = new Debug (true); public void solve(int testNumber, FastInput in, FastOutput out) { int test= in.ri (); while (test-->0){ long a= in.rl (); long b= in.rl (); if(a==b) out.prtl ("0 0"); else{ long x=Math.abs (a-b); if(gcd(a,b)==x){ out.prtl (x+" "+0); } else{ long max=Math.max (a,b); long ans=Math.min (max%x,x-max%x); out.prtl (x+" "+ans); } } } } public static long gcd(long x,long y){ return y==0?x:gcd (y,x%y); } } 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(String c) { cache.append (c); afterWrite (); return this; } public FastOutput println(String c) { return append (c).println (); } public FastOutput println() { return append ('\n'); } final <T> void prt(T a) { append (a + " "); } final <T> void prtl(T a) { append (a + "\n"); } public FastOutput flush() { try { 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 (); } public FastOutput printf(String format, Object... args) { return append (String.format (format, args)); } 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; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } } static class FastInput { private final InputStream is; private StringBuilder defaultStringBuf = new StringBuilder (1 << 13); private byte[] buf = new byte[1 << 13]; private int bufLen; private SpaceCharFilter filter; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } public void populate(int[] data) { for (int i = 0; i < data.length; i++) { data[i] = readInt (); } } public void populate(long[] data) { for (int i = 0; i < data.length; i++) { data[i] = readLong (); } } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read (buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read (); } } public String next() { return readString (); } public int ri() { return readInt (); } public int readInt() { boolean rev = false; skipBlank (); if (next == '+' || next == '-') { rev = next == '-'; next = read (); } int val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read (); } return rev ? val : -val; } public long readLong() { boolean rev = false; skipBlank (); if (next == '+' || next == '-') { rev = next == '-'; next = read (); } long val = 0L; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read (); } return rev ? val : -val; } public long rl() { return readLong (); } public String readString(StringBuilder builder) { skipBlank (); while (next > 32) { builder.append ((char) next); next = read (); } return builder.toString (); } public String readString() { defaultStringBuf.setLength (0); return readString (defaultStringBuf); } public int rs(char[] data, int offset) { return readString (data, offset); } public int rs(char[] data) { return rs (data, 0); } public int readString(char[] data, int offset) { skipBlank (); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read (); } return offset - originalOffset; } public char rc() { return readChar (); } public char readChar() { skipBlank (); char c = (char) next; next = read (); return c; } public double rd() { return nextDouble (); } public double nextDouble() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } double res = 0; while (!isSpaceChar (c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow (10, readInt ()); if (c < '0' || c > '9') throw new InputMismatchException (); res *= 10; res += c - '0'; c = read (); } if (c == '.') { c = read (); double m = 1; while (!isSpaceChar (c)) { if (c == 'e' || c == 'E') return res * Math.pow (10, readInt ()); if (c < '0' || c > '9') throw new InputMismatchException (); m /= 10; res += (c - '0') * m; c = read (); } } return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar (c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class Debug { private boolean offline; private PrintStream out = System.err; static int[] empty = new int[0]; public Debug(boolean enable) { offline = enable && System.getSecurityManager () == null; } public Debug debug(String name, Object x) { return debug (name, x, empty); } public Debug debug(String name, Object x, int... indexes) { if (offline) { if (x == null || !x.getClass ().isArray ()) { out.append (name); for (int i : indexes) { out.printf ("[%d]", i); } out.append ("=").append ("" + x); out.println (); } else { indexes = Arrays.copyOf (indexes, indexes.length + 1); if (x instanceof byte[]) { byte[] arr = (byte[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof short[]) { short[] arr = (short[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof boolean[]) { boolean[] arr = (boolean[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof char[]) { char[] arr = (char[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof int[]) { int[] arr = (int[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof float[]) { float[] arr = (float[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof double[]) { double[] arr = (double[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else if (x instanceof long[]) { long[] arr = (long[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } else { Object[] arr = (Object[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug (name, arr[i], indexes); } } } } return this; } } }
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.*; public class A730A { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); while(t-->0){ long a = scan.nextLong(); long b = scan.nextLong(); if(a==b){ System.out.println("0 0"); }else{ long left = Math.abs(a-b); long div = a/left; if(a%left!=0)div++; long mult = div*left; long right = mult-a; div = a/left; mult = div*left; right = Math.min(right,a-mult); System.out.println(left+" "+right); } } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { 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 long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
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.*; public class ExcitingBets { static long gcd(long a,long b) { if(a==0) return b; return gcd(b%a,b); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { long a=sc.nextLong(); long b=sc.nextLong(); long ans=gcd(a,b); if(a==b) System.out.println("0 0"); else if(Math.abs(a-b)==1) System.out.println("1 0"); else { long min=Math.min(a, b); long max=Math.max(a, b); long minus=Math.abs(a-b); long plus=minus-(max%minus); System.out.println(minus+" "+(Math.min(max%minus, plus))); } } } }
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.*; public class A { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); String input[]; StringBuilder sb = new StringBuilder(""); long a, b, ans; long l1, l2, a1, a2, b1, b2, diff; while(t-->0){ input = br.readLine().trim().split(" "); a = Long.parseLong(input[0]); b = Long.parseLong(input[1]); if(a==b){ sb.append("0 0\n"); continue; } ans = Math.abs(a-b); l1 = a/ans; l2 = b/ans; a1 = ans*l1; a2 = ans*(l1+1); b1 = ans*l2; b2 = ans*(l2+1); a1 = Math.abs(a1-a); a2 = Math.abs(a2-a); b1 = Math.abs(b1-a); b2 = Math.abs(b2-a); diff = Math.min(Math.min(a1, a2), Math.min(a1, a2)); sb.append(ans+" "+diff+"\n"); } System.out.println(sb); } }
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() { #ifndef ONLINE_JUDGE //input from input.txt freopen("input.txt", "r", stdin); //output on output.txt freopen("output.txt", "w", stdout); #endif ll tc; cin>>tc; for(int i=0;i<tc;i++) { ll a,b; cin>>a>>b; ll temp=abs(b-a); if(a==b) { cout<<0<<" "<<0<<endl; continue; } else { ll sel=max(a,b); ll low=sel/temp; ll high=((low+1)*temp)-sel; cout<<temp<<" "<<min(sel%temp,high)<<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 math t=int(input()) for i in range(t): a,b=map(int,input().split()) d=abs(a-b) if a==b: print("0 0") else: print(d,min(b%d,d-b%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 math def solve(): a,b = map(int,input().split()) if a>b: a,b = b,a x = abs(b-a) if a==b: print(0,0) else: if a==0: print(x,0) else: print(x,min(a%x,x-a%x)) t = int(input()) for _ in range(t): 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
import math for i in range(int(input())): a,b = map(int,input().split()) d =abs(a-b) f = min(a,b) if d==0 or d==1: e = 0 else: g= math.ceil(f/d) l = f//d e =min((d*g)-f,f-(l*d)) print(d,e)
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> #define ll long long #define pb push_back #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr) ,cout.tie(nullptr); const int mod = 1000000007; using namespace std; void solve(){ ll a,b; cin>>a>>b; if(a==b){ cout<<0<<" "<<0<<endl; return ; } if(a>b) swap(a,b); ll gcd = abs(b-a); ll k = a%gcd; ll p= gcd-k; cout<<gcd<<" "<<min(k,p)<<endl; } int main(){ fast ll t; 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
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class practice { 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 long gcd(long n1, long n2) { if (n2 == 0) { return n1; } if(n1==0) { return n2;} return gcd(n2, n1 % n2); } public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); for (int r = 0; r < t; r++) { long a = sc.nextLong(); long b = sc.nextLong(); long min = (long) Math.min(a, b); long max = (long) Math.max(a, b); long c = max - min; if (c == 0) { System.out.println("0 0"); } else { long a1 = min, count1 = 0, count2 = 0; long m = c - (a1 % c); a1 += m; count1 += m; long a2 = min; long m2 = (a2 % c); a2 -= m2; count2 += m2; long b1 = max + count1; long b2 = max - count2; if (count2 < count1) { System.out.println(gcd(a2, b2) + " " + count2); } else { System.out.println(gcd(a1, b1) + " " + count1); } } } } }
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 int long long void solve(){ int a,b; cin>>a>>b; if(a==b){ cout<<0<<' '<<0<<'\n'; return; } if(b<a)swap(a,b); set<int>facts; int diff=b-a; int ans=diff; int moves=b/diff; if(b%ans==0){ cout<<ans<<" "<<0<<'\n'; return; }else{ cout<<ans<<" "<<min(b%diff,llabs(b-diff*(moves+1)))<<'\n'; } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); int tt; cin>>tt; while(tt--){ 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 java.util.*; public class p1543A { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scn=new Scanner(System.in); int t=scn.nextInt(); for(int i=0;i<t;i++) { long a=scn.nextLong(); long b=scn.nextLong(); if(a==b) { System.out.println(0+" "+0); } else { long a1=Math.min(a, b); long b1=Math.max(a, b); long c=b1-a1; long r=b1%c; System.out.println(c+" "+Math.min(r, c-r)); } } } }
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 t = int(input()) for i in range(0,t): a, b = map(int, input().split()) maxim = max(a,b) minim = min(a,b) a = maxim b = minim if ((a-b) == 1) or ((b-a) == 1): print("1 0") elif a==b: print("0 0") else: #gcd(a,b) == gcd(a-b, b) x = a-b q = b//x moves = min(b%x, (q+1)*x - b) print(str(x)+" "+str(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
#include<bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" const double pi = acos(-1); #define pb push_back #define LL unsigned long long int static dp[1005][1005]; #define RADHE KRISHNA const int MOD = 1e9 + 7; const int N = 1e6 + 2; // int lcs(string s , string t , int n , int m){ // //base case // if(n == 0 || m == 0){ // return 0; // } // if(dp[n][m] != -1){ // return dp[n][m]; // } // if(s[n-1] == t[m - 1]){ // return (dp[n][m] = 1 + lcs(s , t , n - 1 , m - 1)); // } // else{ // return dp[n][m] = max(lcs(s ,t ,n-1 ,m) ,lcs(s ,t ,n ,m - 1) ); // } // } // ll Pow(ll n, ll p, ll m) { // if(!p) return 1; // else if(p & 1) return (n * Pow(n, p-1, m)) % m; // else { // ll v = Pow(n, p/2, m); // return (v*v) % m; // } // } // bool isprime(ll n){ // if(n<2) return false; // for(ll i=2;i*i*i<=n;++i) if(n%i==0) return false; // for(int it=0;it<1e5;++it){ // ll i = rand()%(n-1)+1; // if(__gcd(i,n)!=1) return false; // if(Pow(i,n-1,n)!=1) return false; // } // return true; // } // void check(ll q){ // vector<int>v; // for(int i = 2 ; i*i <= q; i++){ // if(q % i == 0){ // v.push_back(i); // if(i != q/i){ // v.push_back(q/i); // } // } // } // for(auto x : v){ // cout<<x<<" "; // } // } // set<int>s; // void SieveOfEratosthenes(ll n) // { // bool prime[n + 2]; // memset(prime, true, sizeof(prime)); // for (int p = 2; p * p <= n+1; p++) // { // if (prime[p] == true) // { // for (ll i = p * p; i <= n+1; i += p) // prime[i] = false; // } // } // for (ll p = 2; p <= n+1; p++){ // if (prime[p]){ // s.insert(p); // } // } // } // bool check(int x){ // int y = sqrt(x); // return y*y == x; // } //ll fac[200002]; // bool rev(int i){ // return (s[i] == t[s.size() - i - 1] && s[s.size() - i - 1] == t[i]); // } //queue<pair<pair<int ,int>, int>>q; // bool valid(int x ,int y){ // if(x > 0 && x <= n && y > 0 && y <= m){ // return true; // } // else{ // false; // } // } // bool checkk(int x ,int y){ // return x >= 0 && x < n && y >= 0 && y < m; // } //vector<int>adj[200001]; //ll vis[200001]; //bool is_cycle; //ll min_cost; //int dx1[] = {1,1,1,-1,-1,-1,0,0}; //int dy1[] = {1,0,-1, 1, 0, -1, 1, -1}; // int dx[4]={-1,0,0,1}; // int dy[4]={0,1,-1,0}; // char a[101][101]; // bool vis[101][101]; // int n ,m; // bool valid(int x , int y){ // if(x < 0 || x >= n || y < 0 || y >= m || a[x][y] == '.' || vis[x][y]==true){ // return false; // } // else{ // return true; // } // } // void dfs(ll x ,ll y){ // vis[x][y] = true; // for(int i = 0 ; i < 4; i++){ // int new_x = x + dx[i]; // int new_y = y + dy[i]; // if(valid(new_x , new_y)){ // dfs(new_x , new_y); // } // } // } int prime[N+1]; vector<int>v; void seive(){ memset(prime , true , sizeof(prime)); prime[0] = false; prime[1] = false; for(int i = 2; i <= N; i++){ if(prime[i]){ v.push_back(i); for(int j = 2*i; j <= N; j += i){ prime[j] = false; } } } } //int score[12] = {15 , 12 , 10 ,9 ,8, 7, 6, 5, 4, 3 ,2 ,1}; void solve(){ // cin>>n>>m; // cin>>ch; // set<char>s; // int new_x , new_y; // char mat[n+1][m+1]; // for(int i = 0 ; i < n ;i++){ // for(int j = 0 ;j < m ;j++){ // cin>>mat[i][j]; // } // } // for(int i = 0; i < n; i++){ // for(int j = 0; j < m; j++) { // if(mat[i][j] == ch){ // for(int k = 0; k < 4; k++){ // new_x = i + dx[k]; // new_y = j + dy[k]; // if(checkk(new_x,new_y) && mat[new_x][new_y] != '.' && mat[new_x][new_y] != ch){ // s.insert(mat[new_x][new_y]); // } // } // } // } // } // cout<<s.size()<<endl; // int a[4]; // for(int i = 0 ; i < 4; i++){ // cin>>a[i]; // } // sort(a, a+4); // if(a[0] + a[1] > a[2] || a[1]+a[2] > a[3]){ // cout<<"TRIANGLE"<<endl; // } // else if(a[0] + a[1] >= a[2] || a[1] + a[2] >= a[3]){ // cout<<"SEGMENT"<<endl; // } // else{ // cout<<"IMPOSSIBLE"<<endl; // } // int n; // cin>>n; // for(int i = 1; i <= n; i++){ // vis[i] = 0; // } // for(int i = 1 ; i <= n ;i++){ // cin>>a[i]; // } // for(int i = 1 ; i <= n ;i++){ // cin>>b[i]; // p[a[i]] = b[i]; // } // int ans = 0; // for(int i = 1; i <= n; i++){ // // if(vis[i] || p[i] == i){ // // continue; // // } // if(!vis[i]){ // ans++; // int tmp = i; // while(!vis[tmp]){ // vis[tmp] = 1; // tmp = p[tmp]; // } // } // } // cout<<Pow(2 , ans , MOD)<<endl; // ll n ,m; // cin>>n>>m; // int ans = INT_MIN , num , cnt , tmp; // for(int i = n ; i <= m ; i++){ // tmp = i, cnt = 0; // while(tmp){ // if(tmp & 1){ // cnt++; // } // tmp >>= 1ll; // } // if(cnt > ans){ // ans = cnt; // num = i; // } // } // cout<<num<<endl; ll a,b; cin>>a>>b; if(a==b){ cout<<0<<" "<<0<<endl; } else if(a<b){ ll val=b-a; cout<<val<<" "<<min(a%val,val-a%val)<<endl; } else{ ll val=a-b; cout<<val<<" "<<min(b%val,val-b%val)<<endl; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); int t ; cin>>t; while(t--){ // int n ,m; // cin>>n>>m; // string s ,t; // cin>>s>>t; // memset(dp , -1 , sizeof(dp)); // cout<< n + m - lcs(s , t ,n ,m)<<endl; 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
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) input = lambda: sys.stdin.readline().rstrip("\r\n") ####################################### for t in range(int(input())): a,b=map(int,input().split()) if a==b: print(0,0) continue a,b=max(a,b),min(a,b) ans=a-b if a%ans==0: c=0 else: c=min(a-ans*(a//ans),ans*(a//ans+1)-a) print(ans,c)
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 static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.ceil; import static java.lang.Math.floor; import java.io.*; import java.math.*; import java.util.*; public class apples { /*---------------------------------------CODE STARTS HERE-------------------------*/ public static void main(String[] args) { FastReader x = new FastReader(); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { long a=x.nextLong(); long b=x.nextLong(); if(a==b) { str.append("0 0"); } else if(a==0||b==0) { long d=max(a,b); str.append(d+" "+0); } else { long d=abs(a-b); long r=a%d; if(r>d/2) { r=d-r; } str.append(d+" "+r); } str.append("\n"); t--; } out.println(str); out.flush(); } /*--------------------------------------------FAST I/O--------------------------------*/ 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; } char nextchar() { char ch = ' '; try { ch = (char) br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } } /*--------------------------------------------BOILER PLATE---------------------------*/ static int[] readarr(int n) { int arr[] = new int[n]; FastReader x = new FastReader(); for (int i = 0; i < n; i++) { arr[i] = x.nextInt(); } return arr; } static int[] sort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] revsort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al, Comparator.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] gcd(int a, int b, int ar[]) { if (b == 0) { ar[0] = a; ar[1] = 1; ar[2] = 0; return ar; } ar = gcd(b, a % b, ar); int t = ar[1]; ar[1] = ar[2]; ar[2] = t - (a / b) * ar[2]; return ar; } static boolean[] esieve(int n) { boolean p[] = new boolean[n + 1]; Arrays.fill(p, true); for (int i = 2; i * i <= n; i++) { if (p[i] == true) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } static ArrayList<Integer> primes(int n) { boolean p[] = new boolean[n + 1]; ArrayList<Integer> al = new ArrayList<>(); Arrays.fill(p, true); int i = 0; for (i = 2; i * i <= n; i++) { if (p[i] == true) { al.add(i); for (int j = i * i; j <= n; j += i) { p[j] = false; } } } for (i = i; i <= n; i++) { if (p[i] == true) { al.add(i); } } return al; } static int etf(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res /= i; res *= (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res /= n; res *= (n - 1); } return res; } }
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 case in range(t): a,b = map(int, input().split()) if a==b: print(0,0) continue ex = abs(a-b) moves = min(a%ex, ex - a%ex) print(ex, 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 for _ in range(int(input())): a,b = map(int,input().split()) flag = True if a==b: flag = 'Red' else: if a<b: ans = b-a x = a%ans y = ans - a%ans c = min(x,y) else: ans = a-b x = b%ans y = ans- b%ans c = min(x,y) if flag== 'Red': print(0,0) else: print(ans,c)
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.io.*; 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; } } public static void main (String[] args) throws java.lang.Exception { FastReader s = new FastReader(); int testcases = s.nextInt(); while(testcases-- > 0) { long a = s.nextLong(); long b = s.nextLong(); if(a == b){ System.out.println(0 + " " + 0); continue; } /*else if(a == 0 || b == 0) System.out.println((a == 0? b : a) + " " + 0); else if(Math.abs(a - b) == 1)System.out.println(1 + " " + 0); else { long min = Math.min(a, b); long ab = Math.abs(a - b); if(ab >= min) { System.out.println(ab + " " + (ab - min)); } else { //long i = 0; /*while(true) { if((a + i) % ab == 0 && (b + i) % ab == 0) { System.out.println(ab + " " + i); break;} if((a-i > 0) && (b-i > 0) && ((a - i) % ab == 0) && ((b - i) % ab == 0)) { System.out.println(ab + " " + i); break;} i++; } } }*/ long moves = 0; long mi = Math.min(a, b); long ma = Math.max(a, b); long ex = ma - mi; if(mi % ex == 0) moves = 0; else moves = Math.min(ex - mi % ex, mi % ex); System.out.println(ex + " " + 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 int int64_t void solve(){ int a,b; cin >> a >> b; if(a == b){ cout << "0 0\n"; return; } if(a > b) swap(a,b); int ans = b - a; if(ans == a) cout << ans << " " << 0 << '\n'; else{ cout << ans << " " << min(a%ans,ans-a%ans) << '\n'; } } signed main(){ ios_base::sync_with_stdio(false);cin.tie(NULL); int t=1; 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Practice { public static long mod = (long) Math.pow(10, 9) + 7; public static long mod2 = 998244353; public static int tt = 1; public static ArrayList<Integer> prime; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String[] s1 = br.readLine().split(" "); long a = Long.valueOf(s1[0]); long b = Long.valueOf(s1[1]); if (a == b) { pw.println(0 + " " + 0); } else { long temp1 = Math.min(a, b); long temp2 = Math.max(a, b); long x = temp2 - temp1; long tt = temp1 / x; long y = Math.min(temp1 - tt * x, (tt + 1) * x - temp1); pw.println(x + " " + y); } } pw.close(); } } // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // }
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
#!/usr/bin/env python #from __future__ import division, print_function import math import os import sys #from fractions import * from sys import * from decimal import * from io import BytesIO, IOBase from itertools import accumulate,combinations,permutations,combinations_with_replacement,product from collections import * #import timeit,time #sys.setrecursionlimit(10**5) #from sortedcontainers import * M = 10 ** 9 + 7 import heapq from bisect import * from functools import lru_cache from queue import PriorityQueue # print(math.factorial(5)) if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip # sys.setrecursionlimit(10**6) # 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") def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def out(var): sys.stdout.write(str(var)) # for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) def inpu(): return int(inp()) # ----------------------------------------------------------------- """ def regularbracket(t): p = 0 for i in t: if i == "(": p += 1 else: p -= 1 if p < 0: return False else: if p > 0: return False else: return True # ------------------------------------------------- def binarySearchcount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = ((right + left) // 2) # Check if middle element is # less than or equal to key if (arr[mid] <= key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count #--------------------------------------------------binery search def binarySearch(arr, n, key): left = 0 right = n - 1 while (left <= right): mid = ((right + left) // 2) if arr[mid]==key: return mid if (arr[mid] <= key): left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return -1 #-------------------------------------------------ternary search def ternarysearch(arr,n,key): l,r=0,n-1 while(l<=r): mid = (-l+r)//3 + l mid2 = mid + (-l+r)//3 if arr[mid]==key: return mid if arr[mid2]==key: return mid2 if arr[mid]>key: r=mid-1 elif arr[mid2]<key: l=mid2+1 else: l=mid+1 r=mid2-1 return -1 # ------------------------------reverse string(pallindrome) def reverse1(string): pp = "" for i in string[::-1]: pp += i if pp == string: return True return False # --------------------------------reverse list(paindrome) def reverse2(list1): l = [] for i in list1[::-1]: l.append(i) if l == list1: return True return False def mex(list1): l=0 h=len(list1)-1 while(l<=h): mid=(l+h)//2 if list1[mid]==mid: l=mid+1 else: h=mid-1 return l def sumofdigits(n): n = str(n) s1 = 0 for i in n: s1 += int(i) return s1 def perfect_square(n): s = math.sqrt(n) if s == int(s): return True return False # -----------------------------roman def roman_number(x): if x > 15999: return value = [5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] symbol = ["F", "MF", "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] roman = "" i = 0 while x > 0: div = x // value[i] x = x % value[i] while div: roman += symbol[i] div -= 1 i += 1 return roman def soretd(s): for i in range(1, len(s)): if s[i - 1] > s[i]: return False return True # print(soretd("1")) # --------------------------- def countRhombi(h, w): ct = 0 for i in range(2, h + 1, 2): for j in range(2, w + 1, 2): ct += (h - i + 1) * (w - j + 1) return ct def countrhombi2(h, w): return ((h * h) // 4) * ((w * w) // 4) # --------------------------------- def binpow(a, b): if b == 0: return 1 else: res = binpow(a, b // 2) if b % 2 != 0: return res * res * a else: return res * res # ------------------------------------------------------- def binpowmodulus(a, b, M): if b==1: return a%M if b==0: return 1 if b%2==0: ans=binpowmodulus(a,b//2,M) return (ans*ans)%(M) else: ans=binpowmodulus(a,(b-1)//2,M) return ((ans*a)%M * ans)%M # ------------------------------------------------------------- def coprime_to_n(n): result = n i = 2 while (i * i <= n): if (n % i == 0): while (n % i == 0): n //= i result -= result // i i += 1 if (n > 1): result -= result // n return result def luckynumwithequalnumberoffourandseven(x,n,a): if x >= n and str(x).count("4") == str(x).count("7"): a.append(x) else: if x < 1e12: luckynumwithequalnumberoffourandseven(x * 10 + 4,n,a) luckynumwithequalnumberoffourandseven(x * 10 + 7,n,a) return a #---------------------- def luckynum(x,l,r,a): if x>=l and x<=r: a.append(x) if x>r: a.append(x) return a if x < 1e10: luckynum(x * 10 + 4, l,r,a) luckynum(x * 10 + 7, l,r,a) return a def luckynuber(x, n, a): p = set(str(x)) if len(p) <= 2: a.append(x) if x < n: luckynuber(x + 1, n, a) return a # ------------------------------------------------------interactive problems def interact(type, x): if type == "r": inp = input() return inp.strip() else: print(x, flush=True) # ------------------------------------------------------------------zero at end of factorial of a number def findTrailingZeros(n): # Initialize result count = 0 # Keep dividing n by # 5 & update Count while (n >= 5): n //= 5 count += n return count # -----------------------------------------------merge sort # Python program for implementation of MergeSort def mergeSort(arr): if len(arr) > 1: mid = len(arr) // 2 L = arr[:mid] R = arr[mid:] mergeSort(L) mergeSort(R) i = j = k = 0 while i < len(L) and j < len(R): if L[i] < R[j]: arr[k] = L[i] i += 1 else: arr[k] = R[j] j += 1 k += 1 while i < len(L): arr[k] = L[i] i += 1 k += 1 while j < len(R): arr[k] = R[j] j += 1 k += 1 # -----------------------------------------------lucky number with two lucky any digits res = set() def solven(p, l, a, b, n): # given number if p > n or l > 10: return if p > 0: res.add(p) solven(p * 10 + a, l + 1, a, b, n) solven(p * 10 + b, l + 1, a, b, n) # problem n = int(input()) for a in range(0, 10): for b in range(0, a): solve(0, 0) print(len(res)) """ """ def subsetsUtil(A, subset, index, d): print(*subset) s = sum(subset) d.append(s) for i in range(index, len(A)): subset.append(A[i]) subsetsUtil(A, subset, i + 1, d) subset.pop(-1) return d def subsetSums(arr, l, r, d, sum=0): if l > r: d.append(sum) return subsetSums(arr, l + 1, r, d, sum + arr[l]) # Subset excluding arr[l] subsetSums(arr, l + 1, r, d, sum) return d def print_factors(x): factors = [] for i in range(1, x + 1): if x % i == 0: factors.append(i) return (factors) # ----------------------------------------------- def calc(X, d, ans, D): # print(X,d) if len(X) == 0: return i = X.index(max(X)) ans[D[max(X)]] = d Y = X[:i] Z = X[i + 1:] calc(Y, d + 1, ans, D) calc(Z, d + 1, ans, D) def generate(st, s): if len(s) == 0: return if s not in st: st.add(s) for i in range(len(s)): t = list(s).copy() t.remove(s[i]) t = ''.join(t) generate(st, t) return #=--------------------------------------------longest increasing subsequence def largestincreasingsubsequence(A): l = [1]*len(A) for i in range(1,len(l)): for k in range(i): if A[k]<=A[i]: l[i]=max(l[i],l[k]+1) return max(l) #----------------------------------Function to calculate Bitwise OR of sums of all subsequences def findOR(nums, N): prefix_sum = 0 result = 0 for i in range(N): result |= nums[i] prefix_sum += nums[i] result |= prefix_sum return result def OR(a, n): ans = a[0] for i in range(1, n): ans |= a[i] #l.append(ans) return ans def toString(List): return ''.join(List) # Function to print permutations of string # This function takes three parameters: # 1. String # 2. Starting index of the string # 3. Ending index of the string. def permute(a, l, r,p): if l == r: p.append(toString(a)) else: for i in range(l, r + 1): a[l], a[i] = a[i], a[l] permute(a, l + 1, r,p) a[l], a[i] = a[i], a[l] # backtrack def squareRoot(number, precision): start = 0 end, ans = number, 1 while (start <= end): mid = int((start + end) / 2) if (mid * mid == number): ans = mid break if (mid * mid < number): start = mid + 1 else: end = mid - 1 increment = 0.1 for i in range(0, precision): while (ans * ans <= number): ans += increment ans = ans - increment increment = increment / 10 return ans def countRectangles(l, w): squareSide = math.gcd(l, w) return int((l * w) / (squareSide * squareSide)) # Function that count the # total numbersProgram between L # and R which have all the # digit same def count_same_digit(L, R): tmp = 0 ans = 0 n = int(math.log10(R) + 1) for i in range(0, n): # tmp has all digits as 1 tmp = tmp * 10 + 1 for j in range(1, 10): if (L <= (tmp * j) and (tmp * j) <= R): #print(tmp*j) # Increment the required count ans += 1 return ans #----------------------------------print k closest number of a number in an array def findCrossOver(arr, low, high, x): # Base cases if (arr[high] <= x): # x is greater than all return high if (arr[low] > x): # x is smaller than all return low # Find the middle point mid = (low + high) // 2 if (arr[mid] <= x and arr[mid + 1] > x): return mid if (arr[mid] < x): return findCrossOver(arr, mid + 1, high, x) return findCrossOver(arr, low, mid - 1, x) def Kclosest(arr, x, k, n,ans): # Find the crossover point l = findCrossOver(arr, 0, n - 1, x) r = l + 1 count = 0 if (arr[l] == x): l -= 1 #print(l) while (l >= 0 and r < n and count < k): if (x - arr[l] < arr[r] - x): ans.append(arr[l]) l -= 1 else: ans.append(arr[r]) r += 1 count += 1 while (count < k and l >= 0): ans.append(arr[l]) l -= 1 count += 1 while (count < k and r < n): ans.append(arr[r]) r += 1 count += 1 return ans def dfs(root,nodeVal,nodeConnection,visited): leftVal = nodeVal[root][0] rightVal = nodeVal[root][1] solution = [] if nodeConnection[root]: visited.add(root) for i in nodeConnection[root]: if i not in visited: solution.append(dfs(i,nodeVal,nodeConnection,visited)) leftMax = 0 rightMax = 0 for i in solution: l, r = i leftMax += max(abs(leftVal - l[0]) + l[1], abs(leftVal - r[0]) + r[1]) rightMax += max(abs(rightVal - l[0]) + l[1], abs(rightVal - r[0]) + r[1]) return ((leftVal, leftMax), (rightVal, rightMax)) else: return ((leftVal, 0), (rightVal, 0)) """ def luckynumber(x,n,a): if x >0: a.append(x) if x>10**9: return a else: if x < 1e12: luckynumber(x * 10 + 4,n,a) luckynumber(x * 10 + 7,n,a) def lcm(a,b): return (a*b)//math.gcd(a,b) def query1(l, r): if l >= r: return -1 print('?', l + 1, r + 1) sys.stdout.flush() return int(input()) - 1 def answer(p): print('!', p + 1) sys.stdout.flush() exit() #---------------------count number of primes """ import math MAX = 10**5 prefix = [0] * (MAX + 1) def buildPrefix(): prime = [1] * (MAX + 1) p = 2 while (p * p <= MAX): if (prime[p] == 1): i = p * 2 while (i <= MAX): prime[i] = 0 i += p p += 1 for p in range(2, MAX + 1): prefix[p] = prefix[p - 1] if (prime[p] == 1): prefix[p] += 1 def query(L, R): return prefix[R] - prefix[L - 1] #buildPrefix() def maxSubArraySum(a, size): max_so_far = a[0] curr_max = a[0] for i in range(1, size): curr_max = max(a[i], curr_max + a[i]) max_so_far = max(max_so_far, curr_max) return max_so_far def solvepp(n,k): if n==1 and k==1: return 0 mid=(2**(n-1))//2 if k<=mid: return solvepp(n-1,k) else: return solvepp(n-1,k-(mid))==0 #------------------print subset of strings def solvr(s,p): if len(s)==0: print(p,end=" ") return op1=p op2=p+s[0] s=s[1:] solvr(s,op1) solvr(s,op2) return #-------------------------------------balanced paranthesis def paranthesis(n,m,ans,l): if n==0 and m==0: print(ans) return if n!=0: op1=ans+"(" paranthesis(n-1,m,op1,l) if m>n: op2=ans+")" paranthesis(n,m-1,op2,l) """ """ class node: def __init__(self,data): self.data=data self.next=None class linkedlis: def __init__(self): self.head=None def printlis(self): temp=self.head while(temp): print(temp.data,end=" ") temp=temp.next def pushfirst(self,new_data): new_node=node(new_data) new_node.next=self.head self.head=new_node def pushmid(self,previous_node,new_data): new_node=node(new_data) if previous_node==None: print("call pushfirst function if it is the the start otherwise raise an error.") new_node.next=previous_node.next previous_node.next=new_node def pushlast(self,new_data): new_node=node(new_data) if self.head==None: self.head=new_node return last=self.head while(last.next!=None): last=last.next last.next=new_node def delete_node(self,key): pass if __name__ == '__main__': l=linkedlis() l.head= node(1) p = node(2) pp = node(4) l.head.next = p p.next = pp #print(l.head) l.pushmid(p, 3) l.pushlast(5) l.pushfirst(0) #l.printlis() #print(l.head.data) """ def rse(arr,n): stack=[] ans=[] for i in range(n-1,-1,-1): if len(stack)==0: ans.append(n) else: while(len(stack)!=0): if stack[-1][0]>=arr[i]: stack.pop() else: break if len(stack)==0: ans.append(n) else: ans.append(stack[-1][1]) stack.append([arr[i],i]) ans.reverse() return ans def lse(arr,n): stack=[] ans=[] for i in range(n): if len(stack)==0: ans.append(-1) else: while(len(stack)!=0): if stack[-1][0]>=arr[i]: stack.pop() else: break if len(stack)==0: ans.append(-1) else: ans.append(stack[-1][1]) stack.append([arr[i],i]) return ans def mah(arr): max1=0 p=rse(arr,len(arr)) q=lse(arr,len(arr)) for i in range(len(arr)): a=(p[i]-q[i]-1)*arr[i] max1=max(a,max1) return max1 """ def lcs(s,r): rr=len(r) ss=len(s) l=[[0]*(rr+1) for i in range(ss+1)] for i in range(1,ss+1): for j in range(1,rr+1): if s[i-1]==r[j-1]: l[i][j]=l[i-1][j-1]+1 else: l[i][j] =max(l[i-1][j],l[i][j-1]) return l[ss][rr] def subsetsum(arr,sum,len): dp=[[False]*(sum+1) for i in range(len+1)] for i in range(len+1): dp[i][0]=True for i in range(1,len+1): for j in range(1,sum+1): #print(dp[i][j]) if arr[i-1]>j: dp[i][j]=dp[i-1][j] else: dp[i][j]=dp[i-1][j] or dp[i-1][j-arr[i-1]] return dp[len][sum] """ """ def matrixmincost(cost,n,m): dp = [[0 for x in range(m)] for x in range(n)] for i in range(n): for j in range(m): if i==0 and j==0: dp[i][j]=cost[i][j] elif i==0 and j!=0: dp[i][j]=dp[i][j-1]+cost[i][j] elif j==0 and i!=0: dp[i][j]=dp[i-1][j]+cost[i][j] else: dp[i][j] = cost[i][j] + min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]) #print(dp) return dp[n-1][m-1] """ #--------------------------adding any number to get a number """ def coinchange(n,arr,len1): dp=[0]*(n+1) dp[0]=1 for i in range(len1): for j in range(arr[i],n+1): dp[j]+=dp[j-arr[i]] return dp[n] """ class Graph(object): def __init__(self, vertices): self.graph = defaultdict(list) self.V = vertices def addEdge(self, u, v): self.graph[u].append(v) self.graph[v].append(u) def connectedComponents(self): unvisited = set(range(self.V)) queue = deque() count = 0 while len(unvisited) > 0: count += 1 v = next(iter(unvisited)) unvisited.remove(v) queue.append(v) while len(queue) > 0: v = queue.popleft() for w in self.graph[v]: if w in unvisited: unvisited.remove(w) queue.append(w) return count def maxSumIS(arr, n): msis=arr.copy() for i in range(1, n): for j in range(i): if (arr[i] > arr[j] and msis[i] < msis[j] + arr[i]): msis[i] = msis[j] + arr[i] c=max(msis) p=5 return c #--------------------------------find the index of number in sorted array from behind def binarysearch2(arr,target): lo=0 hi=len(arr)-1 while(lo<=hi): mid=(lo+hi)//2 #print(arr[mid],arr[mid-1],mid) if arr[mid]==target: if mid!=len(arr)-1: if arr[mid+1]!=target: return mid else: lo+=1 else: return mid continue if arr[mid]>target: hi=mid-1 else: lo=mid+1 def ngr(arr,n): stack=[] ans=[] for i in range(n-1,-1,-1): if len(stack)==0: ans.append(-1) else: while(len(stack)>0): if stack[-1][0]<arr[i]: stack.pop() else: break if len(stack)==0: ans.append(-1) else: ans.append(stack[-1][1]) stack.append([arr[i],i]) ans.reverse() return ans def alperm(nums, path,result): if not nums: result.add(tuple(path)) return for i in range(0,len(nums)): alperm(nums[:i] + nums[i + 1:], path + [nums[i]],result) return result #p=float("inf") def minsum(arr,n,m,res,l): if n==1 and m==1: res+=arr[0][0] l.append(res) else: if n!=1: p=res+arr[n-1][m-1] minsum(arr,n-1,m,p,l) if m!=1: p=res+arr[n-1][m-1] minsum(arr,n,m-1,p,l) return min(l) def subset(arr,n,ans,c): if n==0: print(c) ans.add(tuple(sorted(c))) return else: op=c+[arr[n-1]] subset(arr,n-1,ans,op) subset(arr,n-1,ans,c) """ nums=[5,2,6,1] ret = [0] * len(nums) sl = SortedList() for i in range(len(nums) - 1, - 1, - 1): ret[i] = sl.bisect_left(nums[i]) sl.add(nums[i]) print(ret) """ def catalan(n): if (n == 0 or n == 1): return 1 catalan = [0] * (n + 1) catalan[0] = 1 catalan[1] = 1 for i in range(2, n + 1): for j in range(i): catalan[i] += catalan[j] * catalan[i - j - 1] return catalan[n] def isP(p,i,j): if j<=i: return True if p[i]!=p[j]: return False return isP(p,i+1,j-1) """ class Node: def __init__(self, data): self.data = data self.left = None self.right = None def leftViewUtil(root, level, max1): if root is None: return if max1[0]<level: print(root.data) max1[0]=level leftViewUtil(root.left, level + 1, max1) leftViewUtil(root.right,level+1,max1) def leftView(root): max1 =[0] leftViewUtil(root, 1, max1) root = Node(10) root.left = Node(2) root.right = Node(3) root.left.left = Node(7) root.left.right = Node(8) root.right.right = Node(15) root.right.left = Node(12) root.right.right.left = Node(14) #root.left.left.right=Node(15) max1=-1 leftView(root) """ def main(): t=1 t=inpu() for _ in range(t): a,b=sep() q=0 p=abs(a-b) if p==0: print(0,0) continue c=min(a,b) d=max(a,b) s=c//p ans=min((s+1)*p-c,c-(s)*p) print(p,ans) 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
//package Competitve1; //package compete; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; //import java.security.acl.LastOwnerException; import java.util.*; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); 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[] nextSArray() { String sr[] = null; try { sr = br.readLine().trim().split(" "); } catch (IOException e) { e.printStackTrace(); } return sr; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod=1000000007; public static void main(String[] args) throws Exception{ FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0){ long a=sc.nextLong(),b=sc.nextLong(); if(a==b) { out.println("0 0"); continue; } long gcd=Math.abs(a-b); long n=(a/gcd)*gcd; long x=a-n,y=n+gcd-a; out.print(gcd+" "); if(x>y) out.println(y); else out.println(x); } out.close(); } static long pow(long a,long n) { if(n==0l) return 1l; long curr=pow(a,n/2)%mod; if(n%2l==0l) return (curr*curr)%mod; else return (((curr*a)%mod)*curr)%mod; } }
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; long long maxx, minn; maxx = max(a, b); minn = min(a, b); cout << maxx - minn << " "; if(a == b){ cout << 0 << endl; } else { cout << min(minn % (maxx-minn), maxx - minn - minn % (maxx-minn)) << 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
def il(): #Input list return list(map(int,input().split())) def ii(): #Input int return int(input()) def imi(): #Input multiple int return map(int,input().split()) from math import * t=ii() for _ in range(t): a,b=imi() if (a==b): print(0,0) else: k=abs(b-a) a=min(a,b) x=min(a-(a//k)*k,ceil(a/k)*k-a) print(k,x)
PYTHON3