给定一个整数N ,任务是找到满足以下属性的所有小于N的整数的计数:
- 该数字与N不互质,即它们的GCD 大于1。
- 该数字不是N的除数。
例子:
Input: N = 10
Output: 3
Explanation:
All possible integers which are less than 10 and are neither divisors nor coprime with 10, are {4, 6, 8}.
Therefore, the required count is 3.
Input: N = 42
Ouput: 23
方法:
请按照以下步骤解决问题:
- 设置计数= N。
- 长达N的所有值计算欧拉函数找到数字的计数范围为[1,N]是互质到N,即其GCD用N数字(最大公约数)为1。
- 由于它们与N是互质的,因此请从计数中减去它们。
- 用Eratosthenesnes筛算出N的除数。从计数中减去它们。
- 打印count的最终值,该值等于:
Total count = N – Euler’s totient(N) – Divisor count(N)
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to return the count
// of integers less than N
// satisfying given conditions
int count(int n)
{
// Stores Euler counts
int phi[n + 1] = { 0 };
// Store Divisor counts
int divs[n + 1] = { 0 };
// Based on Sieve of Eratosthenes
for (int i = 1; i <= n; i++) {
phi[i] += i;
// Update phi values of all
// multiples of i
for (int j = i * 2; j <= n; j += i)
phi[j] -= phi[i];
// Update count of divisors
for (int j = i; j <= n; j += i)
divs[j]++;
}
// Return the final count
return (n - phi[n] - divs[n] + 1);
}
// Driver Code
int main()
{
int N = 42;
cout << count(N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.Arrays;
class GFG{
// Function to return the count
// of integers less than N
// satisfying given conditions
public static int count(int n)
{
// Stores Euler counts
int []phi = new int[n + 1];
Arrays.fill(phi, 0);
// Store Divisor counts
int []divs = new int[n + 1];
Arrays.fill(divs, 0);
// Based on Sieve of Eratosthenes
for(int i = 1; i <= n; i++)
{
phi[i] += i;
// Update phi values of all
// multiples of i
for(int j = i * 2; j <= n; j += i)
phi[j] -= phi[i];
// Update count of divisors
for(int j = i; j <= n; j += i)
divs[j]++;
}
// Return the final count
return (n - phi[n] - divs[n] + 1);
}
// Driver Code
public static void main(String []args)
{
int N = 42;
System.out.println(count(N));
}
}
// This code is contributed by grand_master
Python3
# Python3 program to implement
# the above approach
# Function to return the count
# of integers less than N
# satisfying given conditions
def count(n):
# Stores Euler counts
phi = [0] * (n + 1)
# Store Divisor counts
divs = [0] * (n + 1)
# Based on Sieve of Eratosthenes
for i in range(1, n + 1):
phi[i] += i
# Update phi values of all
# multiples of i
for j in range(i * 2, n + 1, i):
phi[j] -= phi[i];
# Update count of divisors
for j in range(i, n + 1, i):
divs[j] += 1
# Return the final count
return (n - phi[n] - divs[n] + 1);
# Driver code
if __name__ == '__main__':
N = 42
print(count(N))
# This code is contributed by jana_sayantan
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to return the count
// of integers less than N
// satisfying given conditions
public static int count(int n)
{
// Stores Euler counts
int []phi = new int[n + 1];
// Store Divisor counts
int []divs = new int[n + 1];
// Based on Sieve of Eratosthenes
for(int i = 1; i <= n; i++)
{
phi[i] += i;
// Update phi values of all
// multiples of i
for(int j = i * 2; j <= n; j += i)
phi[j] -= phi[i];
// Update count of divisors
for(int j = i; j <= n; j += i)
divs[j]++;
}
// Return the final count
return (n - phi[n] - divs[n] + 1);
}
// Driver Code
public static void Main(String []args)
{
int N = 42;
Console.WriteLine(count(N));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
23
时间复杂度: O(N * log(log(N)))
辅助空间: O(N)