Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import java.util.*; import java.io.*; import java.math.*; /** * @author Naitik * */ public class Main { static FastReader sc=new FastReader(); static long dp[][]; static int mod=1000000007; static int max; static long bit[]; static long x; static long y; static long B[][]; public static void main(String[] args) { //CHECK FOR N=1 //CHECK FOR N=1 PrintWriter out=new PrintWriter(System.out); StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { long a=l(); long b=l(); long c=min(a,b); long d=max(a,b); if(c==d) { out.println("0 0"); continue outer; } long diff=d-c; long p=c%diff; out.println(diff+" "+Math.min(p, diff-p)); } //System.out.println(sb.toString()); out.close(); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y,int z){ this.x=x; this.y=y; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } // public int hashCode() // { // final int temp = 14; // int ans = 1; // ans =x*31+y*13; // return ans; // } // @Override // public boolean equals(Object o) // { // if (this == o) { // return true; // } // if (o == null) { // return false; // } // if (this.getClass() != o.getClass()) { // return false; // } // Pair other = (Pair)o; // if (this.x != other.x || this.y!=other.y) { // return false; // } // return true; // } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } //FENWICK TREE static void update(int i, int x){ for(; i < bit.length; i += (i&-i)) bit[i] += x; } static int sum(int i){ int ans = 0; for(; i > 0; i -= (i&-i)) ans += bit[i]; return ans; } //END //static void add(int v) { // if(!map.containsKey(v)) { // map.put(v, 1); // } // else { // map.put(v, map.get(v)+1); // } //} //static void remove(int v) { // if(map.containsKey(v)) { // map.put(v, map.get(v)-1); // if(map.get(v)==0) // map.remove(v); // } //} public static int upper(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } 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
#Fast I/O import sys,os import math # To enable the file I/O i the below 2 lines are uncommented. # read from in.txt if uncommented if os.path.exists('in.txt'): sys.stdin=open('in.txt','r') # will print on Console if file I/O is not activated #if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w') # inputs template from io import BytesIO, IOBase def getNum(x): num = x count = -1 a = [] while x > 0: a.append(x%10) count += 1 x //= 10 y = num * (10**count) z = 0 k = 0 for i in range(len(a)-1,0,-1): z += a[i]*(10**k) k+=1 return (y+z) def main(): for _ in range(mi()): a,b = MI() if a == b : print(0,0) continue cx0 = abs(b-a) ch0 = a%cx0 ch1 = cx0-ch0 print(cx0,min(ch0,ch1)) # Sample Inputs/Output # 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") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #for array of integers def MI():return (map(int,input().split())) def mi():return int(input()) # endregion #for fast output, always take string def outP(var): sys.stdout.write(str(var)+'\n') # end of any user-defined functions MOD=10**9+7 mod=998244353 # main functions for execution of the program. if __name__ == '__main__': #This doesn't works here but works wonders when submitted on CodeChef or CodeForces 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> using namespace std; typedef long long int ll; int main() { ll t; cin>>t; while(t--) { ll a,b; cin>>a>>b; if(a==b) cout<<0<<" "<<0<<endl; else { ll gcd = abs(b-a); ll minn = min(a%gcd,gcd-a%gcd); cout<<gcd<<" "<<minn<<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
from math import * t=int(input()) for i in range(t): temp=input().split() x=int(temp[0]);y=int(temp[1]) gre=abs(x-y) if(gre!=0): num = min(x%gre,y%gre,gre-x%gre,gre-x%gre) else: num = 0 print(str(gre)+" "+str(num))
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.lang.*; import java.util.*; public class rough { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int n=sc.nextInt(); long[][] ans=new long[n][2]; for(int i=0;i<n;i++){ long a=sc.nextLong(); long b= sc.nextLong(); long gcd=0; long step=0; if(a==b) gcd=0; else if(a*b==0) { if(a==0) gcd=b; else gcd=a; } else{ gcd=Math.abs(a-b); if((gcd!=a && gcd!=b) || a%gcd!=0){ step=Math.min((gcd*((a/gcd)+1))-a, a%gcd); } } ans[i][0]=gcd; ans[i][1]=step; } for(long[] a:ans){ System.out.println(a[0]+" "+a[1]); } } }
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 class24 { public static void main(String[] args) { Scanner input=new Scanner(System.in); int t=input.nextInt(); for(int i=0;i<t;i++) { long a=input.nextLong(); long b=input.nextLong(); long c=0,d=0,e=0; if(a==b) { System.out.println("0"+" "+"0"); } else { if(a>b) { c=a-b; d=a%c; } else { c=b-a; d=b%c; } if(d>c-d) { e=c-d; } else { e=d; } if(c==1) { System.out.println("1"+" "+"0"); } else { System.out.print(c); System.out.print(" "); System.out.print(e); System.out.println(); } } } } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class thisContestp1 { public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader(new File("input.txt"))); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // read line int t = Integer.parseInt(br.readLine()); for(int i = 0; i < t; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); long a = Long.parseLong(st.nextToken()); long b = Long.parseLong(st.nextToken()); long diff = Math.abs(a-b); if(diff == 0) { System.out.println("0 0"); }else { long offset = Math.min(b % diff, diff-(b%diff)); System.out.println(diff + " " + offset); } } br.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
/* * akshaygupta26 */ import java.io.*; import java.util.*; public class A { public static void main(String[] args) { FastReader sc=new FastReader(); StringBuffer ans=new StringBuffer(); int test=sc.nextInt(); while(test-->0) { long a=sc.nextLong(); long b=sc.nextLong(); if(a == b) { ans.append(0+" 0\n");continue; } long diff =Math.abs(a-b); if(diff == 1) { ans.append(1+" "+0+"\n"); } else { long steps =Math.min(getNext(a,diff)-a,a-getBelow(a,diff)); ans.append(diff+" "+steps+"\n"); } } System.out.print(ans); } static long getNext(long num,long or) { long div =num/or; if(num%or != 0) ++div; return (div*or); } static long getBelow(long num,long or) { long div =num/or; return (div*or); } static long _gcd(long a,long b) { if(b == 0) return a; return _gcd(b,a%b); } 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 void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } 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 os,sys,math from io import BytesIO, IOBase from collections import defaultdict,deque,OrderedDict import bisect as bi def yes():print('YES') def no():print('NO') def I():return (int(input())) def In():return(map(int,input().split())) def ln():return list(map(int,input().split())) def Sn():return input().strip() BUFSIZE = 8192 #complete the main function with number of test cases to complete greater than x def find_gt(a, x): i = bi.bisect_left(a, x) if i != len(a): return i else: return len(a) def solve(): a,b=In() if a>b: a,b=b,a t=b-a if t==0: print(0,0) return mn,mx=a-(a%t),a+(t-(a%t)) print(t,min((mx-a),(a-mn))) pass def main(): T=I() for i in range(T): solve() M = 998244353 P = 1000000007 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()
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
'''input 4 8 5 1 2 4 4 3 9 ''' from sys import stdin,stdout for _ in range(int(stdin.readline())): a, b = map(int,stdin.readline().strip().split()) if a==b: stdout.write("0 0\n") else: diff = abs(a-b) lower = min(a,b)%diff upper = diff - lower stdout.write(str(diff)+" "+str(min(lower,upper))+"\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
#include <bits/stdc++.h> #define ll long long int #define st string #define pb(x) push_back(x) #define sz size() using namespace std; const ll maxn=1e3*2+1,maxv=1e6,mod=1e9; ll a1[maxn]; ll dp[maxn]; int main() { std::ios_base::sync_with_stdio(false);cin.tie(0),cout.tie(0); ll n,a,b; cin>>n; for(int i=1;i<=n;i++){ cin>>a>>b; ll c=abs(a-b); if(c==0) cout<<0<<" "<<0<<endl; else cout<<c<<" "<<min((a%c),c-(a%c))<<endl; } }
CPP
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
#include <iostream> using namespace std; #define f(x,y,z) for(int x=y;x<z;++x) typedef long long ll; ll a, b, s, t; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); for(cin >> t; t--;) { cin >> a >> b; s = abs(a-b); cout << s << ' '; if(s < 2) cout << "0\n"; else { a = min(a, b); cout << min(a%s, s-a%s) << '\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
t = int(input()) for i in range(t): r, s = map(int, input().split()) if r == s: print(0, 0) else: a = min(r, s) b = max(r, s) q = b - a n = b % q print(q, min(n, q - 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
t=int(input()) for i in range(t): a,b=[int(x) for x in input().split()] g=abs(a-b) if g==0: print("0 0") else: lb=a//g hb=lb+1 if a-lb*g<hb*g-a: mo=a-lb*g else: mo=hb*g-a print(g,mo)
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(raw_input()) for _ in xrange(T): a, b = map(int, raw_input().split()) x = abs(a - b) if not x: print 0, 0 else: y = a % x y = min(y, x - y) print x, y
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.lang.*; import java.io.*; public class cf2 { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O using short named function ---------*/ public static String ns() { return scan.next(); } public static int ni() { return scan.nextInt(); } public static long nl() { return scan.nextLong(); } public static double nd() { return scan.nextDouble(); } public static String nln() { return scan.nextLine(); } public static void p(Object o) { out.print(o); } public static void ps(Object o) { out.print(o + " "); } public static void pn(Object o) { out.println(o); } /*-------- for output of an array ---------------------*/ static void iPA(int arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void lPA(long arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void sPA(String arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } static void dPA(double arr[]) { StringBuilder output = new StringBuilder(); for (int i = 0; i < arr.length; i++) output.append(arr[i] + " "); out.println(output); } /*-------------- for input in an array ---------------------*/ static void iIA(int arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ni(); } static void lIA(long arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nl(); } static void sIA(String arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = ns(); } static void dIA(double arr[]) { for (int i = 0; i < arr.length; i++) arr[i] = nd(); } /*------------ for taking input faster ----------------*/ 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 ArrayList<Integer> sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> arr = new ArrayList<>(); // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Method to check if x is power of 2 static boolean isPowerOfTwo(int x) { return x != 0 && ((x & (x - 1)) == 0); } //Method to return lcm of two numbers static int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } //Method to count digit of a number static int countDigit(long n) { String sex = Long.toString(n); return sex.length(); } static void reverse(int a[]) { int i, k, t; int n = a.length; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static final Random random = new Random(); //Method for sorting static void ruffleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = random.nextInt(n); int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void sortadv(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long value : a) { l.add(value); } Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); } //Method for checking if a number is prime or not static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void main(String[] args) throws java.lang.Exception { OutputStream outputStream = System.out; out = new PrintWriter(outputStream); scan = new FastReader(); //for fast output sometimes StringBuilder sb = new StringBuilder(); int t = ni(); while (t-- != 0) { long a=nl(); long b=nl(); if(a==b) pn("0 0"); else if(a==0||b==0) { pn((a==0?b:a)+" 0"); } else{ long diff=Math.abs(a-b); if(diff==1){ pn("1 0"); } else{ long ans=Math.min(diff-Math.max(a,b)%diff,Math.max(a,b)%diff); pn(diff+" "+ans);} } } out.flush(); out.close(); } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
for _ in range(int(input())): a,b = map(int,input().split()) if a == b: g = 0 stps = 0 else: g = abs(a-b) stps = min(a%g,(g-a)%g) print(g,stps)
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.Scanner; public class A { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); while (test-- > 0) { long a = scn.nextLong(); long b = scn.nextLong(); long diff = a - b; if (diff < 0) diff = -diff; if (diff == 0) { System.out.println(0 + " " + 0); continue; } long required = a % diff; required = Long.min(diff - required, required); System.out.println(diff + " " + required); } } }
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.*; public class Main { 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 diff=(long)Math.abs(a-b); long max=(long)Math.max(a,b); if(diff==0) System.out.print(0+" "+0); else{ long min=Long.MIN_VALUE; long i=1; while(diff>0){ long difference=diff*i; long ans=(long)Math.abs(difference-max); if(ans>min && min!=Long.MIN_VALUE) break; min=ans; i++; } System.out.print(diff+" "+min); } System.out.println(); } } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import java.util.Scanner; public class Ecitbets { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); for(int i=0;i<a;i++) { long b =sc.nextLong(); long c=sc.nextLong(); if(b==c) { System.out.println(0+" "+0); } else { long l=Math.max(b, c); long q= Math.min(b, c); long mv=l-q; long ans2=0; if(q%mv ==0) { ans2=0; } else { ans2=Math.min(mv-q%mv, q%mv); } System.out.println(mv+" "+ans2); } } sc.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
t = int(input()) while t>0: a,b = map(int,input().split()) if a==b: print(0,0) else: x = abs(a-b) y = min(a%x,x-a%x) print(x,y) t=t-1
PYTHON3
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import java.io.*; import java.util.*; import java.lang.*; public class Main { static long mod = (long) Math.pow(10, 9) + 7; public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); StringBuilder fout = new StringBuilder(); int t = sc.nextInt(); while (t-- != 0) { long n = sc.nextLong(); long b = sc.nextLong(); if(b<n) { long temp=n; n=b; b=temp; } long x = Math.abs(n-b); if(n==0) { System.out.println( x + " 0" ); continue; } if(x==0) { System.out.println( "0 0" ); continue; } long k ; if(n%x==0 ) { k=0; } else if(x > n) { k=Math.min(x-n,n); } else { k=Math.min(n%x,x-n%x); //k=Math.min(x-n%x,n-x); } System.out.println( x + " " + k); // long a[]=new long[n]; // for(int i=0;i<n;i++) // { // a[i]=sc.nextLong(); // } } } static void print(long[] arr) { for (long l : arr) System.out.print(l + " "); } static void print(int[] arr) { for (int j : arr) System.out.print(j + " "); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static void no() { System.out.println("NO"); } static void yes() { System.out.println("YES"); } static class Pair { int a, b; public Pair(int a, int b) { this.a = a; this.b = b; } } public 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.Scanner; public class gcd { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test_cases = sc.nextInt(); while(test_cases>0) { long a = sc.nextLong(); long b = sc.nextLong(); long diff = Math.abs(a-b); long i=diff==0?0:Math.min(a%diff,diff-a%diff); System.out.println(diff+" "+i); test_cases--; } } }
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 = map(int,input().split()) if(a == b): print('0 0') elif(abs(a-b) == 1): print('1 0') elif(a==0 and b!=0): print(str(b)+" 0") elif(b==0 and a!=0): print(str(a)+" 0") else: exc = abs(a-b) if(a < b): k = 0 while(k < a): pre = k k = k + exc if((a-pre) > (k-a)): print(str(exc)+" "+str(k-a)) else: print(str(exc)+" "+str(a-pre)) else: k = 0 while(k < b): pre = k k = k + exc if((b-pre) > (k-b)): print(str(exc)+" "+str(k-b)) else: print(str(exc)+" "+str(b-pre))
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.Scanner; // problem 1543A - exciting bets public class cf4 { public static void main(String[] args) { Scanner kb = new Scanner(System.in); int num = kb.nextInt(); long a; long b; long dif; for(int i = 0; i < num; i++) { a = kb.nextLong(); b = kb.nextLong(); dif = Math.abs(a-b); if(dif == 0) System.out.println("0 0"); else System.out.println(dif + " " + Math.min(a%dif, dif - (a%dif))); } } }
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; long long add(vector<long long> v,int n){ long long ans = 0; for(long long x: v){ ans+=x; ans = ans%n; } return ans; } void solve() { long long a,b; cin >> a >> b; if(b>a){ long long t = a; a = b; b = t; } cout << (a-b) << " " << ((a-b != 0)?min((b)%(a-b), (a-b) - b%(a-b)):0) <<endl; } int main() { long long t; cin >> t; while (t--) { solve(); } }
CPP
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
#include <bits/stdc++.h> using namespace std; #define ll long long #define f(i,n) for(int i=0;i<n;i++) #define endl ("\n") #define pb push_back #define mp make_pair #define pl pair<ll, ll> #define vl vector<ll> #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL); #define M 1000000007 const int N=2e5+2; ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);} ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);} vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;} ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m int fact[N]; void precalc(){ fact[0]=1; for(int i=1;i<N;i++){ fact[i]=mod_mul(fact[i-1],i,M); } } long long mod(long long x){ return ((x%M + M)%M); } ll inv(ll x){ return expo(x,M-2,M); } ll divide(ll a, ll b){ return mod_mul(a,inv(b),M); } ll nCr(ll n, ll r){ return divide(fact[n],mod_mul(fact[r],fact[n-r],M)); } int main() { fastIO ll t; cin>>t; while(t--){ ll a,b; cin>>a>>b; ll d=abs(a-b); ll e=max(a,b); if(a==b){ cout<<0<<" "<<0<<endl; continue; } ll k=e%d; ll v; if(d-k>=0 and d-k<k) v=d-k; else v=k; cout<<d<<" "<<v<<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
for _ in range(int(input())): a, b = map(int, input().split()) if a == b: print(0, 0) continue if a > b: a, b = b,a c = b - a base1 = a // c base2 = base1 + 1 print(c, min(abs(base1 * c - a), abs(base2 * c - 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 math for _ in range(int(input())): a,b=map(int,input().split()) if a==b: print("0 0") else: ans1=abs(a-b) ans2=min(ans1-(a%ans1),a%ans1) print(ans1,ans2)
PYTHON3
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
#include <bits/stdc++.h> typedef long long int ll; const unsigned int MOD = 1000000007; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t; cin >> t; for (int tt = 0; tt < t; tt++) { ll a,b; cin>>a>>b; if(a>b) swap(a,b); if(a==b) cout<<"0 0\n"; else { ll x=abs(a-b); ll y= a%x; y = min(y, x-y); cout<<x<<" "<<y<<"\n"; } } #ifndef ONLINE_JUDGE cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; #endif 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
if __name__ == '__main__': t = int(input()); for _ in range(t): m, n = map(int,input().split()); if m == n: print('0 0'); else: M = abs(m - n); step = min(m,n); down_step = m%M; up_step = M - m%M; print(abs(m-n), min([up_step, down_step, step]));
PYTHON3
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
t=int(input()) for p in range(t): l=list(map(int,input().rstrip().split())) a=l[0] b=l[1] if a==b: print(f"0 0") # elif a==0 and b==0: # print(f"0 0") # elif a==0: # print(f"{b} 0") # elif b==0: # print(f"{a} 0") else: print(abs(a-b),end=' ') if abs(a-b)==1: print(0) else: print(min(abs(a-b)-(a%(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.*; import java.util.*; public class codeforces { static FastReader fr; static StringBuilder res; static class FastReader { BufferedReader br; StringTokenizer st; BufferedWriter bw; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); bw=new BufferedWriter(new OutputStreamWriter(System.out)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } char nextChar() { return next().charAt(0); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void write(String str){ try { bw.write(str); } catch (IOException e) { e.printStackTrace(); } } void close(){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } 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 { fr=new FastReader(); int t=fr.nextInt(); res=new StringBuilder(); while(t-->0){ solve(); } fr.write(res.toString()); fr.close(); } public static void solve() { long a=fr.nextLong(); long b=fr.nextLong(); if(a==b) { res.append("0 0\n"); return; } long diff=Math.abs(a-b); long c=Math.min(a,b); long e=diff*(c/diff+1)-c; long d=Math.min(c%diff,e); res.append(diff+" "+d+"\n"); } public static int[] readArray(int n) { int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=fr.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
import sys testing = len(sys.argv) == 4 and sys.argv[3] == "myTest" if testing: cmd = sys.stdout from time import time start_time = int(round(time() * 1000)) readAll = open(sys.argv[1], 'r').read sys.stdout = open(sys.argv[2], 'w') else: readAll = sys.stdin.read # ############ ---- I/O Functions ---- ############ flush = sys.stdout.flush class InputData: def __init__(self): self.lines = readAll().split('\n') self.n = len(self.lines) self.ii = -1 def input(self): self.ii += 1 assert self.ii < self.n return self.lines[self.ii] inputData = InputData() input = inputData.input def intin(): return(int(input())) def intlin(): return(list(map(int,input().split()))) def chrin(): return(list(input())) def strin(): return input() def lout(l, sep="\n", toStr=True): print(sep.join(map(str, l) if toStr else l)) def dout(*args, **kargs): if not testing: return if args: print(args[0] if len(args)==1 else args) if kargs: print([(k,v) for k,v in kargs.items()]) # ############ ---- I/O Functions ---- ############ # from math import ceil # from collections import defaultdict as ddict, Counter # from heapq import * # from Queue import Queue def main(): a,b = intlin() if a==b: return '0 0' n = max(a,b) - min(a,b) y = min(a,b) m = abs(a-b) if a%m != b%m: m = 1 x = min(a,b)-1 else: if min(a,b) < m: x = m - min(a,b) else: x = min(a%m, m-a%m) if m >= n: return '%d %d' % (m, min(x,y)) return '%d %d' % (n, y) anss = [] for _ in xrange(intin()): anss.append(main()) # anss.append("YES" if main() else "NO") lout(anss) if testing: sys.stdout = cmd print(int(round(time() * 1000)) - start_time)
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; void excbet() { long long a, b; cin >> a >> b; long long c = abs(a - b); long long d = 0; if( c != 0) d = a%c; cout << c << " " << min(d, c-d) << endl; } int main() { int t; cin >> t; for(int i=0; i<t; i++) { excbet(); } 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 solve(a, b): d = abs(a - b) if not d: print(0, 0) return print(d, min(a % d, d - (a % d))) t, = map(int, input().split()) for _ in range(t): a, b = map(int, input().split()) solve(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.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int t=Integer.parseInt(bu.readLine()); while(t-->0) { String s[]=bu.readLine().split(" "); long a=Long.parseLong(s[0]),b=Long.parseLong(s[1]); if(b<a) a=a^b^(b=a); long d=b-a; if(d==0) sb.append("0 0\n"); else { long add=a/d,rem=Math.min(Math.abs(add*d-a),Math.abs(add*d+d-a)); sb.append(d+" "+rem+"\n"); } } System.out.print(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> using namespace std; #define ll long long ll const MAX_N=1e7+5; int main(){ ll t; cin>>t; while(t--){ ll a, b; cin>>a>>b; ll ans=0; ll a1=max(a,b); ll a2=min(a,b); if(a1*2==b){ cout<<a1<<" "<<0<<endl; } else if(a1==a2){ cout<<0<<" "<<0<<endl; } else{ ll ans1=0; ll ans2=0; ans1=a1-a2; ll k1=0;ll k2=0; k1=a2%ans1; k2=a2/ans1; k1=min(k1,(k2+1)*ans1-a2); cout<<ans1<<" "<<k1<<endl; // else cout<<ans1<<" "<<a2<<endl; } } }
CPP
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
#include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cstdio> #include<stdio.h> #include<cmath> #include<math.h> #include<vector> #include<set> #include<queue> #include<map> #include<sstream> #include<iomanip> #define forn(i,n) for(int (i)=0;i<(n);i++) #define pb push_back #define mp make_pair using namespace std; typedef pair<int,int>pii; typedef long long ll; typedef pair<ll,ll> pll; const int MAXN=100005; const int INF=2147483647; const ll LINF=9223372036854775807; const int dx[]={1,-1,0,0},dy[]={0,0,1,-1}; int tt; ll mod(ll x,ll y) { ll res=x%y; if(res<0)res+=y; return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>tt; while(tt--) { ll a,b; cin>>a>>b; if(a<b)swap(a,b); ll x=a-b; if(x==0)cout<<0<<' '<<0<<endl; else cout<<x<<' '<<min(min(mod(a,x),mod(x-a,x)),min(mod(b,x),mod(x-b,x)))<<endl; } // cout<<mod(-2,3)<<' '<<(-2)%3; return 0; }
CPP
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
for data_samples in range(int(input())): arr = list(map(int, input().split())) if arr[0] == arr[1]: print("0 0") else: max_azart = abs(arr[0] - arr[1]) closest_nums = [min(arr) // max_azart * max_azart, (min(arr) // max_azart + 1) * max_azart] minimum_moves = min(abs(min(arr) - closest_nums[0]), abs(min(arr) - closest_nums[1])) print(max_azart, minimum_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 sys;import math;from collections import deque;from bisect import bisect_left,bisect_right;I=sys.stdin.readline;II=lambda :int(I());IN=lambda x:map(int,x.split());L=lambda x:list(IN(x));M=1000000007;P=print;T=True;F=False for _ in range(II()): a,b=IN(I()) if(a==b): print(0,0) continue if(b>a): a,b=b,a g=a-b q=b//g n=min(b-q*g,(q+1)*g-b) P(g,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
for _ in range(int(input())): a,b=map(int,input().split()) s=abs(a-b) m=0 if s!=0: m=a%s print(s,min(m,s-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
#include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for(int i=a;i<n;i++) #define brep(i,a) for(int i=a;i>=0;i--) #define all(x) x.begin(),x.end() #define allara(x,n) x,x+n #define testcase(x) int x; cin>>x; while(x--) #define vec vector<int> #define vectorpair vector<pair<int,int>> #define Fast_Read ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #define MAX 1000000007 #define mod 1000000007 #define endl "\n" using ll = long long int; //..............................................// int main() { testcase(x) { ll a, b, diff, add, sub; cin>>a>>b; if (a == b) { cout << 0 << " " << 0 << endl; } else if (a == 0) cout << b << " " << 0 << endl; else if (b == 0) cout << a << " " << 0 << endl; else { diff = abs(a - b); sub = a % diff; add=((a/diff+1)*diff)-a; cout<<diff<<" "<<min(add,sub)<<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 range = xrange input = raw_input def gcd(a,b): while b: a,b = b,a%b return a t = int(input()) for _ in range(t): a,b = [int(x) for x in input().split()] goal = max(a,b) - min(a,b) print goal, (min(a%goal, -a%goal) if goal != 0 else 0)
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.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastScanner f = new FastScanner(); int ttt=1; ttt=f.nextInt(); PrintWriter out=new PrintWriter(System.out); for(int tt=0;tt<ttt;tt++) { long a=f.nextLong(); long b=f.nextLong(); if(a==b) { System.out.println(0+" "+0); } else { System.out.println(Math.abs(b-a)+" "+Math.min(Math.abs(b-a)-Math.min(a, b)%Math.abs(b-a),Math.min(a, b)%Math.abs(b-a))); } } out.close(); } static void sort(int[] p) { ArrayList<Integer> q = new ArrayList<>(); for (int i: p) q.add( i); Collections.sort(q); for (int i = 0; i < p.length; i++) p[i] = q.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
# Author : raj1307 - Raj Singh # Date : 07.07.2021 from __future__ import division, print_function import os,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 ii(): return int(input()) def si(): return input() def mi(): return map(int,input().strip().split(" ")) def msi(): return map(str,input().strip().split(" ")) def li(): return list(mi()) def dmain(): sys.setrecursionlimit(1000000) threading.stack_size(1024000) thread = threading.Thread(target=main) thread.start() #from collections import deque, Counter, OrderedDict,defaultdict #from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace #from math import log,sqrt,factorial,cos,tan,sin,radians #from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right #from decimal import * #import threading #from itertools import permutations #Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy abc='abcdefghijklmnopqrstuvwxyz' abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod=1000000007 #mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def getKey(item): return item[1] def sort2(l):return sorted(l, key=getKey,reverse=True) def d2(n,m,num):return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo (x): return (x and (not(x & (x - 1))) ) def decimalToBinary(n): return bin(n).replace("0b","") def ntl(n):return [int(i) for i in str(n)] def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1))) def ceil(x,y): if x%y==0: return x//y else: return x//y+1 def powerMod(x,y,p): res = 1 x %= p while y > 0: if y&1: res = (res*x)%p y = y>>1 x = (x*x)%p return res def gcd(x, y): while y: x, y = y, x % y return x def isPrime(n) : # Check Prime Number or not if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def main(): for _ in range(ii()): a,b=mi() a,b=min(a,b),max(a,b) if a==b: print(0,0) continue d=b-a f=a//d e=a//d+1 m1=a-f*d m2=e*d-a if m1<m2: print(d,m1) else: print(d,m2) # region fastio # template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py 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__": #read() main() #dmain() # Comment Read()
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
for _ in range(int(input())): a,b=map(int,input().split()) temp=abs(a-b) if temp==0: print(0,0) continue temp2=min(a%temp,b%temp,abs(temp-(a%temp)),abs(temp-(b%temp))) print(temp,temp2)
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 int long long int t, a, b; signed main() { for(cin >> t; t--; ) { cin >> a >> b; if(a == b) { cout << "0 0\n"; continue; } if(a < b) swap(a, b); int ans = a - b; cout << ans << " " << min(ans-b%ans, b%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
// import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; public class Test { static BufferedReader br; public static void main(String[] args) throws IOException { // your code goes here br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { // int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); // int n = Integer.parseInt(br.readLine()); // String s = br.readLine(); // int n = Integer.parseInt(st.nextToken()); long m = Long.parseLong(st.nextToken()); long n = Long.parseLong(st.nextToken()); // long k = Long.parseLong(st.nextToken()); // long i = Long.parseLong(st.nextToken()); // long j = Long.parseLong(st.nextToken()); long diff = Math.abs(m - n); if (m == n) { System.out.println("0 0"); } else { long min = Math.min(m, n); long div = min / diff; long a = min - div * diff; long b = (div + 1) * diff - min; System.out.println(diff + " " + Math.min(a, b)); } // System.out.println(ans); } } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } } // // StringTokenizer st = new StringTokenizer(br.readLine()); // // int n = Integer.parseInt(st.nextToken()); // // int m = Integer.parseInt(st.nextToken()); // // long w = Long.parseLong(st.nextToken()); // // String str = br.readLine(); // // int a[] = new int[n]; // // st = new StringTokenizer(br.readLine()); // // HashMap<Long, Integer> canBE = new HashMap<>();
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class LinkedList { public static void main(String[] args) { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); int t = scan.nextInt(); for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out); out.close(); } static class Task { public void solve(int testNumber, FastReader fs, PrintWriter out) { long a = fs.nextLong(), b = fs.nextLong(); if (a == b) { out.println("0 0"); return; } long diff = Math.abs(a - b); long min = Math.min(a, b); out.println(diff + " " + Math.min(min%diff, diff-min%diff)); } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.concurrent.ThreadLocalRandom; //2 10, 3 11, 4 12, // 8 5, 24 15, 8 5 // 18 5 public class C { public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } private static void sport(long a, long b) { if (a == b) { System.out.println("0 0"); return; } long ans1 = Math.abs(a - b); if (gcd(a, b) == ans1) { System.out.println(ans1 + " 0"); return; } long res1 = Math.min(a, b); long mod1 = (long) a % ans1; long mod2 = (long) b % ans1; if (mod1 == mod2) { //if (ans1-mod1>0) { res1 = Math.min(res1, ans1 - mod1); res1 = Math.min(res1, mod1); //} } System.out.println(ans1 + " " + res1); } public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); for (int p = 0; p < t; p++) { long a = sc.nextLong(); long b = sc.nextLong(); sport(a, b); } } static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int[] readArrayInt(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(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.*; public class Exciting_Bets { public static void main(String[] args) { long a,b; Scanner sc=new Scanner(System.in); long input_count=sc.nextInt(); while(input_count-->0){ a=sc.nextLong(); b=sc.nextLong(); if(a>b){ //左小右大 long temp = b; b=a; a=temp; } long ans = b-a; if(ans == 0) System.out.println("0 0"); else{ long one = (ans-a%ans); long two = a%ans; System.out.println(ans+" "+Math.min(one, two)); } } } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
for i in range(int(input())): n,m=map(int,input().split()) x=abs(n-m) if x==0: print(0,0) else: y=n%x y=min(y,x-y) z=m%x z=min(z,x-z) print(x,min(y,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
import java.util.*; public class sbr{ public static void main(String args[]){ Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { long a = s.nextLong(); long b = s.nextLong(); if (a ==b) System.out.println(0 + " " + 0); else if (Math.abs(a - b) == 1) System.out.println(1 + " " + 0); else { long x=Math.abs(a-b); long y=Math.max(a,b)%x; System.out.println(x+" "+Math.min(y,x-y)); } } } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
#include<iostream> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; LL gcd(LL a,LL b){ return b ? gcd(b,b%a) : a; } void solve(){ LL x,y; cin>>x>>y; if(x<y)swap(x,y); if(x==y)cout<<"0 0"<<endl; else{ LL t = x - y; LL l = 0,r = 1e18 / t+1; while(l<r){ LL mid = (l+r)/2; if(t*mid > y)r=mid; else l=mid+1; } LL s = min(l*t-y,y-(l-1)*t); cout<<t<<" "<<s<<endl; } } int main(){ int T; cin>>T;while(T--)solve(); }
CPP
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import sys input = sys.stdin.readline for _ in range(int(input())): a,b = map(int,input().split()) if a == b: print("0","0") else: diff = max(a,b)-min(a,b) print(diff,end = " ") print(min(a%diff,diff-a%diff))
PYTHON3
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import java.io.*; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Scanner; import java.util.TreeMap; import java.util.TreeSet; public class Main { void run(String[]args) throws IOException{ Scanner in=new Scanner(System.in); int t=in.nextInt(); while(t-->0) { long a=in.nextLong(); long b=in.nextLong(); if(a<b) { long tmp=a; a=b; b=tmp; } long k=a-b; if(k==0) { out.println("0 0"); continue; } long ans=b%k; ans=Math.min(ans, k-ans); out.println(k+" "+ans); } out.flush(); } public static void main(String[]args) throws IOException { new Main().run(args); } StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); double ind() throws IOException { in.nextToken(); return in.nval; } int in() throws IOException { in.nextToken(); return(int)in.nval; } long inl() throws IOException { in.nextToken(); return(long)in.nval; } String ins()throws IOException{ in.nextToken(); return in.sval; } }
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 Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tc = scanner.nextInt(); for(int i=0;i<tc;i++) { long a = scanner.nextLong(); long b = scanner.nextLong(); if(a==b) { System.out.println("0 0"); continue; } long diff = Math.max(a,b)-Math.min(a, b); long steps = Math.min(a%diff, diff-(a%diff)); System.out.println(diff+" "+steps); } } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import java.util.*; import java.lang.*; public class Contest { static void solve(long a,long b) { long temp; if(a>b) { temp = a; a = b; b = temp; } if(a==b) { System.out.println(0+" "+0); return; } else if(a==0) { System.out.println(b+" "+0); return; } else if(b-a==1) { System.out.println(1+" "+(b-a-1)); return; } else if(b%a==0) { temp = b - a; long c = (temp - a)<(b - temp) ? (temp - a) : (b - temp); System.out.println(temp+" "+c); return; } else { temp = b - a; long y = b%temp; long x = (y) > (temp-y) ? (temp - y) : (y); if((x)<a) { System.out.println(temp+" "+(x)); return; } else { System.out.println(temp+" "+a); return; } } } public static void main(String h[]) { Scanner sc =new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++) { long a = sc.nextLong(); long b = sc.nextLong(); solve(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; void solve(){ long long a,b; cin>>a>>b; if(a==b) cout<<0<<" "<<0<<endl; else{ cout<< abs(a-b)<<" "<<min(a%abs(a-b), abs(a-b)-a%abs(a-b))<<endl; } } int main() { 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.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pranay2516 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); AExcitingBets solver = new AExcitingBets(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class AExcitingBets { public void solve(int testNumber, FastReader in, PrintWriter out) { long a = in.nextLong(), b = in.nextLong(); long d = Math.abs(a - b); if (d == 0) { out.println("0 0"); return; } out.println(d + " " + Math.min(a % d, d - a % d)); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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); } } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; 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; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.TreeMap; public class CodeForcesB { static HashMap<String,Integer> hm; 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[] nextArray(int n) { int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=nextInt(); return arr; } } static boolean palin(char[] arr) { if(arr.length==1) return false; for(int i=0;i<arr.length/2;i++) if(arr[i]!=arr[arr.length-1-i]) return false; return true; } 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 boolean palin(String all) { for(int i=0;i<all.length()-1;i++) { for(int j=0;j<=i;j++) { if(palin(all.substring(j,all.length()-i+j).toCharArray())) return true; } } return false; } static void helper(int n,int[] arr,StringBuilder sb) { int max=0; for(int i=1;i<n;i++) { if(arr[i]-arr[max]>0) max=i; } for(int i=max;i<n;i++) sb.append(arr[i]+" "); if(max>0) helper(max, arr, sb); } static boolean helper(String text,String ans,HashSet<String> hs) { if(text.equals(ans)) return true; if(hs.contains(text)) return false; hs.add(text); int c=0,c2=0; char[] arr=text.toCharArray(); StringBuilder sb=new StringBuilder(); for(int i=0;i<arr.length;i++) { char ch=arr[i]; if(ch=='1') { c++; sb.append(0); } else { c--; sb.append(1); } if(arr[i]!=ans.charAt(i)) c2++; if(c==0&&c2%2==0) { if(i==arr.length-1) { if(helper(sb.toString(), ans, hs)) return true; } else { if(helper(sb.toString()+text.substring(i+1), ans, hs)) return true; } } } return false; } static boolean helper(String text,String ans,int i,HashSet<String> hs,PriorityQueue<Integer> pq) { if(ans.equals(text)) return true; if(hs.contains(text)||i==-1) return false; int curr=pq.remove(); String change=text.substring(0,curr+1); int num=Integer.parseInt(change,2); String changed=(~num)+""; int prev=-1; if(!pq.isEmpty()) prev=pq.peek(); return helper(changed+text.substring(i+1), ans,prev, hs,pq)||helper(text, ans,prev, hs,pq); } public static <K, V extends Comparable<V> > TreeMap<K, V> valueSort( Map<K, V> map) { // Static Method with return type Map and // extending comparator class which compares values // associated with two keys Comparator<K> valueComparator = new Comparator<K>() { public int compare(K k1, K k2) { int comp = map.get(k1).compareTo(map.get(k2)); if (comp >= 0) return 0; else return comp; } }; System.out.println(map); TreeMap<K, V> sorted = new TreeMap<K, V>(valueComparator); sorted.putAll(map); return sorted; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Entry<Integer, Integer>> list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { if (o1.getValue()==o2.getValue()) return o1.getKey()-o2.getKey(); return -o1.getValue()+o2.getValue(); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int helper(int[] arr,int i,long curr) { if(curr<0) return -1; if(i==arr.length) return 0; // System.out.println(i+","+curr); if(arr[i]>=0) return 1+helper(arr, i+1, arr[i]+curr); else { // System.out.println(i+","+Math.max(helper(arr, i+1, curr),1+helper(arr, i+1,arr[i]+ curr))); return Math.max(helper(arr, i+1, curr),1+helper(arr, i+1,arr[i]+ curr)); } } static boolean helper(int a,int b,int n,HashSet<Integer> hs) { if(n<=0) return false; if(n==1||n==a) return true; if(hs.contains(n)) return false; double check=Math.log10(n)/Math.log10(a); if(Math.ceil(check)==check) return true; check=Math.log10(n)/Math.log10(b+1); if(Math.ceil(check)==check) return true; while(n>0) { hs.add(n); double ans=Math.log10(n)/Math.log10(a); if(Math.ceil(ans)==ans) return true; n-=b; } return false; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader fs=new FastReader(); PrintWriter out=new PrintWriter(System.out); //out.print("111"); int T=fs.nextInt(); StringBuilder sb=new StringBuilder(); while(T-->0) { // int a=fs.nextInt(),b=fs.nextInt(); long a=fs.nextLong(),b=fs.nextLong(); long ans=Math.abs(a-b); if(ans==0) sb.append("0 0"); else { long min=Math.min(a%ans,ans-a%ans); sb.append(ans+" "+min); } // boolean[] dp=new boolean[n+1]; 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
import sys import math #sys.stdin=open('input.txt','r') #sys.stdout=open('output.txt','w') def solve(): #n=int(input()) a,b=map(int,input().split()) if(a==b): print("0 0") else: r=abs(a-b) if(math.gcd(a,b)==r): print(r,0) else: e=a//r y1=((e+1)*r)-a y2=abs(((e)*r)-a) print(abs(a-b),min(y1,y2)) t=int(input()) while(t!=0): solve() t-=1
PYTHON3
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
# link: https://codeforces.com/contest/1543/problem/0 import os, sys, math 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 math import ceil mod = 10 ** 9 + 7 # number of test cases for _ in range(int(input())): a,b = map(int, input().split()) if a==b: print(0,0) else: if a == b-1 or b == a-1: print(1,0) else: aa = a bb = b if aa < bb: bb = bb - aa aa = 0 else: aa = aa - bb bb = 0 first_num = aa second_num = bb if first_num == 0: intial_gcd = second_num else: intial_gcd = first_num # for first number if a%intial_gcd == 0 and b%intial_gcd == 0: print(intial_gcd, 0) else: s = 0 e = pow(10, 18) till = 0 while s <= e: m = s + (e - s) // 2 if intial_gcd*m >= a: e = m - 1 till = intial_gcd * m else: s = m + 1 m1 = abs(till - a) s = 0 e = pow(10, 18) till = 0 while s <= e: m = s + (e - s) // 2 if m*intial_gcd <= a: s = m + 1 till = intial_gcd * m else: e = m - 1 print(intial_gcd, min(abs(till - a), m1))
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 = [int(__) for __ in input().split()] if a == b: print(0, 0) continue shit = abs(a - b) x = min(a, b) print(shit, min(x % shit, shit - (x % shit)))
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.math.BigInteger; import java.util.*; public class GCDEam { public static void main(String[] args) { handmadeInput.FastReader in = new handmadeInput.FastReader(); int t = in.nextInt(); StringBuilder stringBuilder = new StringBuilder(); while (t-- > 0) { long l = in.nextLong(); long l2 = in.nextLong(); long max = Math.max(l, l2); long min = Math.min(l, l2); long gcd = max - min; if (min == gcd) { stringBuilder.append(gcd).append(" ").append(0).append(" "); } else if (gcd == 0) { stringBuilder.append(gcd).append(" ").append(0).append(" "); } else { long steps = (max / gcd) * gcd; steps = Math.abs(steps - max); long steps2 = (long) Math.ceil(max / (double) gcd)*gcd; steps2 = Math.abs(steps2 - max); steps = Math.min(steps2, steps); stringBuilder.append(gcd).append(" ").append(steps).append(" "); } } System.out.println(stringBuilder); } } // Additional Classes go here class handmadeInput { 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; } } } class UtilityFunctions { //! returns gcd of given numbers public int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public int primeFactors(int n) { HashMap<Integer, Integer> hashMap = new HashMap<>(n); int c = 0; // Print the number of 2s that divide n while (n % 2 == 0) { // if (hashMap.containsKey(2)) { // hashMap.replace(2, hashMap.get(2) + 1); // } else { // hashMap.put(2, 1); // } c++; n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { //System.out.print(i + " "); // if (hashMap.containsKey(i)) { // hashMap.replace(i, hashMap.get(i) + 1); // } else { // hashMap.put(i, 1); // } c++; n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) { // if (hashMap.containsKey(n)) { // hashMap.replace(n, hashMap.get(n) + 1); // } else { // hashMap.put(n, 1); // } c++; } //System.out.print(n); return c; } }
JAVA
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
/* * Author:- Tanmay */ import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int t=1; int t=sc.nextInt(); // outer: while(t-- >0) { long a = sc.nextLong(), b = sc.nextLong(); if(a == b) out.println("0 0"); else { long x = Math.abs(a-b); long ans = Math.min(a%x, x-a%x); out.println(x+" "+ans); } } out.flush(); out.close(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int [] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } long [] longArray(int n) { long[] a=new long[n]; for(int i=0 ; i<n ; i++) a[i]=nextLong(); return a; } double nextDouble() { return Double.parseDouble(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
# cf730div2 A ''' x + t = (y + t) * k = z x = y*k + t * (k-1) ''' T = int(input().strip()) for cas in range(T) : x,y = map(int,input().strip().split()) if (x==y) : print("0 0"); continue; if (x<y) : x,y = y,x; print("%d %d" %(x-y,min(y%(x-y),(x-y)-y%(x-y))))
PYTHON3
1543_A. Exciting Bets
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. To make the race more exciting, you can perform two types of operations: 1. Increase both a and b by 1. 2. Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that gcd(x,0)=x for any x ≥ 0. Input The first line of input contains a single integer t (1≤ t≤ 5⋅ 10^3) — the number of test cases. The first and the only line of each test case contains two integers a and b (0≤ a, b≤ 10^{18}). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Example Input 4 8 5 1 2 4 4 3 9 Output 3 1 1 0 0 0 6 3 Note For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.
2
7
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (a); i > (b); --i) #define ALL(x) x.begin(), x.end() #define F first #define S second #define PB push_back #define MP make_pair using namespace std; using ll = long long; using ld = long double; typedef vector<int> vi; typedef pair<int, int> pi; void fast_io() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } /* ---------- Code here ---------- */ void solve() { ll a, b; cin >> a >> b; if (a == b) { cout << "0 0\n"; return; } ll x = max(a, b), y = min(a, b); if (a == 0 || b == 0) { cout << x << " 0\n"; return; } ll z = x - y; ll q = x % z, w = z - q; cout << z << " " << min(q, w) << "\n"; } /* ---------- Main here ---------- */ int main() { fast_io(); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int 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
// package com.company /* * @author :: Yuvraj Singh * CS UNDERGRAD AT IT GGV BILASPUR */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.lang.System.out; import static java.lang.Long.MAX_VALUE; public final class Main{ FastReader in; StringBuffer sb; public static void main(String[] args) { new Main().run(); } void run(){ in= new FastReader(); start(); } void start(){ sb= new StringBuffer(); for(int t=in.nextInt();t>0;t--) solve(); out.print(sb); } void solve(){ long a= in.nextLong(); long b= in.nextLong(); long p= abs(a-b); if(p==0){ sb.append(0).append(" ").append(0).append(" "); return; } sb.append(p).append(" "); sb.append(min(p-a%p, a%p)).append(" "); } int upper_bound(long[] arr, int key) { int i=0, j=arr.length-1; if (arr[j]<=key) return j+1; if(arr[i]>key) return i; while (i<j){ int mid= (i+j)/2; if(arr[mid]<=key){ i= mid+1; }else{ j=mid; } } return i; } void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } long[] longArr(int n){ long[] res= new long[n]; for(int i=0;i<n;i++){ res[i]= in.nextLong(); } return res; } // sieve of eratosthenes code for precomputing whether numbers are prime or not up to MAX_VALUE long MAX= MAX_VALUE; int[] precomp= new int[(int) (MAX+1)]; void sieve(){ long n= MAX; boolean[] prime = new boolean[(int) (n+1)]; for(int i=0;i<=n;i++) prime[i] = true; for(long p = 2; p*p <=n; p++) { // If prime[p] is not changed, then it is a prime if(prime[(int) p]) { // Update all multiples of p for(long i = p*p; i <= n; i += p) prime[(int) i] = false; } } // Print all prime numbers for(long i = 2; i <= n; i++) { if(prime[(int) i]) precomp[(int) i]= 1; } } boolean isDigitSumPalindrome(long N) { // code here long sum= sumOfDigits(String.valueOf(N)); long rev=0; long org= sum; while (sum!=0){ long d= sum%10; rev = rev*10 +d; sum /= 10; } return org == rev; } long sumOfDigits(String n){ long sum= 0; for (char c: n.toCharArray()){ sum += Integer.parseInt(String.valueOf(c)); } return sum; } long[] revArray(long[] arr) { int n= arr.length; int i=0, j=n-1; while (i<j){ long temp= arr[i]; arr[i]= arr[j]; arr[j]= temp; i++; j--; } return arr; } long gcd(long a, long b){ if (b==0) return a; return gcd(b, a%b); } long lcm(long a,long b){ return (a*b)/gcd(a,b); } static class Pair{ long first; long second; Pair(long x,long y){ this.first=x; this.second=y; } } static class Compare { static void compare(ArrayList<Pair> arr, long n) { arr.sort(new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return (int) (p1.first - p2.first); } }); } } public static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st==null || !st.hasMoreElements()){ try{ st=new StringTokenizer(br.readLine()); }catch (Exception e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } float nextFloat(){ return Float.parseFloat(next()); } String nextLine(){ String str=""; try{ str=br.readLine(); }catch (Exception e){ e.printStackTrace(); } return str; } } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) { cout << 1; return 0; } cout << n * (n - 1) * 6 + 1; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; const int N = int(300000) + 5; long long int k, n, m, t, cnt, sum, ans, x, y; long long int a[N], b[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; if (n == 1) { cout << 1; return 0; } cnt = 1; while (n > 1) { sum += 12; cnt += sum; n--; } cout << cnt; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int ans = 1, n; int main() { cin >> n; ans = (2 * n - 1) * (2 * n - 1) + 4 * (n - 1) * (n) / 2; cout << ans; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int res = 1; for (int i = 1, t = 12; i < n; i++, t += 12) res += t; cout << res << endl; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> int main(int argc, char *argv[]) { int n; scanf("%d", &n); printf("%d\n", 6 * n * (n - 1) + 1); return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> int main() { int n, a[20000], i, s = 0; scanf("%d", &n); a[1] = 1; for (i = 2; i <= n; i++) { a[i] = 12 * (i - 1); s += a[i]; } printf("%d", s + 1); return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
def star(n): a, b = 1, 12 for i in range(2, n + 1): a += b b += 12 return a print(star(int(input())))
PYTHON3
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class B { int INF = 1 << 28; void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long sum = 1; if( n == 1 ) { System.out.println(1); return; } for(int i=2;i<=n;i++) { sum += (long)(i-1) * 12; } System.out.println(sum); } public static void main(String[] args) { new B().run(); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> int main() { long long int n, i, c = 0; scanf("%I64d", &n); printf("%I64d", 6 * n * (n - 1) + 1); return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main() { int m, n, i, j, k, a, b; scanf("%d", &a); if (a == 1) printf("1\n"); else { printf("%d\n", (a - 1) * a / 2 * 12 + 1); } }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> const long long int INF = 1e15; using namespace std; const long long int MAX = 1e7 + 5; const long long int mod = 1e9 + 7; long long int gcd(long long int a, long long int b) { while (b > 0) { a = a % b; swap(a, b); } return a; } long long int binpow(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b % 2 == 1) res = res * a % mod; a = a * a % mod; b /= 2; } return res % mod; } void solve() { long long int n; cin >> n; long long int ans = 3 * n * n - 2 + 3 * (n - 1) * (n - 1); cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; for (long long int i = 1; i <= t; i++) { solve(); } }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << 1 + 6 * n * (n - 1); return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> int main() { int n, aux; scanf("%d", &n); aux = 1; for (int i = 2; i <= n; i++) aux += 12 * (i - 1); printf("%d", aux); return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
n = int(input()) sum = (12 + 12*(n-1)) * (n - 2 + 1) / 2 print sum + 1
PYTHON
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; long star(int a) { if (a == 1) return 1; else return star(a - 1) + 12 * (a - 1); } int main() { int n; cin >> n; cout << star(n) << endl; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); printf("%d", 6 * n * (n - 1) + 1); return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1}; int dr_y[] = {-1, 0, 1, 0}; int dc_x[] = {0, 1, 0, -1}; int f[100000]; using namespace std; void init() { int i; f[1] = 1; for (int i = 2; i <= 18257; i++) f[i] = f[i - 1] + (i - 1) * 12; } int main() { int n; init(); while (scanf("%d", &n) != EOF) printf("%d\n", f[n]); return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; import java.text.*; public class Prac{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static class Key { private final int x; private final int y; public Key(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Key)) return false; Key key = (Key) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static PrintWriter w = new PrintWriter(System.out); static long mod=998244353L,mod1=1000000007; static int co[][]; static void dsort(){ Arrays.parallelSort(co,(o1,o2)->o1[0]!=o2[0]?o1[0]-o2[0]:o1[1]-o2[1]); } static long power(long x, long y) { long temp=0; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return (temp*temp); else return (((x*temp))*temp); } static long modInverse(long a, long m){ long num=m; long x=1; long power=a; while(num>0){ if(num%2==1){ x=(x*power); } num>>=1; power=(power*power); } return x; } static long ncr(int n,int r){ if(n<r)return 0L; long ans=fact[n]/(fact[r]*fact[n-r]); return ans; } static long fact[]=new long[1000005]; public static void findFact(){ fact[0]=1; for(int i=1;i<=1000000;i++){ fact[i]=(i*fact[i-1])%mod; } } static long nCr(int n,int r){ if(n<r)return 0L; return (fact[n]*modInverse((fact[r]*fact[n-r])%mod,mod-2))%mod; } public static void main (String[] args)throws IOException{ InputReader sc=new InputReader(System.in); int n=sc.ni(); w.println(6*n*(n-1)+1); w.close(); } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
import java.io.*; import java.util.*; import java.math.*; public class Main { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer=null; public static void main(String[] args) throws IOException { new Main().execute(); } void debug(Object...os) { System.out.println(Arrays.deepToString(os)); } int ni() throws IOException { return Integer.parseInt(ns()); } long nl() throws IOException { return Long.parseLong(ns()); } double nd() throws IOException { return Double.parseDouble(ns()); } String ns() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(br.readLine()); return tokenizer.nextToken(); } String nline() throws IOException { tokenizer=null; return br.readLine(); } //Main Code starts Here int totalCases, testNum; int n; void execute() throws IOException { totalCases = 1; for(testNum = 1; testNum <= totalCases; testNum++) { if(!input()) break; solve(); } } void solve() throws IOException { System.out.println(1+6*n*(n-1)); } boolean input() throws IOException { n = ni(); return true; } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; public class Main{ public static void main(String[] args) throws Exception { int a = nextInt(); int ans = 1; for(int i = 1; i < a; i++){ ans = ans + 12*i; } exit(ans); } private static PrintWriter out; private static BufferedReader inB; private static boolean FILE = false; static { try { out = new PrintWriter(FILE ? (new FileOutputStream("output.txt")) : System.out); inB = new BufferedReader(new InputStreamReader(FILE ? new FileInputStream("input.txt") : System.in)); } catch(Exception e) {e.printStackTrace();} } private static StreamTokenizer in = new StreamTokenizer(inB); private static void exit(Object o) throws Exception { out.println(o); out.flush(); System.exit(0); } private static void println(Object o) throws Exception{ out.println(o); out.flush(); } private static void print(Object o) throws Exception{ out.print(o); out.flush(); } private static int nextInt() throws Exception { in.nextToken(); return (int)in.nval; } private static String nextString() throws Exception { in.nextToken(); return in.sval; } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << 6 * n * (n - 1) + 1 << endl; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
import java.util.Scanner; public class ProblemB { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); long sum = 1; for(int i = 1; i < a; i++){ sum += 12*i; } System.out.println(sum); } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; cout << (n * (n - 1) * 6) + 1 << endl; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("O8") #pragma GCC optimization("unroll-loops") using namespace std; const long long int N = 1e6 + 20, mod = 1e9 + 7, inf = 2e18, dlt = 12250, maxm = 524288; int main() { long long n; cin >> n; cout << 1 + 6 * n * (n - 1); }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
import java.util.Locale; import java.util.Scanner; public class StarSolver { private int n; public static void main(String[] args) { StarSolver solver = new StarSolver(); solver.readData(); int solution = solver.solve(); solver.print(solution); } private int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } private int lcm(int a, int b) { return a * b / gcd(a, b); } private void print(int[] values) { StringBuilder builder = new StringBuilder(); for (int value : values) { builder.append(value); builder.append(" "); } print(builder); } private void print(Object value) { System.out.println(value); } private void print(boolean value) { System.out.println(value ? "YES" : "NO"); } private void print(int value) { System.out.println(value); } private void print(long value) { System.out.println(value); } private void print(double value) { System.out.printf(Locale.ENGLISH, "%.10f", value); } private int[] getDigits(int number) { int[] digits = new int[10]; int index = digits.length - 1; int digitsCount = 0; while (number > 0) { digits[index] = number % 10; number /= 10; index--; digitsCount++; } int[] result = new int[digitsCount]; System.arraycopy(digits, digits.length - digitsCount, result, 0, digitsCount); return result; } private int[] readArray(Scanner scanner, int size) { int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = scanner.nextInt(); } return result; } private void readData() { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); } private int solve() { int result = 6 * n * (n - 1) + 1; return result; } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long sl, sB, k; cin >> k; sl = k - 1; sB = k * 3 - 2; cout << ((sB + 1) * sB) / 2 + 3 * ((sl + 1) * sl) / 2; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
import static java.lang.Double.parseDouble; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class B { static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static void solve() throws Exception { int a = nextInt(); out.println(1 + 6L * a * (a - 1)); } static int nextInt() throws IOException { return parseInt(next()); } static long nextLong() throws IOException { return parseLong(next()); } static double nextDouble() throws IOException { return parseDouble(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
import java.util.Scanner; public class B_Star { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int n=sc.nextInt(); int p=0; for (int i = 1; i <n; i++) { p=p+(12*i); } System.out.println(p+1); } }
JAVA
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; int main(int argc, const char *argv[]) { int n; cin >> n; cout << 12 * (n - 1) + 6 * (n - 1) * (n - 2) + 1; return 0; }
CPP
171_B. Star
<image> Input The input contains a single integer a (1 ≤ a ≤ 18257). Output Print a single integer output (1 ≤ output ≤ 2·109). Examples Input 2 Output 13
2
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { return (y == 0) ? x : gcd(y, x % y); } unsigned long long lcm(long long x, long long y) { return x / gcd(x, y) * y; } void the_end() {} int dy8[] = {0, 0, 1, -1, 1, -1, -1, 1}; int dx8[] = {1, -1, 0, 0, 1, -1, 1, -1}; int id8[] = {0, 1, 2, 3, 4, 5, 6, 7}; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long a; cin >> a; cout << 6 * a * (a - 1) + 1 << '\n'; the_end(); }
CPP