给定一个整数N ,任务是计算生成数组的方法的数目, arr []由N个整数组成,这样对于每个索引i (基于1的索引), arr [i]要么是一个因子,要么是i的倍数,或两者皆是。 arr []必须是[1,N]范围内所有数字的排列。
例子:
Input: N=2
Output: 2
Explanation:
Two possible arrangements are {1, 2} and {2, 1}
Input: N=3
Output: 3
Explanation:
The 6 possible arrangements are {1, 2, 3}, {2, 1, 3}, {3, 2, 1}, {3, 1, 2}, {2, 3, 1} and {1, 3, 2}.
Among them, the valid arrangements are {1, 2, 3}, {2, 1, 3} and {3, 2, 1}.
方法:可以使用Backtracking技术解决问题,并使用递归打印所有排列的概念。请按照以下步骤查找重复关系:
- 遍历范围[1,N] 。
- 对于当前索引pos ,如果i%pos == 0且i%pos == 0 ,则将i插入到排列中,并使用“回溯”概念查找有效的排列。
- 删除我。
- 对[1,N]范围内的所有值重复上述步骤,最后打印有效排列的计数。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to find the count of
// desired permutations
int findPermutation(unordered_set& arr,
int N)
{
int pos = arr.size() + 1;
// Base case
if (pos > N)
return 1;
int res = 0;
for (int i = 1; i <= N; i++) {
// If i has not been inserted
if (arr.find(i) == arr.end()) {
// Backtrack
if (i % pos == 0 or pos % i == 0) {
// Insert i
arr.insert(i);
// Recur to find valid permutations
res += findPermutation(arr, N);
// Remove i
arr.erase(arr.find(i));
}
}
}
// Return the final count
return res;
}
// Driver Code
int main()
{
int N = 5;
unordered_set arr;
cout << findPermutation(arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the count of
// desired permutations
static int findPermutation(Setarr,
int N)
{
int pos = arr.size() + 1;
// Base case
if (pos > N)
return 1;
int res = 0;
for(int i = 1; i <= N; i++)
{
// If i has not been inserted
if (! arr.contains(i))
{
// Backtrack
if (i % pos == 0 || pos % i == 0)
{
// Insert i
arr.add(i);
// Recur to find valid permutations
res += findPermutation(arr, N);
// Remove i
arr.remove(i);
}
}
}
// Return the final count
return res;
}
// Driver Code
public static void main(String []args)
{
int N = 5;
Set arr = new HashSet();
System.out.print(findPermutation(arr, N));
}
}
// This code is contributed by chitranayal
Python3
# Python3 program to implement
# the above approach
# Function to find the count of
# desired permutations
def findPermutation(arr, N):
pos = len(arr) + 1
# Base case
if(pos > N):
return 1
res = 0
for i in range(1, N + 1):
# If i has not been inserted
if(i not in arr):
# Backtrack
if(i % pos == 0 or pos % i == 0):
# Insert i
arr.add(i)
# Recur to find valid permutations
res += findPermutation(arr, N)
# Remove i
arr.remove(i)
# Return the final count
return res
# Driver Code
N = 5
arr = set()
# Function call
print(findPermutation(arr, N))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the count of
// desired permutations
static int findPermutation(HashSetarr,
int N)
{
int pos = arr.Count + 1;
// Base case
if (pos > N)
return 1;
int res = 0;
for(int i = 1; i <= N; i++)
{
// If i has not been inserted
if (! arr.Contains(i))
{
// Backtrack
if (i % pos == 0 || pos % i == 0)
{
// Insert i
arr.Add(i);
// Recur to find valid permutations
res += findPermutation(arr, N);
// Remove i
arr.Remove(i);
}
}
}
// Return the readonly count
return res;
}
// Driver Code
public static void Main(String []args)
{
int N = 5;
HashSet arr = new HashSet();
Console.Write(findPermutation(arr, N));
}
}
// This code is contributed by gauravrajput1
输出:
10
时间复杂度: O(N×N!)
辅助空间: O(N)