计算数组中每个元素的除数或倍数
给定一个包含N个整数的数组A[] ,对于数组中的每个整数A[i] ,任务是找到数组中整数A[j] (j != i) 的个数,使得A[i] % A[j] = 0或A[j] % A[i] = 0 。
例子:
Input: A = {2, 3, 4, 5, 6}
Output: 2 1 1 0 2
Explanation:
For i=0, the valid indices are 2 and 4 as 4%2 = 0 and 6%2 = 0.
For i=1, the only valid index is 4 as 6%3 = 0.
For i=2, the only valid index is 0 as 4%2 = 0.
For i=3, there are no valid indices.
For i=0, the valid indices are 0 and 1 as 6%2 = 0 and 6%3 = 0.
Input: A = {6, 6, 6, 6, 6}
Output: 4 4 4 4 4
方法:给定问题可以通过观察满足给定条件的整数个数可以分为两种情况来解决。假设当前整数是P , Q是满足给定条件的整数。
- 第一种情况,其中Q是P的倍数。因此,给定数组中可被 P 整除的整数的计数是所需的答案。这种情况可以使用这里讨论的埃拉托色尼筛的简单修改来处理。
- 情况 2 ,其中P是Q的倍数。因此,Q 除以 P 的给定数组中的整数计数是所需答案。这种情况可以使用与第一种情况类似的筛子来处理。
因此,任何整数所需的答案是Case 1和Case 2的结果整数之和。在P = Q的情况下,案例 1和案例 2都表示相同的值,并且应该只考虑一次。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
using namespace std;
// Function to find the count of integers
// such that A[i]%A[j] = 0 or A[j]%A[i] = 0
// for each index of the array A[]
void countIndex(int A[], int N)
{
// Stores the maximum integer in A[]
int MAX = *max_element(A, A + N);
// Stores the frequency of each
// element in the array A[]
vector freq(MAX + 1, 0);
for (int i = 0; i < N; i++)
freq[A[i]]++;
// Stores the valid integers in A[]
// for all integers from 1 to MAX
vector res(MAX + 1, 0);
for (int i = 1; i <= MAX; ++i) {
for (int j = i; j <= MAX; j += i) {
// Case where P = Q
if (i == j) {
// Subtract 1 because P & Q
// cannot have same index
res[i] += (freq[j] - 1);
}
else {
// Case 1
res[i] += freq[j];
// Case 2
res[j] += freq[i];
}
}
}
// Loop to print answer for
// each index of array A[]
for (int i = 0; i < N; i++) {
cout << res[A[i]] << " ";
}
}
// Driver Code
int main()
{
int A[] = { 2, 3, 4, 5, 6 };
int N = sizeof(A) / sizeof(int);
// Function Call
countIndex(A, N);
return 0;
}
Java
// Java Program for the above approach
import java.util.*;
class GFG{
// Function to find the count of integers
// such that A[i]%A[j] = 0 or A[j]%A[i] = 0
// for each index of the array []A
static void countIndex(int []A, int N)
{
// Stores the maximum integer in []A
int MAX = Arrays.stream(A).max().getAsInt();
// Stores the frequency of each
// element in the array []A
int []freq = new int[MAX + 1];
for (int i = 0; i < N; i++)
freq[A[i]]++;
// Stores the valid integers in []A
// for all integers from 1 to MAX
int []res = new int[MAX + 1];
for (int i = 1; i <= MAX; ++i) {
for (int j = i; j <= MAX; j += i) {
// Case where P = Q
if (i == j) {
// Subtract 1 because P & Q
// cannot have same index
res[i] += (freq[j] - 1);
}
else {
// Case 1
res[i] += freq[j];
// Case 2
res[j] += freq[i];
}
}
}
// Loop to print answer for
// each index of array []A
for (int i = 0; i < N; i++) {
System.out.print(res[A[i]]+ " ");
}
}
// Driver Code
public static void main(String[] args)
{
int []A = { 2, 3, 4, 5, 6 };
int N = A.length;
// Function Call
countIndex(A, N);
}
}
// This code is contributed by Princi Singh
Python3
# Python 3 Program for the above approach
# Function to find the count of integers
# such that A[i]%A[j] = 0 or A[j]%A[i] = 0
# for each index of the array A[]
def countIndex(A, N):
# Stores the maximum integer in A[]
MAX = max(A)
# Stores the frequency of each
# element in the array A[]
freq = [0 for i in range(MAX+1)]
for i in range(N):
if A[i] in freq:
freq[A[i]] += 1
else:
freq[A[i]] = 1
# Stores the valid integers in A[]
# for all integers from 1 to MAX
res = [0 for i in range(MAX+1)]
for i in range(1, MAX + 1, 1):
for j in range(i, MAX + 1, i):
# Case where P = Q
if (i == j):
# Subtract 1 because P & Q
# cannot have same index
res[i] += (freq[j] - 1)
else:
# Case 1
res[i] += freq[j]
# Case 2
res[j] += freq[i]
# Loop to print answer for
# each index of array A[]
for i in range(N):
print(res[A[i]],end = " ")
# Driver Code
if __name__ == '__main__':
A = [2, 3, 4, 5, 6]
N = len(A)
# Function Call
countIndex(A, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program of above approach
using System;
public class GFG {
static void countIndex(int[] A, int N)
{
// Stores the maximum integer in []A
int MAX = A[0];
for (int i = 1; i < N; i++) {
if (A[i] > MAX) {
MAX = A[i];
}
}
// Stores the frequency of each
// element in the array []A
int[] freq = new int[MAX + 1];
for (int i = 0; i < N; i++)
freq[A[i]]++;
// Stores the valid integers in []A
// for all integers from 1 to MAX
int[] res = new int[MAX + 1];
for (int i = 1; i <= MAX; ++i) {
for (int j = i; j <= MAX; j += i) {
// Case where P = Q
if (i == j) {
// Subtract 1 because P & Q
// cannot have same index
res[i] += (freq[j] - 1);
}
else {
// Case 1
res[i] += freq[j];
// Case 2
res[j] += freq[i];
}
}
}
// Loop to print answer for
// each index of array []A
for (int i = 0; i < N; i++) {
Console.Write(res[A[i]] + " ");
}
}
// Driver Code
static public void Main()
{
int[] A = { 2, 3, 4, 5, 6 };
int N = A.Length;
// Function Call
countIndex(A, N);
}
}
// This code is contributed by maddler.
Javascript
2 1 1 0 2
时间复杂度: O(N*log N)
辅助空间: O(MAX),其中 MAX 表示给定数组中的最大整数。