📜  在大小为K的数组的N次交换中,项返回到其初始位置的方式的数量

📅  最后修改于: 2021-06-25 15:57:41             🧑  作者: Mango

给定两个数字KN ,任务是找到方法的数量,以使位置i处的项目以N个步骤返回到长度为K的数组中的初始位置,其中每个步骤中都可以交换该项目与K中的任何其他项目

例子:

方法:解决此问题的想法是使用组合的概念。这个想法是,在每一步中,都有K – 1种可能性可以将项目放置在下一个位置。为了实现这一点,使用数组F [] ,其中F [i]表示将项目放置在第i步的位置1的方式的数量。由于假定该项目不属于上一步所属的人,因此,必须为每个步骤减去上一步的方式数。因此,数组F []可以填充为:

F[i] = (K - 1)(i - 1) - F[i - 1]

最后,返回数组F []的最后一个元素。

下面是该方法的实现:

C++
// C++ program to find the number of ways
// in which an item returns back to its
// initial position in N swaps
// in an array of size K
 
#include 
using namespace std;
 
#define mod 1000000007
 
// Function to calculate (x^y)%p in O(log y)
long long power(long x, long y)
{
    long p = mod;
 
    // Initialize result
    long res = 1;
 
    // Update x if it is more than or
    // equal to p
    x = x % p;
 
    while (y > 0) {
        // If y is odd, multiply
        // x with result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        // y = y/2
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
 
// Function to return the number of ways
long long solve(int n, int k)
{
    // Base case
    if (n == 1)
        return 0LL;
 
    // Recursive case
    // F(n) = (k-1)^(n-1) - F(n-1).
    return (power((k - 1), n - 1) % mod
            - solve(n - 1, k) + mod)
           % mod;
}
 
// Drivers code
int main()
{
    int n = 4, k = 5;
 
    // Function calling
    cout << solve(n, k);
 
    return 0;
}


Java
// Java program to find the number of ways
// in which an item returns back to its
// initial position in N swaps
// in an array of size K
class GFG{
 
static int mod = 1000000007;
 
// Function to calculate (x^y)%p in O(log y)
public static int power(int x, int y)
{
    int p = mod;
   
    // Initialize result
    int res = 1;
   
    // Update x if it is more than
    // or equal to p
    x = x % p;
   
    while (y > 0)
    {
         
        // If y is odd, multiply
        // x with result
        if ((y & 1) != 0)
            res = (res * x) % p;
   
        // y must be even now
        // y = y/2
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
   
// Function to return the number of ways
public static int solve(int n, int k)
{
     
    // Base case
    if (n == 1)
        return 0;
   
    // Recursive case
    // F(n) = (k-1)^(n-1) - F(n-1).
    return (power((k - 1), n - 1) % mod -
             solve(n - 1, k) + mod) % mod;
}
 
// Driver code
public static void main(String []args)
{
    int n = 4, k = 5;
     
    // Function calling
    System.out.println(solve(n, k));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to find the number of ways
# in which an item returns back to its
# initial position in N swaps
# in an array of size K
 
mod = 1000000007
 
# Function to calculate (x^y)%p in O(log y)
def power(x, y):
 
    p = mod
 
    # Initialize result
    res = 1
 
    # Update x if it is more than or
    # equal to p
    x = x % p
 
    while (y > 0) :
        # If y is odd, multiply
        # x with result
        if (y & 1) != 0 :
            res = (res * x) % p
 
        # y must be even now
        # y = y/2
        y = y >> 1
        x = (x * x) % p
 
    return res
 
# Function to return the number of ways
def solve(n, k):
 
    # Base case
    if (n == 1) :
        return 0
 
    # Recursive case
    # F(n) = (k-1)^(n-1) - F(n-1).
    return (power((k - 1), n - 1) % mod - solve(n - 1, k) + mod) % mod
 
n, k = 4, 5
 
# Function calling
print(solve(n, k))
 
# This code is contributed by divyesh072019


C#
// C# program to find the number of ways
// in which an item returns back to its
// initial position in N swaps
// in an array of size K
using System;
class GFG
{
  
  static int mod = 1000000007;
 
  // Function to calculate
  // (x^y)%p in O(log y)
  public static int power(int x,
                          int y)
  {
    int p = mod;
 
    // Initialize result
    int res = 1;
 
    // Update x if it
    // is more than
    // or equal to p
    x = x % p;
 
    while (y > 0)
    {
      // If y is odd, multiply
      // x with result
      if ((y & 1) != 0)
        res = (res * x) % p;
 
      // y must be even now
      // y = y/2
      y = y >> 1;
      x = (x * x) % p;
    }
    return res;
  }
 
  // Function to return
  // the number of ways
  public static int solve(int n,
                          int k)
  {
    // Base case
    if (n == 1)
      return 0;
 
    // Recursive case
    // F(n) = (k-1)^(n-1) - F(n-1).
    return (power((k - 1), n - 1) % mod -
            solve(n - 1, k) + mod) % mod;
  }
 
  // Driver code
  public static void Main(string []args)
  {
    int n = 4, k = 5;
 
    // Function calling
    Console.Write(solve(n, k));
  }
}
 
// This code is contributed by rutvik_56


Javascript


输出:
52