📌  相关文章
📜  每 K 个集合的第一个元素具有恰好 K 个小于 N 的质因子的连续元素

📅  最后修改于: 2021-09-06 06:26:59             🧑  作者: Mango

给定两个整数NK ,任务是为每组K个连续元素找到第一个元素,这些元素恰好具有K 个质因子并且小于N

例子:

朴素的方法:朴素的方法是从2 到 N迭代并检查每个连续的K个数字形成一个集合,该集合中的每个元素都有K 个素因子。如果“是”,则打印该集合的第一个元素并检查接下来的 K 个元素。
时间复杂度: O(N*K)
辅助空间: O(1)
有效的方法:可以通过预先计算直到N的质因子的数量并检查每 K 个连续元素计数具有K 个质因子来优化上述方法。以下是步骤:

  1. 使用 Eratosthenes 筛法创建一个最小的质因子数组spf[] ,它存储每个数字的最小质因子,直到N。
  2. 使用上述步骤,计算质因数的数量,直到每个数字N
  3. 将数字存储在素数为K的数组中(比如result[] )。
  4. 对于阵列结果的每个元素[]检查是否存在连续的K数,然后打印K个连续编号的第一个元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#define x 2000021
using namespace std;
 
// For storing smallest prime factor
long long int v[x];
 
// Function construct smallest
// prime factor array
void sieve()
{
    v[1] = 1;
 
    // Mark smallest prime factor for
    // every number to be itself.
    for (long long int i = 2; i < x; i++)
        v[i] = i;
 
    // separately mark spf for every even
    // number as 2
    for (long long int i = 4; i < x; i += 2)
        v[i] = 2;
 
    for (long long int i = 3; i * i < x; i++) {
 
        // Check if i is prime
        if (v[i] == i) {
 
            // Mark SPF for all numbers
            // divisible by i
            for (long long int j = i * i;
                 j < x; j += i) {
 
                // Mark spf[j] if it is
                // not previously marked
                if (v[j] == j) {
                    v[j] = i;
                }
            }
        }
    }
}
 
// Function for counts total number
// of prime factors
long long int prime_factors(long long n)
{
    set s;
 
    while (n != 1) {
        s.insert(v[n]);
        n = n / v[n];
    }
    return s.size();
}
 
// Function to print elements of sets
// of K consecutive elements having
// K prime factors
void distinctPrimes(long long int m,
                    long long int k)
{
 
    // To store the result
    vector result;
    for (long long int i = 14;
         i < m + k; i++) {
 
        // Count number of prime
        // factors of number
        long long count
            = prime_factors(i);
 
        // If number has exactly K
        // factors push in result[]
        if (count == k) {
            result.push_back(i);
        }
    }
 
    long long int p = result.size();
 
    for (long long int index = 0;
         index < p - 1; index++) {
 
        long long element = result[index];
        long long count = 1, z = index;
 
        // Iterate till we get K consecutive
        // elements in result[]
        while (z < p - 1 && count <= k
               && result[z] + 1
                      == result[z + 1]) {
 
            // Count sequence until K
            count++;
            z++;
        }
 
        // Print the element if count >= K
        if (count >= k)
            cout << element << ' ';
    }
}
 
