给定大小为N的整数和整数K的数组arr ,任务是查找所有素数且在K可整除的位置上的所有数字的XOR
例子:
Input: arr[] = {2, 3, 5, 7, 11, 8}, K = 2
Output: 4
Explanation:
Positions which are divisible by K are 2, 4, 6 and the elements are 3, 7, 8.
3 and 7 are primes among these.
Therefore XOR = 3 ^ 7 = 4
Input: arr[] = {1, 2, 3, 4, 5}, K = 3
Output: 3
天真的方法:遍历数组,对于每个可被k整除的位置i ,检查其是否为素数。如果是质数,则计算该数字与先前答案的XOR。
高效方法:一种有效方法是使用Eratosthenes筛。筛子阵列可用于检查O(1)时间内是否为质数。
下面是上述方法的实现:
C++
// C++ program to find XOR of
// all Prime numbers in an Array
// at positions divisible by K
#include
using namespace std;
#define MAX 1000005
void SieveOfEratosthenes(vector& prime)
{
// 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
for (int p = 2; p * p < MAX; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2;
i < MAX; i += p)
prime[i] = false;
}
}
}
// Function to find the required XOR
void prime_xor(int arr[], int n, int k)
{
vector prime(MAX, true);
SieveOfEratosthenes(prime);
// To store XOR of the primes
long long int ans = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// If the number is a prime
if (prime[arr[i]]) {
// If index is divisible by k
if ((i + 1) % k == 0) {
ans ^= arr[i];
}
}
}
// Print the xor
cout << ans << endl;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 5, 7, 11, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
int K = 2;
// Function call
prime_xor(arr, n, K);
return 0;
}
Java
// Java program to find XOR of
// all Prime numbers in an Array
// at positions divisible by K
class GFG {
static int MAX = 1000005;
static boolean prime[] = new boolean[MAX];
static void SieveOfEratosthenes(boolean []prime)
{
// 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
for (int p = 2; p * p < MAX; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2;i < MAX; i += p)
prime[i] = false;
}
}
}
// Function to find the required XOR
static void prime_xor(int arr[], int n, int k)
{
for(int i = 0; i < MAX; i++)
prime[i] = true;
SieveOfEratosthenes(prime);
// To store XOR of the primes
int ans = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// If the number is a prime
if (prime[arr[i]]) {
// If index is divisible by k
if ((i + 1) % k == 0) {
ans ^= arr[i];
}
}
}
// Print the xor
System.out.println(ans);
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 2, 3, 5, 7, 11, 8 };
int n = arr.length;
int K = 2;
// Function call
prime_xor(arr, n, K);
}
}
// This code is contributed by Yash_R
Python3
# Python3 program to find XOR of
# all Prime numbers in an Array
# at positions divisible by K
MAX = 1000005
def SieveOfEratosthenes(prime) :
# 0 and 1 are not prime numbers
prime[1] = False;
prime[0] = False;
for p in range(2, int(MAX ** (1/2))) :
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True) :
# Update all multiples of p
for i in range(p * 2, MAX, p) :
prime[i] = False;
# Function to find the required XOR
def prime_xor(arr, n, k) :
prime = [True]*MAX ;
SieveOfEratosthenes(prime);
# To store XOR of the primes
ans = 0;
# Traverse the array
for i in range(n) :
# If the number is a prime
if (prime[arr[i]]) :
# If index is divisible by k
if ((i + 1) % k == 0) :
ans ^= arr[i];
# Print the xor
print(ans);
# Driver code
if __name__ == "__main__" :
arr = [ 2, 3, 5, 7, 11, 8 ];
n = len(arr);
K = 2;
# Function call
prime_xor(arr, n, K);
# This code is contributed by Yash_R
C#
// C# program to find XOR of
// all Prime numbers in an Array
// at positions divisible by K
using System;
class GFG {
static int MAX = 1000005;
static bool []prime = new bool[MAX];
static void SieveOfEratosthenes(bool []prime)
{
// 0 and 1 are not prime numbers
prime[1] = false;
prime[0] = false;
for (int p = 2; p * p < MAX; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2;i < MAX; i += p)
prime[i] = false;
}
}
}
// Function to find the required XOR
static void prime_xor(int []arr, int n, int k)
{
for(int i = 0; i < MAX; i++)
prime[i] = true;
SieveOfEratosthenes(prime);
// To store XOR of the primes
int ans = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// If the number is a prime
if (prime[arr[i]]) {
// If index is divisible by k
if ((i + 1) % k == 0) {
ans ^= arr[i];
}
}
}
// Print the xor
Console.WriteLine(ans);
}
// Driver code
public static void Main (string[] args)
{
int []arr = { 2, 3, 5, 7, 11, 8 };
int n = arr.Length;
int K = 2;
// Function call
prime_xor(arr, n, K);
}
}
// This code is contributed by Yash_R
输出:
4