📜  计数最多 N 的 GCD 与 N 小于该数字的数字

📅  最后修改于: 2022-05-13 01:56:08.437000             🧑  作者: Mango

计数最多 N 的 GCD 与 N 小于该数字的数字

给定一个整数N ,任务是计算K的值(其中1 ≤ K≤ N ),使得1< GCD (K, N) < K

例子:

朴素方法:最简单的方法是在范围[1, N]上进行迭代,并检查每个数字是否满足给定条件。最后,打印这些数字的计数。

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

有效方法:想法是找到范围[1, N]中的所有数字,其中gcd(K, N) = 1gcd(K, N) = K然后最后从N中删除所有这些数字以获得最后的答案。请按照以下步骤解决问题:

  • 使用埃拉托色尼筛法存储N的所有主要因子。
  • 存储所有数字的计数 在范围[1, N]中,变量count1中的gcd(N, K) = K。
  • 存储所有数字的计数 gcd(N, K) = 1的范围[1, N]中使用 欧拉总函数 在变量count2中。
  • 通过将ans更新为N – (count1 + count2)N中删除这些数字。
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Store the all prime numbers
// using Sieve of Eratosthenes
const int MAXN = 100001;
vector spf;
 
// Function to fill the prime numbers
// using Sieve of Eratosthenes
void sieve()
{
    // Create a boolean array and
    // initialize all entries it as 0
    int p[MAXN + 1] = { 0 };
 
    p[2] = 1;
    for (long long int i = 3; i < MAXN; i += 2) {
        p[i] = 1;
    }
 
    // Push the first prime number
    spf.push_back(2);
 
    for (long long i = 3; i < MAXN; i += 2) {
 
        // If the current number is prime
        if (p[i]) {
 
            // Store the prime numbers
            // in spf which is prime
            spf.push_back(i);
 
            // Mark all its multiples as false
            for (long long int j = i * i; j < MAXN;
                 j += 2 * i)
                p[j] = 0;
        }
    }
}
 
// Function to count the values of K where
// 1≤K≤N such that 1< gcd(K, N) < K
void countK(int N)
{
    // Precalculate prime numbers
    sieve();
    int N1 = N;
 
    // Store the smallest prime number
    int div = spf[0];
 
    int index = 1, C = 0;
 
    // Store the count of those numbers in the
    // range [1, N] for which gcd(N, K) = K
    int count1 = 1;
 
    // Store the count of those numbers from
    // the range [1, N] such that gcd(N, K) = 1
    float count2 = N;
 
    // Iterate through all prime factors of N
    while (div * div <= N) {
        if (N % div == 0) {
            C = 0;
            while (N % div == 0) {
                N /= div;
                C++;
            }
 
            count1 = count1 * (C + 1);
 
            // count2 is determined by
            // Euler's Totient Function
            count2 *= (1.0 - (1.0 / (float)div));
        }
 
        div = spf[index];
 
        index++;
    }
    if (N != 1) {
 
        count1 *= 2;
        count2 = count2 * (1.0 - (1.0 / (float)N));
    }
 
    int ans = N1 - count1 - count2;
 
    // Add 1 to result as 1 is contributed
    // twice due to count1 and count2
    ans = ans + 1;
 
    // Print the result
    cout << ans;
 
    return;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 10;
 
    // Function Call
    countK(N);
 
    return 0;
}


Python3
# Python3 program for the above approach
 
# Store the all prime numbers
# using Sieve of Eratosthenes
MAXN = 100001
spf = []
 
# Function to fill the prime numbers
# using Sieve of Eratosthenes
def sieve():
     
    global MAXN
     
    # Create a boolean array and
    # initialize all entries it as 0
    p = [0] * (MAXN + 1)
    p[2] = 1
     
    for i in range(3, MAXN, 2):
        p[i] = 1
 
    # Push the first prime number
    spf.append(2)
 
    for i in range(3, MAXN, 2):
         
        # If the current number is prime
        if (p[i]):
 
            # Store the prime numbers
            # in spf which is prime
            spf.append(i)
 
            # Mark all its multiples as false
            for j in range(i * i, MAXN, 2 * i):
                p[j] = 0
 
# Function to count the values of K where
# 1≤K≤N such that 1< gcd(K, N) < K
def countK(N):
     
    # Precalculate prime numbers
    sieve()
    N1 = N
 
    # Store the smallest prime number
    div = spf[0]
 
    index, C = 1, 0
 
    # Store the count of those numbers in the
    # range [1, N] for which gcd(N, K) = K
    count1 = 1
 
    # Store the count of those numbers from
    # the range [1, N] such that gcd(N, K) = 1
    count2 = N
 
    # Iterate through all prime factors of N
    while (div * div <= N):
        if (N % div == 0):
            C = 0
             
            while (N % div == 0):
                N //= div
                C += 1
 
            count1 = count1 * (C + 1)
 
            # count2 is determined by
            # Euler's Totient Function
            count2 *= (1.0 - (1.0 / div))
 
        div = spf[index]
 
        index += 1
 
    if (N != 1):
        count1 *= 2
        count2 = count2 * (1.0 - (1.0 / N))
 
    ans = N1 - count1 - count2
 
    # Add 1 to result as 1 is contributed
    # twice due to count1 and count2
    ans = ans + 1
 
    # Print the result
    print(int(ans))
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    N = 10
 
    # Function Call
    countK(N)
 
# This code is contributed by mohit kumar 29


Javascript


输出:
3

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