使用等价关系中的给定操作,使用范围 [2, N] 中的整数可能的集合计数
给定一个整数N ,从2到N范围内重复选择两个不同的整数,如果发现它们的 GCD 大于 1,则将它们尽可能长地插入同一个集合中。在等价关系中形成的集合。因此,如果整数a和b在同一个集合中,并且整数b和c在同一个集合中,那么整数a和c也被称为在同一个群中。任务是找到可以形成的此类集合的总数。
例子:
Input: N = 3
Output: 2
Explanation: Sets formed are: {2}, {3}. They cannot be put in the same set because there GCD is 1.
Input: N = 9
Output : 3
Sets formed are : {2, 3, 4, 6, 8, 9}, {5}, {7}
As {2, 4, 6, 8} lies in same set and {3, 6, 9} also lies in same set. Hence, all these lie in one set together, because 6 is the common element in both the sets.
方法:解决问题的想法是基于以下观察,即所有小于或等于N/2的数都属于同一个集合,因为如果在其中乘以 2,它们将是偶数并且 GCD 大于1与2 。所以剩下的集合是由大于 N/2 的数组成并且是素数,因为如果它们不是素数,那么有一个小于或等于 N/2 的数是该数的除数。使用埃拉托色尼筛法可以找到从 2 到 N 的素数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
bool prime[100001];
// Sieve of Eratosthenes to find
// primes less than or equal to N
void SieveOfEratosthenes(int n)
{
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
// Function to find number of Sets
void NumberofSets(int N)
{
SieveOfEratosthenes(N);
// Handle Base Case
if (N == 2) {
cout << 1 << endl;
}
else if (N == 3) {
cout << 2 << endl;
}
else {
// Set which contains less
// than or equal to N/2
int ans = 1;
// Number greater than N/2 and
// are prime increment it by 1
for (int i = N / 2 + 1; i <= N; i++) {
// If the number is prime
// Increment answer by 1
if (prime[i]) {
ans += 1;
}
}
cout << ans << endl;
}
}
// Driver Code
int main()
{
// Input
int N = 9;
// Function Call
NumberofSets(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static boolean prime[] = new boolean[100001];
// Sieve of Eratosthenes to find
// primes less than or equal to N
static void SieveOfEratosthenes(int n)
{
Arrays.fill(prime, true);
for(int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for(int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
// Function to find number of Sets
static void NumberofSets(int N)
{
SieveOfEratosthenes(N);
// Handle Base Case
if (N == 2)
{
System.out.print(1);
}
else if (N == 3)
{
System.out.print(2);
}
else
{
// Set which contains less
// than or equal to N/2
int ans = 1;
// Number greater than N/2 and
// are prime increment it by 1
for(int i = N / 2 + 1; i <= N; i++)
{
// If the number is prime
// Increment answer by 1
if (prime[i])
{
ans += 1;
}
}
System.out.print(ans);
}
}
// Driver Code
public static void main(String[] args)
{
// Input
int N = 9;
// Function Call
NumberofSets(N);
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
prime = [True] * 100001
# Sieve of Eratosthenes to find
# primes less than or equal to N
def SieveOfEratosthenes(n):
global prime
for p in range(2, n + 1):
if p * p > n:
break
if (prime[p] == True):
for i in range(p * p, n + 1, p):
prime[i] = False
# Function to find number of Sets
def NumberofSets(N):
SieveOfEratosthenes(N)
# Handle Base Case
if (N == 2):
print(1)
elif (N == 3):
print(2)
else:
# Set which contains less
# than or equal to N/2
ans = 1
# Number greater than N/2 and
# are prime increment it by 1
for i in range(N // 2, N + 1):
# If the number is prime
# Increment answer by 1
if (prime[i]):
ans += 1
print(ans)
# Driver Code
if __name__ == '__main__':
# Input
N = 9
# Function Call
NumberofSets(N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
static bool []prime = new bool[100001];
// Sieve of Eratosthenes to find
// primes less than or equal to N
static void SieveOfEratosthenes(int n)
{
for(int i=0;i<100001;i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
// Function to find number of Sets
static void NumberofSets(int N)
{
SieveOfEratosthenes(N);
// Handle Base Case
if (N == 2) {
Console.Write(1);
}
else if (N == 3) {
Console.Write(2);
}
else {
// Set which contains less
// than or equal to N/2
int ans = 1;
// Number greater than N/2 and
// are prime increment it by 1
for (int i = N / 2 + 1; i <= N; i++) {
// If the number is prime
// Increment answer by 1
if (prime[i]) {
ans += 1;
}
}
Console.Write(ans);
}
}
// Driver Code
public static void Main()
{
// Input
int N = 9;
// Function Call
NumberofSets(N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
3
时间复杂度: O(N)
辅助空间: O(K)