给定四个整数N , A , B和C。任务是打印包含A , B或C的倍数的集合中的第N个数字。
例子:
Input: A = 2, B = 3, C = 5, N = 8
Output: 10
2, 3, 4, 5, 6, 8, 9, 10, 12, 14, …
Input: A = 2, B = 3, C = 5, N = 100
Output: 136
天真的方法:从1开始遍历,直到找到第N个元素,该元素可以被A , B或C整除。
高效的方法:给定一个数字,我们可以找到A , B或C的除数的数量。现在,可以使用二进制搜索来找到第N个可被A , B或C整除的数字。
所以,如果数字是num
计数=(num / A)+(num / B)+(num / C)–(num / lcm(A,B))–(num / lcm(C,B))–(num / lcm(A,C) ))–(num / lcm(A,B,C))
下面是上述方法的实现:
C++
// C++ program to find nth term
// divisible by a, b or c
#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 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)/gcd(a, b))* c) / gcd(((a*b)/gcd(a, b)), c))));
}
// Function for binary search to find the
// nth term divisible by a, b or c
int findNthTerm(int a, int b, int c, long n)
{
// Set low to 1 and high to LONG_MAX
long low = 1, high = LONG_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()
{
long a = 2, b = 3, c = 5, n = 100;
cout << findNthTerm(a, b, c, n);
return 0;
}
Java
// Java program to find nth term
// divisible by a, b or c
class GFG
{
// Function to return
// 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))));
}
// Function for binary search to find the
// nth term divisible by a, b or c
static long findNthTerm(int a, int b, int c, long n)
{
// Set low to 1 and high to LONG_MAX
long low = 1, high = Long.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 = 100;
System.out.println(findNthTerm(a, b, c, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find nth term
# divisible by a, b or c
import sys
# 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 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))));
# Function for binary search to find the
# nth term divisible by a, b or c
def findNthTerm(a, b, c, n):
# Set low to 1 and high to LONG_MAX
low = 1; high = sys.maxsize; 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 int(low);
# Driver code
a = 2; b = 3; c = 5; n = 100;
print(findNthTerm(a, b, c, n));
# This code is contributed by 29AjayKumar
C#
// C# program to find nth term
// divisible by a, b or c
using System;
class GFG
{
// Function to return
// 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))));
}
// Function for binary search to find the
// nth term divisible by a, b or c
static long findNthTerm(int a, int b,
int c, long n)
{
// Set low to 1 and high to LONG_MAX
long low = 1, high = long.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 = 100;
Console.WriteLine(findNthTerm(a, b, c, n));
}
}
// This code is contributed by PrinciRaj1992
输出:
136