📜  计算先减少然后增加的排列。

📅  最后修改于: 2021-04-29 11:04:49             🧑  作者: Mango

给定一个整数N ,计算A = [1、2,…,N]的排列数量,这些排列数量先减小然后增大。

例子:

方法:显然,序列从减少到增加的点被排列最小元素1占据。同样,递减序列始终跟随递增序列,这意味着最小元素的位置可以在[2,…,N-1]范围内。否则,将导致完全增加或完全减少的顺序。

例如,考虑N = 5和position = 2 ,即序列中位置2的最小元素。用Configuration = [_,1,_,_,_]计数所有可能的序列

从其余4个元素(2、3、4、5)中选择任意1个元素以填充位置1。例如,我们选择element =3。配置看起来像[3,1,_,_,_]。只能有1个序列,即[3,1,2,4,5]。因此,对于每个要填充的元素在位置1处,都可以执行序列。通过这种配置,总共可以进行4个C 1排列。

现在,考虑配置= [_,_,1,_,_]。
通过从其余4个元素中选择任意2个元素,可以应用类似的方法,并且对于每对元素,单个有效置换都是可能的。因此,此配置的排列数量= 4 C 2

N = 5的最终配置为[_,_,_,1,_]
选择其余4个元素中的任意3个,对于每个三元组,都会获得一个排列。在这种情况下,排列数= 4 C 3

最终计数= 4 C 1 + 4 C 2 + 4 C 3 (N = 5)

N的广义结果:

Count = N-1 C 1 + N-1 C 2 + … + N-1 C N-2 ,简化为:  \sum_{i = 1}^{N-2} N-1 C i = 2 N- 1-2(根据二项式定理)

C++
// C++ implementation of the above approach
#include 
  
#define ll long long
using namespace std;
  
const int mod = 1000000007;
  
// Function to compute a^n % mod
ll power(ll a, ll n)
{
  
    if (n == 0)
        return 1;
  
    ll p = power(a, n / 2) % mod;
    p = (p * p) % mod;
    if (n & 1)
        p = (p * a) % mod;
  
    return p;
}
  
// Function to count permutations that are
// first decreasing and then increasing
int countPermutations(int n)
{
  
    // For n = 1 return 0
    if (n == 1) {
        return 0;
    }
  
    // Calculate and return result
    return (power(2, n - 1) - 2) % mod;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << countPermutations(n);
    return 0;
}


Java
// Java implementation of the above approach 
class GFG
{
  
static final int mod = 1000000007; 
  
// Function to compute a^n % mod 
static long power(long a, long n) 
{ 
  
    if (n == 0) 
        return 1; 
  
    long p = power(a, n / 2) % mod; 
    p = (p * p) % mod; 
    if ((n & 1) == 1) 
        p = (p * a) % mod; 
  
    return p; 
} 
  
// Function to count permutations that are 
// first decreasing and then increasing 
static int countPermutations(int n) 
{ 
  
    // For n = 1 return 0 
    if (n == 1) 
    { 
        return 0; 
    } 
  
    // Calculate and return result 
    return ((int)power(2, n - 1) - 2) % mod; 
} 
  
// Driver code 
public static void main(String args[])
{ 
    int n = 5; 
    System.out.println(countPermutations(n)); 
} 
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the above approach
mod = 1000000007
  
# Function to compute a^n % mod
def power(a, n):
    if(n == 0):
        return 1
  
    p = power(a, int(n / 2)) % mod;
    p = (p * p) % mod
    if (n & 1):
        p = (p * a) % mod
  
    return p
  
# Function to count permutations that are
# first decreasing and then increasing
def countPermutations(n):
      
    # For n = 1 return 0
    if (n == 1):
        return 0
  
    # Calculate and return result
    return (power(2, n - 1) - 2) % mod
  
# Driver code
if __name__ == '__main__':
    n = 5
    print(countPermutations(n))
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
  
class GFG
{
      
static int mod = 1000000007; 
  
// Function to compute a^n % mod 
static long power(long a, long n) 
{ 
  
    if (n == 0) 
        return 1; 
  
    long p = power(a, n / 2) % mod; 
    p = (p * p) % mod; 
    if ((n & 1) == 1) 
        p = (p * a) % mod; 
  
    return p; 
} 
  
// Function to count permutations that are 
// first decreasing and then increasing 
static int countPermutations(int n) 
{ 
  
    // For n = 1 return 0 
    if (n == 1) 
    { 
        return 0; 
    } 
  
    // Calculate and return result 
    return ((int)power(2, n - 1) - 2) % mod; 
} 
  
// Driver code 
static public void Main ()
{
    int n = 5; 
    Console.WriteLine(countPermutations(n)); 
} 
}
  
// This code is contributed by ajit..


PHP


输出:
14