📜  小于或等于N的A,B或C的倍数计数

📅  最后修改于: 2021-04-28 00:09:19             🧑  作者: Mango

给定四个整数NABC。任务是从[1,N]范围内找到整数,该整数可以被ABC整除。

例子:

方法:一种有效的方法是使用集合论的概念。因为我们必须找到可以被a或b或c整除的数字。
  \begin{document} \begin{itemize} \item Let $n(a) \colon $ count of numbers divisible by a. \item Let $n(b) \colon $ count of numbers divisible by b. \item Let $n(c) \colon $ count of numbers divisible by c. \item $n(a \bigcap b) \colon $ count of numbers divisible by a and b. \item $n(a \bigcap c) \colon $ count of numbers divisible by b and c. \item $n(b \bigcap c) \colon $ count of numbers divisible by c and a. \item $n(a \bigcap b \bigcap c) \colon $ count of numbers divisible by a and b and c. \end{itemize}  According to set theory,  $n\left( a \bigcup b \bigcup c \right)=n(a)+n(b)+n(c)-n(a \bigcap b)-n(b \bigcap c)-n(a \bigcap c)+n(a \bigcap b \bigcap c)$ \end{document}
所以。被A,B或C整除的数字计数为(num / A)+(num / B)+(num / C)–(num / lcm(A,B))–(num / lcm(A,B) ))–(num / lcm(A,C))+ –(num / lcm(A,B,C))

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the
// gcd of a and b
long gcd(long a, long b)
{
    if (a == 0)
        return b;
  
    return gcd(b % a, a);
}
  
// Function to return the count of integers
// from the range [1, num] which are
// divisible by either a, b or c
long divTermCount(long a, long b, long c, long num)
{
    // Calculate the number of terms divisible by a, b
    // and c then remove the terms which are divisible
    // by both (a, b) or (b, c) or (c, a) and then
    // add the numbers which are divisible by a, b and c
    return ((num / a) + (num / b) + (num / c)
            - (num / ((a * b) / gcd(a, b)))
            - (num / ((c * b) / gcd(c, b)))
            - (num / ((a * c) / gcd(a, c)))
            + (num / ((a * b * c) / gcd(gcd(a, b), c))));
}
  
// Driver code
int main()
{
    long a = 7, b = 3, c = 5, n = 100;
  
    cout << divTermCount(a, b, c, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
      
class GFG
{
      
// Function to return the
// gcd of a and b
static long gcd(long a, long b)
{
    if (a == 0)
        return b;
  
    return gcd(b % a, a);
}
  
// Function to return the count of integers
// from the range [1, num] which are
// divisible by either a, b or c
static long divTermCount(long a, long b, 
                         long c, long num)
{
    // Calculate the number of terms divisible by a, b
    // and c then remove the terms which are divisible
    // by both (a, b) or (b, c) or (c, a) and then
    // add the numbers which are divisible by a, b and c
    return ((num / a) + (num / b) + (num / c) - 
                (num / ((a * b) / gcd(a, b))) - 
                (num / ((c * b) / gcd(c, b))) - 
                (num / ((a * c) / gcd(a, c))) + 
                (num / ((a * b * c) / gcd(gcd(a, b), c))));
}
  
// Driver code
static public void main (String []arr)
{
    long a = 7, b = 3, c = 5, n = 100;
  
    System.out.println(divTermCount(a, b, c, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
  
# Function to return the 
# gcd of a and b 
def gcd(a, b) : 
  
    if (a == 0) :
        return b; 
  
    return gcd(b % a, a); 
  
# Function to return the count of integers 
# from the range [1, num] which are 
# divisible by either a, b or c 
def divTermCount(a, b, c, num) : 
  
    # Calculate the number of terms divisible by a, b 
    # and c then remove the terms which are divisible 
    # by both (a, b) or (b, c) or (c, a) and then 
    # add the numbers which are divisible by a, b and c 
    return ((num // a) + (num // b) + (num // c) - 
                 (num // ((a * b) // gcd(a, b))) - 
                 (num // ((c * b) // gcd(c, b))) - 
                 (num // ((a * c) // gcd(a, c))) + 
                 (num // ((a * b * c) // gcd(gcd(a, b), c)))); 
  
# Driver code 
if __name__ == "__main__" : 
  
    a = 7; b = 3; c = 5; n = 100; 
  
    print(divTermCount(a, b, c, n)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation for above approach
using System;
      
class GFG
{
      
// Function to return the
// gcd of a and b
static long gcd(long a, long b)
{
    if (a == 0)
        return b;
  
    return gcd(b % a, a);
}
  
// Function to return the count of integers
// from the range [1, num] which are
// divisible by either a, b or c
static long divTermCount(long a, long b, 
                         long c, long num)
{
    // Calculate the number of terms divisible by a, b
    // and c then remove the terms which are divisible
    // by both (a, b) or (b, c) or (c, a) and then
    // add the numbers which are divisible by a, b and c
    return ((num / a) + (num / b) + (num / c) - 
            (num / ((a * b) / gcd(a, b))) - 
            (num / ((c * b) / gcd(c, b))) - 
            (num / ((a * c) / gcd(a, c))) + 
            (num / ((a * b * c) / gcd(gcd(a, b), c))));
}
  
// Driver code
static public void Main (String []arr)
{
    long a = 7, b = 3, c = 5, n = 100;
  
    Console.WriteLine(divTermCount(a, b, c, n));
}
}
  
// This code is contributed by 29AjayKumar


输出:
55