给定大小为N的数组arr [] ,任务是查找给定数组每个元素的不同素数的计数。
例子:
Input: arr[] = {6, 9, 12}
Output: 2 1 2
Explanation:
- 6 = 2 × 3. Therefore, count = 2
- 9 = 3 × 3. Therefore, count = 1
- 12 = 2 × 2 × 3. Therefore, count = 2
The count of distinct prime factors of each array element are 2, 1, 2 respectively.
Input: arr[] = {4, 8, 10, 6}
Output: 1 1 2 2
天真的方法:解决问题的最简单方法是找到每个数组元素的素因。然后,在其中找到不同的质数,并打印每个数组元素的计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:可以通过使用所有数字的最小素因数预先计算所有数字的不同因数来优化上述方法。请按照以下步骤解决问题
- 初始化一个向量,例如v ,以存储不同的素因数。
- 使用Eratosthenes筛网存储最小的质数因子(SPF),最多10 5 。
- 通过将数字递归除以它们的最小质因数直至其减为1并在v [X]中存储X的不同质因数,计算出所有数字的不同质因数。
- 遍历数组arr [] ,对于每个数组元素,将计数打印为v [arr [i]]。size() 。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
#define MAX 100001
// Stores smallest prime
// factor for every number
int spf[MAX];
// Stores distinct prime factors
vector v[MAX];
// Function to find the smallest
// prime factor of every number
void sieve()
{
// Mark the smallest prime factor
// of every number to itself
for (int i = 1; i < MAX; i++)
spf[i] = i;
// Seperately mark all the
// smallest prime factor of
// every even number to be 2
for (int i = 4; i < MAX; i = i + 2)
spf[i] = 2;
for (int i = 3; i * i < MAX; i++)
// If i is prime
if (spf[i] == i) {
// Mark spf for all numbers
// divisible by i
for (int j = i * i; j < MAX;
j = j + i) {
// Mark spf[j] if it is
// not previously marked
if (spf[j] == j)
spf[j] = i;
}
}
}
// Function to find the distinct
// prime factors
void DistPrime()
{
for (int i = 1; i < MAX; i++) {
int idx = 1;
int x = i;
// Push all distinct of x
// prime factor in v[x]
if (x != 1)
v[i].push_back(spf[x]);
x = x / spf[x];
while (x != 1) {
if (v[i][idx - 1]
!= spf[x]) {
// Pushback into v[i]
v[i].push_back(spf[x]);
// Increment the idx
idx += 1;
}
// Update x = (x / spf[x])
x = x / spf[x];
}
}
}
// Function to get the distinct
// factor count of arr[]
void getFactorCount(int arr[],
int N)
{
// Precompute the smallest
// Prime Factors
sieve();
// For distinct prime factors
// Fill the v[] vector
DistPrime();
// Count of Distinct Prime
// Factors of each array element
for (int i = 0; i < N; i++) {
cout << (int)v[arr[i]].size()
<< " ";
}
}
// Driver Code
int main()
{
int arr[] = { 6, 9, 12 };
int N = sizeof(arr) / sizeof(arr[0]);
getFactorCount(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int MAX = 100001;
// Stores smallest prime
// factor for every number
static int spf[];
// Stores distinct prime factors
static ArrayList v[];
// Function to find the smallest
// prime factor of every number
static void sieve()
{
// Mark the smallest prime factor
// of every number to itself
for (int i = 1; i < MAX; i++)
spf[i] = i;
// Seperately mark all the
// smallest prime factor of
// every even number to be 2
for (int i = 4; i < MAX; i = i + 2)
spf[i] = 2;
for (int i = 3; i * i < MAX; i++)
// If i is prime
if (spf[i] == i) {
// Mark spf for all numbers
// divisible by i
for (int j = i * i; j < MAX; j = j + i) {
// Mark spf[j] if it is
// not previously marked
if (spf[j] == j)
spf[j] = i;
}
}
}
// Function to find the distinct
// prime factors
static void DistPrime()
{
for (int i = 1; i < MAX; i++) {
int idx = 1;
int x = i;
// Push all distinct of x
// prime factor in v[x]
if (x != 1)
v[i].add(spf[x]);
x = x / spf[x];
while (x != 1) {
if (v[i].get(idx - 1) != spf[x]) {
// Pushback into v[i]
v[i].add(spf[x]);
// Increment the idx
idx += 1;
}
// Update x = (x / spf[x])
x = x / spf[x];
}
}
}
// Function to get the distinct
// factor count of arr[]
static void getFactorCount(int arr[], int N)
{
// initialization
spf = new int[MAX];
v = new ArrayList[MAX];
for (int i = 0; i < MAX; i++)
v[i] = new ArrayList<>();
// Precompute the smallest
// Prime Factors
sieve();
// For distinct prime factors
// Fill the v[] vector
DistPrime();
// Count of Distinct Prime
// Factors of each array element
for (int i = 0; i < N; i++) {
System.out.print((int)v[arr[i]].size() + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 6, 9, 12 };
int N = arr.length;
getFactorCount(arr, N);
}
}
// This code is contributed by Kingash.
输出:
2 1 2
时间复杂度: O(N * log N)
辅助空间: O(N)