📜  找出第n个可被a或b或c整除的项

📅  最后修改于: 2021-04-29 03:07:39             🧑  作者: Mango

给定四个整数abcN。任务是找到第n可被abc整除的项。

例子:

天真的方法:一种简单的方法是遍历从1开始的所有项,直到找到所需的N项,该项可以被abc整除。该解决方案的时间复杂度为O(N)。

高效的方法:这个想法是使用二进制搜索。在这里,我们可以使用以下公式计算从1num的数可以被abc整除: (num / a)+(num / b)+(num / c)–(num / lcm(a, b))–(num / lcm(b,c))–(num / lcm(a,c))+(num / lcm(a,b,c)))
上面的公式是使用集合理论推导的

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return
// gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
  
    return gcd(b % a, a);
}
  
// Function to return the lcm of a and b
int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}
  
// Function to return the count of numbers
// from 1 to num which are divisible by a, b or c
int divTermCount(int a, int b, int c, int num)
{
  
    // Calculate number of terms divisible by a and
    // by b and by c then, remove the terms which is are
    // divisible by both a and b, both b and c, both
    // c and a and then add which are divisible by a and
    // b and c
    return ((num / a) + (num / b) + (num / c)
            - (num / lcm(a, b))
            - (num / lcm(b, c))
            - (num / lcm(a, c))
            + (num / lcm(a, lcm(b, c))));
}
  
// Function to find the nth term
// divisible by a, b or c
// by using binary search
int findNthTerm(int a, int b, int c, int n)
{
  
    // Set low to 1 and high to max (a, b, c) * n
    int low = 1, high = INT_MAX, mid;
  
    while (low < high) {
        mid = low + (high - low) / 2;
  
        // If the current term is less than
        // n then we need to increase low
        // to mid + 1
        if (divTermCount(a, b, c, mid) < n)
            low = mid + 1;
  
        // If current term is greater than equal to
        // n then high = mid
        else
            high = mid;
    }
  
    return low;
}
  
// Driver code
int main()
{
    int a = 2, b = 3, c = 5, n = 10;
  
    cout << findNthTerm(a, b, c, n);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG
{
      
    // Function to return 
    // gcd of a and b 
    static int gcd(int a, int b) 
    { 
        if (a == 0) 
            return b; 
          
        return gcd(b % a, a); 
    } 
  
    // Function to return the lcm of a and b 
    static int lcm(int a, int b) 
    { 
        return (a * b) / gcd(a, b); 
    } 
  
    // Function to return the count of numbers 
    // from 1 to num which are divisible by a, b or c 
    static int divTermCount(int a, int b, int c, int num) 
    { 
  
        // Calculate number of terms divisible by a and 
        // by b and by c then, remove the terms which is are 
        // divisible by both a and b, both b and c, both 
        // c and a and then add which are divisible by a and 
        // b and c 
        return ((num / a) + (num / b) + (num / c) 
                - (num / lcm(a, b)) 
                - (num / lcm(b, c)) 
                - (num / lcm(a, c)) 
                + (num / lcm(a, lcm(b, c)))); 
    } 
  
    // Function to find the nth term 
    // divisible by a, b or c 
    // by using binary search 
    static int findNthTerm(int a, int b, int c, int n) 
    { 
  
        // Set low to 1 and high to max (a, b, c) * n 
        int low = 1, high = Integer.MAX_VALUE, mid; 
  
        while (low < high) { 
            mid = low + (high - low) / 2; 
  
            // If the current term is less than 
            // n then we need to increase low 
            // to mid + 1 
            if (divTermCount(a, b, c, mid) < n) 
                low = mid + 1; 
      
            // If current term is greater than equal to 
            // n then high = mid 
            else
                high = mid; 
        } 
  
        return low; 
    } 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        int a = 2, b = 3, c = 5, n = 10; 
  
        System.out.println(findNthTerm(a, b, c, n)); 
  
    } 
}
  
// This code is contributed by
// Rajnis09


Python
# Python3 implementation of the approach
  
# Function to return
# gcd of a and b
def gcd(a, b):
  
    if (a == 0):
        return b
  
    return gcd(b % a, a)
  
# Function to return the lcm of a and b
def lcm(a, b):
  
    return ((a * b) // gcd(a, b))
  
  
# Function to return the count of numbers
# from 1 to num which are divisible by a, b or c
def divTermCount(a, b, c, num):
  
    # Calculate number of terms divisible by a and
    # by b and by c then, remove the terms which is are
    # divisible by both a and b, both b and c, both
    # c and a and then add which are divisible by a and
    # b and c
    return ((num // a) + (num // b) + (num // c)
            - (num // lcm(a, b))
            - (num // lcm(b, c))
            - (num // lcm(a, c))
            + (num // lcm(a, lcm(b, c))))
  
  
# Function to find the nth term
# divisible by a, b or c
# by using binary search
def findNthTerm(a, b, c, n):
  
  
    # Set low to 1 and high to max (a, b, c) * n
    low = 1
    high = 10**9
    mid=0
  
    while (low < high):
        mid = low + (high - low) // 2
  
        # If the current term is less than
        # n then we need to increase low
        # to mid + 1
        if (divTermCount(a, b, c, mid) < n):
            low = mid + 1
  
        # If current term is greater than equal to
        # n then high = mid
        else:
            high = mid
  
  
    return low
  
# Driver code
a = 2
b = 3
c = 5
n = 10
  
print(findNthTerm(a, b, c, n))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach 
using System;
      
class GFG
{
      
    // Function to return 
    // gcd of a and b 
    static int gcd(int a, int b) 
    { 
        if (a == 0) 
            return b; 
          
        return gcd(b % a, a); 
    } 
  
    // Function to return the lcm of a and b 
    static int lcm(int a, int b) 
    { 
        return (a * b) / gcd(a, b); 
    } 
  
    // Function to return the count of numbers 
    // from 1 to num which are divisible by a, b or c 
    static int divTermCount(int a, int b, int c, int num) 
    { 
  
        // Calculate number of terms divisible by a and 
        // by b and by c then, remove the terms which is are 
        // divisible by both a and b, both b and c, both 
        // c and a and then add which are divisible by a and 
        // b and c 
        return ((num / a) + (num / b) + (num / c) 
                - (num / lcm(a, b)) 
                - (num / lcm(b, c)) 
                - (num / lcm(a, c)) 
                + (num / lcm(a, lcm(b, c)))); 
    } 
  
    // Function to find the nth term 
    // divisible by a, b or c 
    // by using binary search 
    static int findNthTerm(int a, int b, int c, int n) 
    { 
  
        // Set low to 1 and high to max (a, b, c) * n 
        int low = 1, high = int.MaxValue, mid; 
  
        while (low < high) { 
            mid = low + (high - low) / 2; 
  
            // If the current term is less than 
            // n then we need to increase low 
            // to mid + 1 
            if (divTermCount(a, b, c, mid) < n) 
                low = mid + 1; 
      
            // If current term is greater than equal to 
            // n then high = mid 
            else
                high = mid; 
        } 
  
        return low; 
    } 
  
    // Driver code 
    public static void Main(String[] args) 
    { 
        int a = 2, b = 3, c = 5, n = 10; 
  
        Console.WriteLine(findNthTerm(a, b, c, n)); 
  
    } 
}
  
/* This code is contributed by PrinciRaj1992 */


输出:
14