给定四个整数N , A , B和C。任务是从[1,N]范围内找到整数,该整数可以被A , B或C整除。
例子:
Input: A = 2, B = 3, C = 5, N = 10
Output: 8
2, 3, 4, 5, 6, 8, 9 and 10 are the only number from the
range [1, 10] which are divisible by wither 2, 3 or 5.
Input: A = 7, B = 3, C = 5, N = 100
Output: 55
方法:一种有效的方法是使用集合论的概念。因为我们必须找到可以被a或b或c整除的数字。
- 设n(a):被a整除的数的计数。
- 令n(b):被b整除的数的计数。
- 令n(c):可被c整除的数字计数。
- n(a∩b):被a和b整除的数字计数。
- n(a∩c):被a和c整除的数字计数。
- n(b∩c):被b和c整除的数字计数。
- n(a b b c):被a和b与c整除的数字计数。
根据集合论
n(a b c)= n(a)+ n(b)+ n(c)– n(a b)– n(b c)– n(a c)+ n(a b ∩c)
所以。被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);
def lcm (x, y):
return (x * y) // gcd (x, y)
# 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 // lcm(a, b) -
num // lcm(c, b) -
num // lcm(a, c) +
num // (lcm(lcm(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
Javascript
输出:
55
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。