前 N 个正整数的排列计数,使得任意两个连续数之和为素数
求前 N 个正整数的排列数,使得任意两个连续数之和为素数,其中所有循环排列都被认为是相同的。
注意:第一个和最后一个元素的总和也必须是素数。
示例:
Input: N = 6
Output: 2
Explanation: The two valid permutations are {1, 4, 3, 2, 5, 6} and {1, 6, 5, 2, 3, 4}. The permutation like {3, 2, 5, 6, 1, 4} is considered a cyclic permutation of the 1st one and hence not included.
Input: N = 3
Output: 0
Explanation: No valid permutations exist.
方法:给定的问题可以通过使用递归和回溯来解决。可以观察到,要找到不同的循环数,而不失一般性,循环应该从1开始。可以使用 Eratosthenes 筛创建一个isPrime[]数组,该数组存储一个数字是否为素数。因此,创建一个递归函数并在排列中添加元素,使其与最后一个元素的和为素数。如果第一个和最后一个元素的总和也是素数,则增加排列计数。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
#define ll long long
// Initialize a global variable N
const int maxn = 100;
// Stores the final count of permutations
ll ans = 0;
// Stores whether the integer is prime
bool isPrime[maxn];
bool marked[maxn];
void SieveOfEratosthenes(int n)
{
memset(isPrime, true, sizeof(isPrime));
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed,
// then it is a prime
if (isPrime[p] == true) {
// Update all multiples of P
for (int i = p * p; i <= n; i += p)
isPrime[i] = false;
}
}
}
// Function to find the number of valid permutations
void countCycles(int m, int n, int prev, int par)
{
// If a complete permutation is formed
if (!m) {
if (isPrime[prev + 1]) {
// If the sum of 1st and last element
// of the current permutation is prime
ans++;
}
return;
}
// Iterate from par to N
for (int i = 1 + par; i <= n; i++) {
if (!marked[i] && isPrime[i + prev]) {
// Visit the current number
marked[i] = true;
// Recursive Call
countCycles(m - 1, n, i, 1 - par);
// Backtrack
marked[i] = false;
}
}
}
int countPermutations(int N)
{
// Finding all prime numbers upto 2 * N
SieveOfEratosthenes(2 * N);
// Initializing all values in marked as 0
memset(marked, false, sizeof(marked));
// Initial condition
marked[1] = true;
countCycles(N - 1, N, 1, 1);
// Return Answer
return ans;
}
// Driver code
int main()
{
int N = 6;
cout << countPermutations(N);
return 0;
}
Java
// Java implementation for the above approach
import java.util.*;
class GFG{
// Initialize a global variable N
static int maxn = 100;
// Stores the final count of permutations
static int ans = 0;
// Stores whether the integer is prime
static boolean []isPrime = new boolean[maxn];
static boolean []marked = new boolean[maxn];
static void SieveOfEratosthenes(int n)
{
for (int i = 0; i
Python3
# python implementation for the above approach
import math
# Initialize a global variable N
maxn = 100
# Stores the final count of permutations
ans = 0
# Stores whether the integer is prime
isPrime = [True for _ in range(maxn)]
marked = [False for _ in range(maxn)]
def SieveOfEratosthenes(n):
global ans
global isPrime
global marked
for p in range(2, int(math.sqrt(n))+1):
# If prime[p] is not changed,
# then it is a prime
if (isPrime[p] == True):
# Update all multiples of P
for i in range(p*p, n+1, p):
isPrime[i] = False
# Function to find the number of valid permutations
def countCycles(m, n, prev, par):
global ans
global isPrime
global marked
# If a complete permutation is formed
if (not m):
if (isPrime[prev + 1]):
# If the sum of 1st and last element
# of the current permutation is prime
ans += 1
return
# Iterate from par to N
for i in range(1+par, n+1):
if (not marked[i] and isPrime[i + prev]):
# Visit the current number
marked[i] = True
# Recursive Call
countCycles(m - 1, n, i, 1 - par)
# Backtrack
marked[i] = False
def countPermutations(N):
global ans
global isPrime
global marked
# Finding all prime numbers upto 2 * N
SieveOfEratosthenes(2 * N)
# Initial condition
marked[1] = True
countCycles(N - 1, N, 1, 1)
# Return Answer
return ans
# Driver code
if __name__ == "__main__":
N = 6
print(countPermutations(N))
# This code is contributed by rakeshsahni
C#
// C# implementation for the above approach
using System;
public class GFG
{
// Initialize a global variable N
static int maxn = 100;
// Stores the final count of permutations
static int ans = 0;
// Stores whether the integer is prime
static bool []isPrime = new bool[maxn];
static bool []marked = new bool[maxn];
static void SieveOfEratosthenes(int n)
{
for (int i = 0; i < isPrime.Length; i += 1) {
isPrime[i] = true;
}
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed,
// then it is a prime
if (isPrime[p] == true) {
// Update all multiples of P
for (int i = p * p; i <= n; i += p)
isPrime[i] = false;
}
}
}
// Function to find the number of valid permutations
static void countCycles(int m, int n, int prev, int par)
{
// If a complete permutation is formed
if (m==0) {
if (isPrime[prev + 1]) {
// If the sum of 1st and last element
// of the current permutation is prime
ans++;
}
return;
}
// Iterate from par to N
for (int i = 1 + par; i <= n; i++) {
if (!marked[i] && isPrime[i + prev]) {
// Visit the current number
marked[i] = true;
// Recursive Call
countCycles(m - 1, n, i, 1 - par);
// Backtrack
marked[i] = false;
}
}
}
static int countPermutations(int N)
{
// Finding all prime numbers upto 2 * N
SieveOfEratosthenes(2 * N);
// Initializing all values in marked as 0
for (int i = 0; i
Javascript
输出
2
时间复杂度: O(N!)
辅助空间: O(1)