给定一个整数K和N个整数的数组arr [] ,每个数组都是Aarithemetic Progression的第一项和共同差,任务是找到通过合并N个算术级数而形成的集合S的第K个元素。
例子:
Input: arr[] = {2, 3}, K = 10
Output: 15
Explanation:
There are 2 arithmetic progressions.
First-term and the common difference of the first A.P is 2. Therefore AP1 = {2, 4, 6, …}
First term and the common difference of the second A.P is 3. Therefore AP2 = {3, 6, 9, …}
Set S contains AP1 and AP2, i.e. { 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20…… }.
Hence 10th term is: 15
Input: arr[] = {2, 3, 5, 7, 11}, K = 8
Output: 9
Explanation:
There are 5 arithmetic progressions.
First-term and the common difference of the first A.P is 2. Therefore AP1 = {2, 4, 6, …}
First term and the common difference of the second A.P is 3. Therefore AP2 = {3, 6, 9, …}
First-term and the common difference of the third A.P is 5. Therefore AP3 = {5, 10, 15, …}
First term and the common difference of the fourth A.P is 7. Therefore AP4 = {7, 14, 21, …}
First-term and the common difference of the fifth A.P is 11. Therefore AP5 = {11, 22, 33, …}
Thus set S contains { 2, 3, 4, 5, 6, 7, 8, 9, 10, …. }.
Hence 8th term is: 9
方法:
请按照以下步骤解决问题:
- 考虑[1,maxm]的范围,并计算该范围的中间值。
- 检查是否可以通过合并N系列获得K个元素。如果项数超过K ,则将R减小到mid 。否则,将L更新为mid 。现在,继续进行下去,直到找到一个中位数,该中位数可以精确获得系列中的K个项。
- 若要确定在任何特定值之前将出现多少个元素,我们需要应用“包含”和“排除”原理来获得所有AP的并集
下面是上述方法的实现:
C++
// C++ program to find k-th term of
// N merged Arithmetic Progressions
#include
using namespace std;
#define maxm 1000000000
// Function to count and return the
// number of values less than equal
// to N present in the set
int count(vector v, int n)
{
int i, odd = 0, even = 0;
int j, d, count;
int t = (int)1 << v.size();
int size = v.size();
for (i = 1; i < t; i++) {
d = 1, count = 0;
for (j = 0; j < size; j++) {
// Check whether j-th bit
// is set bit or not
if (i & (1 << j)) {
d *= v[j];
count++;
}
}
if (count & 1)
odd += n / d;
else
even += n / d;
}
return (odd - even);
}
// Function to implement Binary
// Search to find K-th element
int BinarySearch(int l, int r,
vector v,
int key)
{
int mid;
while (r - l > 1) {
// Find middle index of
// the array
mid = (l + r) / 2;
// Search in the left half
if (key <= count(v, mid)) {
r = mid;
}
// Search in the right half
else {
l = mid;
}
}
// If exactly K elements
// are present
if (key == count(v, l))
return l;
else
return r;
}
// Driver Code
int main()
{
int N = 2, K = 10;
vector v = { 2, 3 };
cout << BinarySearch(1, maxm, v, K)
<< endl;
return 0;
}
Java
// Java program to find k-th term of
// N merged Arithmetic Progressions
import java.util.*;
class GFG{
static final int maxm = 1000000000;
// Function to count and return the
// number of values less than equal
// to N present in the set
static int count(int []v, int n)
{
int i, odd = 0, even = 0;
int j, d, count;
int t = (int)1 << v.length;
int size = v.length;
for (i = 1; i < t; i++)
{
d = 1;
count = 0;
for (j = 0; j < size; j++)
{
// Check whether j-th bit
// is set bit or not
if ((i & (1 << j)) > 0)
{
d *= v[j];
count++;
}
}
if (count % 2 == 1)
odd += n / d;
else
even += n / d;
}
return (odd - even);
}
// Function to implement Binary
// Search to find K-th element
static int BinarySearch(int l, int r,
int []v,
int key)
{
int mid;
while (r - l > 1)
{
// Find middle index of
// the array
mid = (l + r) / 2;
// Search in the left half
if (key <= count(v, mid))
{
r = mid;
}
// Search in the right half
else
{
l = mid;
}
}
// If exactly K elements
// are present
if (key == count(v, l))
return l;
else
return r;
}
// Driver Code
public static void main(String[] args)
{
int N = 2, K = 10;
int []v = { 2, 3 };
System.out.print(BinarySearch(1, maxm, v, K) + "\n");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find k-th term of
# N merged Arithmetic Progressions
maxm = 1000000000
# Function to count and return the
# number of values less than equal
# to N present in the set
def count(v, n):
odd, even = 0, 0
t = 1 << len(v)
size = len(v)
for i in range(1, t):
d, count = 1, 0
for j in range(0, size):
# Check whether j-th bit
# is set bit or not
if (i & (1 << j)):
d *= v[j]
count += 1
if (count & 1):
odd += n // d
else:
even += n // d
return (odd - even)
# Function to implement Binary
# Search to find K-th element
def BinarySearch(l, r, v, key):
while (r - l > 1):
# Find middle index of
# the array
mid = (l + r) // 2
# Search in the left half
if (key <= count(v, mid)):
r = mid
# Search in the right half
else:
l = mid
# If exactly K elements
# are present
if (key == count(v, l)):
return l
else:
return r
# Driver Code
N, K = 2, 10
v = [ 2, 3 ]
print(BinarySearch(1, maxm, v, K))
# This code is contributed by divyeshrabadiya07
C#
// C# program to find k-th term of
// N merged Arithmetic Progressions
using System;
class GFG{
static readonly int maxm = 1000000000;
// Function to count and return the
// number of values less than equal
// to N present in the set
static int count(int []v, int n)
{
int i, odd = 0, even = 0;
int j, d, count;
int t = (int)1 << v.Length;
int size = v.Length;
for(i = 1; i < t; i++)
{
d = 1;
count = 0;
for(j = 0; j < size; j++)
{
// Check whether j-th bit
// is set bit or not
if ((i & (1 << j)) > 0)
{
d *= v[j];
count++;
}
}
if (count % 2 == 1)
odd += n / d;
else
even += n / d;
}
return (odd - even);
}
// Function to implement Binary
// Search to find K-th element
static int BinarySearch(int l, int r,
int []v, int key)
{
int mid;
while (r - l > 1)
{
// Find middle index of
// the array
mid = (l + r) / 2;
// Search in the left half
if (key <= count(v, mid))
{
r = mid;
}
// Search in the right half
else
{
l = mid;
}
}
// If exactly K elements
// are present
if (key == count(v, l))
return l;
else
return r;
}
// Driver Code
public static void Main(String[] args)
{
//int N = 2;
int K = 10;
int []v = { 2, 3 };
Console.Write(BinarySearch(
1, maxm, v, K) + "\n");
}
}
// This code is contributed by gauravrajput1
Javascript
15
时间复杂度: O(N * 2 N )
辅助空间: O(1)