每个数组元素的 [1, arr[i]] 范围内的非互质数对的计数
给定一个由N个整数组成的数组arr[] ,数组中每个第 i个元素的任务是从[1, arr[i]]范围内找到非互质数对的数量。
例子:
Input: N = 2, arr[] = {3, 4}
Output: 2 4
Explanation:
- All non-co-prime pairs from the range [1, 3] are (2, 2) and (3, 3).
- All non-co-prime pairs from the range [1, 4] are (2, 2), (2, 4), (3, 3) and (4, 4).
Input: N = 3, arr[] = {5, 10, 20}
Output: 5 23 82
朴素的方法:解决问题的最简单方法是迭代每个第 i个数组元素,然后从范围[1, arr[i]]中生成每个可能的对,并且对于每一对,检查它是否是非 co -prime,即该对的gcd是否大于1 。
请按照以下步骤解决此问题:
- 使用变量i遍历范围[0, N – 1] ,并执行以下步骤:
- 将变量lastEle初始化为arr[i]并计数为0以分别存储当前范围的最后一个值和互质数对的数量。
- 使用变量x和y遍历范围[1, arr[i]]中的每一对,然后执行以下操作:
- 如果GCD(x, y)大于1 ,则递增计数1 。
- 最后,打印计数作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Recursive function to
// return gcd of two numbers
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to count the number of
// non co-prime pairs for each query
void countPairs(int* arr, int N)
{
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Stores the count of
// non co-prime pairs
int count = 0;
// Iterate over the range [1, x]
for (int x = 1; x <= arr[i]; x++) {
// Iterate over the range [x, y]
for (int y = x; y <= arr[i]; y++) {
// If gcd of current pair
// is greater than 1
if (gcd(x, y) > 1)
count++;
}
}
cout << count << " ";
}
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Recursive function to
// return gcd of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to count the number of
// non co-prime pairs for each query
static void countPairs(int[] arr, int N)
{
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Stores the count of
// non co-prime pairs
int count = 0;
// Iterate over the range [1, x]
for(int x = 1; x <= arr[i]; x++)
{
// Iterate over the range [x, y]
for(int y = x; y <= arr[i]; y++)
{
// If gcd of current pair
// is greater than 1
if (gcd(x, y) > 1)
count++;
}
}
System.out.print(count + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int arr[] = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by subhammahato348
Python3
# Python3 program for the above approach
# Recursive program to
# return gcd of two numbers
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
# Function to count the number of
# non co-prime pairs for each query
def countPairs(arr, N):
# Traverse the array arr[]
for i in range(0, N):
# Stores the count of
# non co-prime pairs
count = 0
# Iterate over the range [1,x]
for x in range(1, arr[i] + 1):
# Iterate over the range [x,y]
for y in range(x, arr[i] + 1):
# If gcd of current pair
# is greater than 1
if gcd(x, y) > 1:
count += 1
print(count, end = " ")
# Driver code
if __name__ == '__main__':
# Given Input
arr = [ 5, 10, 20 ]
N = len(arr)
# Function Call
countPairs(arr, N)
# This code is contributed by MuskanKalra1
C#
// C# program for the above approach
using System;
class GFG{
// Recursive function to
// return gcd of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to count the number of
// non co-prime pairs for each query
static void countPairs(int[] arr, int N)
{
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Stores the count of
// non co-prime pairs
int count = 0;
// Iterate over the range [1, x]
for(int x = 1; x <= arr[i]; x++)
{
// Iterate over the range [x, y]
for(int y = x; y <= arr[i]; y++)
{
// If gcd of current pair
// is greater than 1
if (gcd(x, y) > 1)
count++;
}
}
Console.Write(count + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
// Given Input
int []arr = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by shivanisinghss2110
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
const int MAX = 1005;
// Auxiliary function to pre-compute
// the answer for each array
void preCalculate(vector& phi,
vector& ans)
{
phi[0] = 0;
phi[1] = 1;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++)
phi[i] = i;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++) {
// If the number is prime
if (phi[i] == i) {
for (int j = i; j <= MAX; j += i)
// Subtract the number of
// pairs which has i as one
// of their factors
phi[j] -= (phi[j] / i);
}
}
// Iterate over the range [1, MAX]
for (int i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
// Function to count the number of
// non co-prime pairs for each query
void countPairs(int* arr, int N)
{
// The i-th element stores
// the count of element that
// are co-prime with i
vector phi(1e5, 0);
// Stores the resulting array
vector ans(1e5, 0);
// Function Call
preCalculate(phi, ans);
// Traverse the array arr[]
for (int i = 0; i < N; ++i) {
cout << ans[arr[i]] << " ";
}
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
static int MAX = 1005;
// Auxiliary function to pre-compute
// the answer for each array
static void preCalculate(int[] phi,
int[] ans)
{
phi[0] = 0;
phi[1] = 1;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++)
phi[i] = i;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++) {
// If the number is prime
if (phi[i] == i) {
for (int j = i; j <= MAX; j += i)
// Subtract the number of
// pairs which has i as one
// of their factors
phi[j] -= (phi[j] / i);
}
}
// Iterate over the range [1, MAX]
for (int i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
// Function to count the number of
// non co-prime pairs for each query
static void countPairs(int[] arr, int N)
{
// The i-th element stores
// the count of element that
// are co-prime with i
int[] phi = new int[100000];
Arrays.fill(phi, 0);
// Stores the resulting array
int[] ans = new int[100000];
Arrays.fill(ans, 0);
// Function Call
preCalculate(phi, ans);
// Traverse the array arr[]
for (int i = 0; i < N; ++i) {
System.out.print(ans[arr[i]] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int arr[] = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by code_hunt.
Python3
MAX = 1005;
def preCalculate(phi,ans):
phi[0] = 0
phi[1] = 1
# Iterate over the range [1, MAX]
for i in range(2, MAX+1):
phi[i] = i
# Iterate over the range [1, MAX]
for i in range(2, MAX+1):
if (phi[i] == i):
for j in range(i, MAX+1, i):
# Subtract the number of
# pairs which has i as one
# of their factors
phi[j] -= (phi[j] // i);
# Iterate over the range [1, MAX]
for i in range(1, MAX+1):
ans[i] = ans[i - 1] + (i - phi[i]);
# Function to count the number of
# non co-prime pairs for each query
def countPairs(arr, N):
# The i-th element stores
# the count of element that
# are co-prime with i
phi = [0 for i in range(100001)]
# Stores the resulting array
ans = [0 for i in range(100001)]
# Function Call
preCalculate(phi, ans);
# Traverse the array arr[]
for i in range(N):
print(ans[arr[i]],end=' ');
# Given Input
arr= [5, 10, 20]
N = 3;
# Function Call
countPairs(arr, N);
# This code is contributed by rutvik_56.
C#
// C# program for the above approach
using System;
class GFG{
static int MAX = 1005;
// Auxiliary function to pre-compute
// the answer for each array
static void preCalculate(int[] phi,
int[] ans)
{
phi[0] = 0;
phi[1] = 1;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++)
phi[i] = i;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++) {
// If the number is prime
if (phi[i] == i) {
for (int j = i; j <= MAX; j += i)
// Subtract the number of
// pairs which has i as one
// of their factors
phi[j] -= (phi[j] / i);
}
}
// Iterate over the range [1, MAX]
for (int i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
// Function to count the number of
// non co-prime pairs for each query
static void countPairs(int[] arr, int N)
{
// The i-th element stores
// the count of element that
// are co-prime with i
int[] phi = new int[100000];
Array.Clear(phi, 0, 100000);
// Stores the resulting array
int[] ans = new int[100000];
Array.Clear(ans, 0, 100000);
// Function Call
preCalculate(phi, ans);
// Traverse the array arr[]
for (int i = 0; i < N; ++i) {
Console.Write(ans[arr[i]] + " ");
}
}
// Driver Code
public static void Main()
{
// Given Input
int[] arr = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
5 23 82
时间复杂度: O(N*M 2 *log(M)),其中 M 是 大批。
辅助空间: O(1)
高效方法:上述方法可以通过使用欧拉的totient函数和前缀和数组进行优化。请按照以下步骤解决问题:
- 初始化两个数组,比如phi[]和ans[]为0 ,其中第i个 数组的元素表示与i互质的整数的计数和从范围[1, arr[i]] 形成的非互质对的计数。
- 使用变量 i 遍历范围[1, MAX] ,并将i分配给phi[i]。
- 使用变量 i 遍历范围[2, MAX] ,并执行以下步骤:
- 如果phi[i] = i,则使用变量j遍历范围[i, MAX]并执行以下步骤:
- 将phi[j] / i从phi[j]递减,然后将j增加i 。
- 如果phi[i] = i,则使用变量j遍历范围[i, MAX]并执行以下步骤:
- 使用变量 i 遍历范围[1, MAX] ,并执行以下步骤:
- 将ans[i]更新为ans[i – 1] + (i – phi[i])。
- 最后,完成上述步骤后,打印数组ans[] 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int MAX = 1005;
// Auxiliary function to pre-compute
// the answer for each array
void preCalculate(vector& phi,
vector& ans)
{
phi[0] = 0;
phi[1] = 1;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++)
phi[i] = i;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++) {
// If the number is prime
if (phi[i] == i) {
for (int j = i; j <= MAX; j += i)
// Subtract the number of
// pairs which has i as one
// of their factors
phi[j] -= (phi[j] / i);
}
}
// Iterate over the range [1, MAX]
for (int i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
// Function to count the number of
// non co-prime pairs for each query
void countPairs(int* arr, int N)
{
// The i-th element stores
// the count of element that
// are co-prime with i
vector phi(1e5, 0);
// Stores the resulting array
vector ans(1e5, 0);
// Function Call
preCalculate(phi, ans);
// Traverse the array arr[]
for (int i = 0; i < N; ++i) {
cout << ans[arr[i]] << " ";
}
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
static int MAX = 1005;
// Auxiliary function to pre-compute
// the answer for each array
static void preCalculate(int[] phi,
int[] ans)
{
phi[0] = 0;
phi[1] = 1;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++)
phi[i] = i;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++) {
// If the number is prime
if (phi[i] == i) {
for (int j = i; j <= MAX; j += i)
// Subtract the number of
// pairs which has i as one
// of their factors
phi[j] -= (phi[j] / i);
}
}
// Iterate over the range [1, MAX]
for (int i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
// Function to count the number of
// non co-prime pairs for each query
static void countPairs(int[] arr, int N)
{
// The i-th element stores
// the count of element that
// are co-prime with i
int[] phi = new int[100000];
Arrays.fill(phi, 0);
// Stores the resulting array
int[] ans = new int[100000];
Arrays.fill(ans, 0);
// Function Call
preCalculate(phi, ans);
// Traverse the array arr[]
for (int i = 0; i < N; ++i) {
System.out.print(ans[arr[i]] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int arr[] = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by code_hunt.
Python3
MAX = 1005;
def preCalculate(phi,ans):
phi[0] = 0
phi[1] = 1
# Iterate over the range [1, MAX]
for i in range(2, MAX+1):
phi[i] = i
# Iterate over the range [1, MAX]
for i in range(2, MAX+1):
if (phi[i] == i):
for j in range(i, MAX+1, i):
# Subtract the number of
# pairs which has i as one
# of their factors
phi[j] -= (phi[j] // i);
# Iterate over the range [1, MAX]
for i in range(1, MAX+1):
ans[i] = ans[i - 1] + (i - phi[i]);
# Function to count the number of
# non co-prime pairs for each query
def countPairs(arr, N):
# The i-th element stores
# the count of element that
# are co-prime with i
phi = [0 for i in range(100001)]
# Stores the resulting array
ans = [0 for i in range(100001)]
# Function Call
preCalculate(phi, ans);
# Traverse the array arr[]
for i in range(N):
print(ans[arr[i]],end=' ');
# Given Input
arr= [5, 10, 20]
N = 3;
# Function Call
countPairs(arr, N);
# This code is contributed by rutvik_56.
C#
// C# program for the above approach
using System;
class GFG{
static int MAX = 1005;
// Auxiliary function to pre-compute
// the answer for each array
static void preCalculate(int[] phi,
int[] ans)
{
phi[0] = 0;
phi[1] = 1;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++)
phi[i] = i;
// Iterate over the range [1, MAX]
for (int i = 2; i <= MAX; i++) {
// If the number is prime
if (phi[i] == i) {
for (int j = i; j <= MAX; j += i)
// Subtract the number of
// pairs which has i as one
// of their factors
phi[j] -= (phi[j] / i);
}
}
// Iterate over the range [1, MAX]
for (int i = 1; i <= MAX; i++)
ans[i] = ans[i - 1] + (i - phi[i]);
}
// Function to count the number of
// non co-prime pairs for each query
static void countPairs(int[] arr, int N)
{
// The i-th element stores
// the count of element that
// are co-prime with i
int[] phi = new int[100000];
Array.Clear(phi, 0, 100000);
// Stores the resulting array
int[] ans = new int[100000];
Array.Clear(ans, 0, 100000);
// Function Call
preCalculate(phi, ans);
// Traverse the array arr[]
for (int i = 0; i < N; ++i) {
Console.Write(ans[arr[i]] + " ");
}
}
// Driver Code
public static void Main()
{
// Given Input
int[] arr = { 5, 10, 20 };
int N = 3;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
5 23 82
时间复杂度: O(N+ M*log(N)),其中 M 是数组的最大元素。
辅助空间: O(M+N)