给定一个大小为N的整数数组arr和一个整数K ,任务是找到所有质数且位于可被K整除的位置的数字的异或。
例子:
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 ,检查它是否为素数。如果它是素数,则计算该数字与前一个答案的异或。
有效的方法:一种有效的方法是使用埃拉托色尼筛。筛数组可用于在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
Javascript
输出:
4
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live