📜  给定的N个合并算术级数的第K个项

📅  最后修改于: 2021-06-25 18:57:25             🧑  作者: Mango

给定一个整数KN个整数的数组arr [] ,每个数组都是Aarithemetic Progression的第一项和共同差,任务是找到通过合并N个算术级数而形成的集合S的K元素。

例子:

方法:
请按照以下步骤解决问题:

  • 考虑[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)