n-th integer divisible by a or b#9018

n-th integer divisible by a or b (Editoryal)

Let the function compute the number of positive integers in the range that are divisible by either or . This value can be computed using the formula:

n / a + n / b - n / НОК(a, b)

Let be the -th number that is divisible by either or . Then is the smallest positive integer such that f(x) = n. We will find using binary search on the interval [left; right] = [1; 10^9]. At each step, compute mid = (left + right) / 2. If f(mid) \ge n, continue searching in the left subinterval [left; mid]; otherwise, search in the right subinterval [mid + 1; right].

Example

Consider the first example where a = 2 and b = 5. We need to find the smallest value of such that f(x) = 10. Let's compute the values of the function for some values of .

The smallest solution to the equation f(x) = 10 is x = 16.

Algorithm implementation

The functions gcd and lcm compute the greatest common divisor and the least common multiple, respectively.

long long gcd(long long a, long long b)
{
  return (b == 0) ? a : gcd(b, a % b);
}

long long lcm(long long a, long long b)
{
  return a / gcd(a, b) * b;
}

The function returns the number of positive integers less than or equal to that are divisible by either or .

long long f(long long n)
{
  return n / a + n / b - n / lcm(a, b);
}

Read the input data.

scanf("%lld %lld %lld", &a, &b, &n);

Perform a binary search on the interval [left; right] = [1; 10^9]. The goal is to find the smallest value of such that f(left) = n.

left = 1; right = 1e9;
while (left < right)
{
  middle = (left + right) / 2;
  if (f(middle) >= n)
    right = middle;
  else
    left = middle + 1;
}

Print the answer.

printf("%lld\n", left);

Java implementation

import java.util.*;
import java.io.*;

public class Main 
{
  static long gcd(long a, long b)
  {
    return (b == 0) ? a : gcd(b, a % b);
  }

  static long lcm(long a, long b)
  {
     return a / gcd(a, b) * b;
  }

  static long f(long n, long a, long b)
  {
    return n / a + n / b - n / lcm(a, b);
  }

  public static void main(String[] args) 
  {
    FastScanner con = new FastScanner(new InputStreamReader(System.in));    
    long a = con.nextInt();
    long b = con.nextInt();
    long n = con.nextInt();

    long left = 1, right = 1000000000;
    while (left < right)
    {
      long middle = (left + right) / 2;
      if (f(middle, a, b) >= n)
        right = middle;
      else
        left = middle + 1;
    }
    
    System.out.println(left);
  }
}

class FastScanner
{
  private BufferedReader br;
  private StringTokenizer st;
 
  public FastScanner(InputStreamReader reader) 
  {
    br = new BufferedReader(reader);
  }
 
  public String next() 
  {
    while (st == null || !st.hasMoreTokens()) 
    {
      try 
      {
        st = new StringTokenizer(br.readLine());
      } catch (Exception e) 
      {
        e.printStackTrace();
      }
    }
    return st.nextToken();
  }

  public int nextInt() 
  {
    return Integer.parseInt(next());
  }
  
  public void close() throws Exception 
  {
    br.close();
  }
}