给定N个不同的对象,如果所有顺时针排列都被认为是相同的,则任务是找到N个对象的不同排列的数量。
If A, B, and C are three distinct objects, then arrangements {A, B, C}, {C, A, B}, and {B, C, A} are considered the same as all the arrangements are clockwise to each other.
例子:
Input: N = 3
Output: 2
Explanation:
Permutation of 3 distinct objects can be represented as these 6 arrangements: {A, B, C}, {A, C, B}, {B, A, C}, {B, C, A}, {C, A, B} and {C, B, A}. But arrangement {A, B, C}, {C, A, B} and {B, C, A} are considered the same.
The arrangements {A, C, B}, {B, A, C} and {C, B, A} are also considered the same.
Hence, there are only 2 distinct arrangements.
Input: N = 2
Output: 1
Explanation:
There can only 2 arrangements: {A, B} and {B, A}. Both these arrangements are same considering the clockwise arrangements.
Hence, only 1 distinct arrangement exist.
天真的方法:这个想法是生成N的所有排列 不同的对象。现在,如果先前布置中不存在当前布置的顺时针旋转,则迭代所有布置并增加计数器。检查完所有布置后,将计数器打印为可能的总的独特顺时针布置。
时间复杂度: O(N * N!)
辅助空间: O(N * N!)
高效的方法:该想法是使用以下观察:
Given a permutation {A1, A2, …, AN} of N distinct objects. Then, there can be N clockwise rotated arrangements denotes as follows:
{A1, A2, …, AN}
{AN, A1, …, AN-1}
{AN-1, AN, …, AN-2}
{AN-2, AN-1, …, AN-3}
and so on.
Therefore, for each permutation of length N there exist N clockwise arrangements. Also, for N distinct objects, there are N! arrangements without considering the clockwise arrangements same.
Hence, N*X = N!, where
N the clockwise arrangements of any permutation of length N.
X is the number of permutations considering clockwise arrangements the same.
N! is the total number of permutations.
Using the above equation:
X = N! / N
X = (N – 1)!
因此,考虑顺时针排列的不同排列的总数为(N – 1)! 。因此,任务是打印阶乘(N – 1)的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the factorial
// of given number
unsigned int factorial(unsigned int n)
{
// Base case
if (n == 0)
return 1;
// Recursively find the factorial
return n * factorial(n - 1);
}
// Driver Code
int main()
{
// Given N objects
int N = 4;
// Function Call
cout << factorial(N - 1);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to return the factorial
// of given number
static int factorial(int n)
{
// Base case
if (n == 0)
return 1;
// Recursively find the factorial
return n * factorial(n - 1);
}
// Driver Code
public static void main (String[] args)
{
// Given N objects
int N = 4;
// Function Call
System.out.print(factorial(N - 1));
}
}
// This code is contributed by math_lover
Python3
# Python3 program for the above approach
# Function to return the factorial
# of a given number
def factorial(n):
# Base Case
if n == 0:
return 1
# Recursively find the factorial
return n * factorial(n-1)
# Driver Code
# Given N objects
N = 4
# Function Call
print(factorial(N - 1))
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to return
// the factorial
// of given number
static int factorial(int n)
{
// Base case
if (n == 0)
return 1;
// Recursively find the
// factorial
return n * factorial(n - 1);
}
// Driver Code
public static void Main(String[] args)
{
// Given N objects
int N = 4;
// Function Call
Console.Write(factorial(N - 1));
}
}
// This code is contributed by gauravrajput1
6
时间复杂度: O(N)
辅助空间: O(1)