📜  Euler 的 Totient 值比自身小 1 的元素计数

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

给定一个由N 个整数组成的数组arr[]和一个范围LR ,任务是找到数组中从索引L 到 R满足以下条件的元素总数:

例子:

朴素的方法:解决这个问题的朴素的方法是迭代数组的所有元素,并检查当前元素的欧拉Totient值是否比它本身小1。如果是,则增加计数。
时间复杂度: O(N * sqrt(N))
辅助空间: O(1)
有效的方法:

  1. 如果我们观察,我们可以注意到上面给定的条件只有质数才能满足。
  2. 所以,我们需要做的就是计算给定范围内素数的总数。
  3. 我们将使用埃拉托色尼筛法来有效地计算素数。
  4. 此外,我们将预先计算计数数组中素数的个数。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
long long prime[1000001] = { 0 };
 
// Seiev of Erotosthenes method to
// compute all primes
void seiveOfEratosthenes()
{
 
    for (int i = 2; i < 1000001;
         i++) {
        prime[i] = 1;
    }
 
    for (int i = 2; i * i < 1000001;
         i++) {
 
        // If current number is
        // marked prime then mark
        // its multiple as non-prime
        if (prime[i] == 1) {
            for (int j = i * i;
                 j < 1000001; j += i) {
                prime[j] = 0;
            }
        }
    }
}
 
// Function to count the number
// of element satisfying the condition
void CountElements(int arr[],
                   int n, int L,
                   int R)
{
    seiveOfEratosthenes();
 
    long long countPrime[n + 1]
        = { 0 };
 
    // Compute the number of primes
    // in count prime array
    for (int i = 1; i <= n; i++) {
        countPrime[i] = countPrime[i - 1]
                        + prime[arr[i - 1]];
    }
 
    // Print the number of elements
    // satisfying the condition
    cout << countPrime[R]
                - countPrime[L - 1]
         << endl;
 
    return;
}
 
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 2, 4, 5, 8 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(int);
    int L = 1, R = 3;
 
    // Function Call
    CountElements(arr, N, L, R);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
static int prime[] = new int[1000001];
 
// Seiev of Erotosthenes method to
// compute all primes
static void seiveOfEratosthenes()
{
    for(int i = 2; i < 1000001; i++)
    {
       prime[i] = 1;
    }
 
    for(int i = 2; i * i < 1000001; i++)
    {
        
       // If current number is
       // marked prime then mark
       // its multiple as non-prime
       if (prime[i] == 1)
       {
           for(int j = i * i;
                   j < 1000001; j += i)
           {
              prime[j] = 0;
           }
       }
    }
}
 
// Function to count the number
// of element satisfying the condition
static void CountElements(int arr[], int n,
                          int L, int R)
{
    seiveOfEratosthenes();
 
    int countPrime[] = new int[n + 1];
 
    // Compute the number of primes
    // in count prime array
    for(int i = 1; i <= n; i++)
    {
       countPrime[i] = countPrime[i - 1] +
                        prime[arr[i - 1]];
    }
 
    // Print the number of elements
    // satisfying the condition
    System.out.print(countPrime[R] -
                     countPrime[L - 1] + "\n");
 
    return;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    int arr[] = { 2, 4, 5, 8 };
 
    // Size of the array
    int N = arr.length;
    int L = 1, R = 3;
 
    // Function Call
    CountElements(arr, N, L, R);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
prime = [0] * (1000001)
 
# Seiev of Erotosthenes method to
# compute all primes
def seiveOfEratosthenes():
 
    for i in range(2, 1000001):
        prime[i] = 1
     
    i = 2
    while(i * i < 1000001):
 
        # If current number is
        # marked prime then mark
        # its multiple as non-prime
        if (prime[i] == 1):
            for j in range(i * i, 1000001, i):
                prime[j] = 0
         
        i += 1
 
# Function to count the number
# of element satisfying the condition
def CountElements(arr, n, L, R):
     
    seiveOfEratosthenes()
 
    countPrime = [0] * (n + 1)
 
    # Compute the number of primes
    # in count prime array
    for i in range(1, n + 1):
        countPrime[i] = (countPrime[i - 1] +
                          prime[arr[i - 1]])
     
    # Print the number of elements
    # satisfying the condition
    print(countPrime[R] -
          countPrime[L - 1])
 
    return
 
# Driver Code
 
# Given array
arr = [ 2, 4, 5, 8 ]
 
# Size of the array
N = len(arr)
L = 1
R = 3
 
# Function call
CountElements(arr, N, L, R)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
class GFG{
 
static int []prime = new int[1000001];
 
// Seiev of Erotosthenes method to
// compute all primes
static void seiveOfEratosthenes()
{
    for(int i = 2; i < 1000001; i++)
    {
        prime[i] = 1;
    }
 
    for(int i = 2; i * i < 1000001; i++)
    {
         
        // If current number is
        // marked prime then mark
        // its multiple as non-prime
        if (prime[i] == 1)
        {
            for(int j = i * i;
                    j < 1000001; j += i)
            {
                prime[j] = 0;
            }
        }
    }
}
 
// Function to count the number
// of element satisfying the condition
static void CountElements(int []arr, int n,
                          int L, int R)
{
    seiveOfEratosthenes();
 
    int []countPrime = new int[n + 1];
 
    // Compute the number of primes
    // in count prime array
    for(int i = 1; i <= n; i++)
    {
        countPrime[i] = countPrime[i - 1] +
                         prime[arr[i - 1]];
    }
 
    // Print the number of elements
    // satisfying the condition
    Console.Write(countPrime[R] -
                  countPrime[L - 1] + "\n");
 
    return;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array
    int []arr = { 2, 4, 5, 8 };
 
    // Size of the array
    int N = arr.Length;
    int L = 1, R = 3;
 
    // Function Call
    CountElements(arr, N, L, R);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live