// Driver Code
int main()
{
    // To construct spf[]
    sieve();
 
    // Given N and K
    long long int N = 1000, K = 3;
 
    // Function Call
    distinctPrimes(N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
static final int x = 2000021;
     
// For storing smallest prime factor
static int []v = new int[x];
 
// Function consmallest
// prime factor array
static void sieve()
{
    v[1] = 1;
 
    // Mark smallest prime factor for
    // every number to be itself.
    for(int i = 2; i < x; i++)
        v[i] = i;
 
    // Separately mark spf for every even
    // number as 2
    for(int i = 4; i < x; i += 2)
        v[i] = 2;
 
    for(int i = 3; i * i < x; i++)
    {
         
        // Check if i is prime
        if (v[i] == i)
        {
 
            // Mark SPF for all numbers
            // divisible by i
            for(int j = i * i; j < x; j += i)
            {
                 
                // Mark spf[j] if it is
                // not previously marked
                if (v[j] == j)
                {
                    v[j] = i;
                }
            }
        }
    }
}
 
// Function for counts total number
// of prime factors
static int prime_factors(int n)
{
    HashSet s = new HashSet();
 
    while (n != 1)
    {
        s.add(v[n]);
        n = n / v[n];
    }
     
    return s.size();
}
 
// Function to print elements of sets
// of K consecutive elements having
// K prime factors
static void distinctPrimes(int m, int k)
{
     
    // To store the result
    Vector result = new Vector();
    for (int i = 14; i < m + k; i++)
    {
         
        // Count number of prime
        // factors of number
        long count = prime_factors(i);
 
        // If number has exactly K
        // factors push in result[]
        if (count == k)
        {
            result.add(i);
        }
    }
 
    int p = result.size();
    for(int index = 0;
            index < p - 1; index++)
    {
         
        long element = result.get(index);
        int count = 1, z = index;
 
        // Iterate till we get K consecutive
        // elements in result[]
        while (z < p - 1 && count <= k &&
                     result.get(z) + 1 ==
                     result.get(z + 1))
        {
             
            // Count sequence until K
            count++;
            z++;
        }
         
        // Print the element if count >= K
        if (count >= k)
            System.out.print(element + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // To construct spf[]
    sieve();
 
    // Given N and K
    int N = 1000, K = 3;
 
    // Function call
    distinctPrimes(N, K);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
x = 2000021
 
# For storing smallest prime factor
v = [0] * x
 
# Function construct smallest
# prime factor array
def sieve():
 
    v[1] = 1
 
    # Mark smallest prime factor for
    # every number to be itself
    for i in range(2, x):
        v[i] = i
 
    # separately mark spf for every
    # even number as 2
    for i in range(4, x, 2):
        v[i] = 2
 
    i = 3
    while (i * i < x):
 
        # Check if i is prime
        if (v[i] == i):
 
            # Mark SPF for all numbers
            # divisible by i
            for j in range(i * i, x, i):
 
                # Mark spf[i] if it is
                # not previously marked
                if (v[j] == j):
                    v[j] = i
 
        i += 1
 
# Function for counts total number
# of prime factors
def prime_factors(n):
 
    s = set()
 
    while (n != 1):
        s.add(v[n])
        n = n // v[n]
 
    return len(s)
 
# Function to print elements of sets
# of K consecutive elements having
# K prime factors
def distinctPrimes(m, k):
 
    # To store the result
    result = []
 
    for i in range(14, m + k):
 
        # Count number of prime
        # factors of number
        count = prime_factors(i)
 
        # If number has exactly K
        # factors puch in result[]
        if (count == k):
            result.append(i)
 
    p = len(result)
 
    for index in range(p - 1):
        element = result[index]
        count = 1
        z = index
 
        # Iterate till we get K consecutive
        # elements in result[]
        while (z < p - 1 and count <= k and
               result[z] + 1 == result[z + 1]):
 
            # Count sequence until K
            count += 1
            z += 1
 
        # Print the element if count >= K
        if (count >= k):
            print(element, end = ' ')
 
# Driver Code
if __name__ == '__main__':
     
    # To construct spf[]
    sieve()
 
    # Given N and K
    N = 1000
    K = 3
 
    # Function call
    distinctPrimes(N, K)
 
# This code is contributed by himanshu77


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
static readonly int x = 2000021;
     
// For storing smallest prime factor
static int []v = new int[x];
 
// Function consmallest
// prime factor array
static void sieve()
{
    v[1] = 1;
 
    // Mark smallest prime factor for
    // every number to be itself.
    for(int i = 2; i < x; i++)
        v[i] = i;
 
    // Separately mark spf for every even
    // number as 2
    for(int i = 4; i < x; i += 2)
        v[i] = 2;
 
    for(int i = 3; i * i < x; i++)
    {
         
        // Check if i is prime
        if (v[i] == i)
        {
 
            // Mark SPF for all numbers
            // divisible by i
            for(int j = i * i; j < x; j += i)
            {
                 
                // Mark spf[j] if it is
                // not previously marked
                if (v[j] == j)
                {
                    v[j] = i;
                }
            }
        }
    }
}
 
// Function for counts total number
// of prime factors
static int prime_factors(int n)
{
    HashSet s = new HashSet();
 
    while (n != 1)
    {
        s.Add(v[n]);
        n = n / v[n];
    }
    return s.Count;
}
 
// Function to print elements of sets
// of K consecutive elements having
// K prime factors
static void distinctPrimes(int m, int k)
{
     
    // To store the result
    List result = new List();
    for (int i = 14; i < m + k; i++)
    {
         
        // Count number of prime
        // factors of number
        long count = prime_factors(i);
 
        // If number has exactly K
        // factors push in result[]
        if (count == k)
        {
            result.Add(i);
        }
    }
 
    int p = result.Count;
    for(int index = 0;
            index < p - 1; index++)
    {
        long element = result[index];
        int count = 1, z = index;
 
        // Iterate till we get K consecutive
        // elements in result[]
        while (z < p - 1 && count <= k &&
               result[z] + 1 == result[z + 1])
        {
             
            // Count sequence until K
            count++;
            z++;
        }
         
        // Print the element if count >= K
        if (count >= k)
            Console.Write(element + " ");
    }
}
 
// Driver code
public static void Main(String[] args)
{
     
    // To construct spf[]
    sieve();
 
    // Given N and K
    int N = 1000, K = 3;
 
    // Function call
    distinctPrimes(N, K);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
644 740 804 986

时间复杂度: O(N*log(log N))
辅助空间: O(N)