📜  通过在 N 个硬币上执行 S 次翻转来计算所有可能的独特结果

📅  最后修改于: 2021-09-17 16:00:49             🧑  作者: Mango

给定两个正整数NS ,任务是计算对N 个硬币执行 S 次翻转操作时可能出现的唯一结果的数量。

例子:

朴素的方法:给定的问题可以通过使用递归来解决,其递归状态定义为:

  • 考虑F(N, S)表示当掷N 个硬币且总翻转次数等于S时唯一结果的数量。
  • 那么F(N, S)也可以表示为 1 次翻转或 2 次翻转的所有组合的总和,即,
  • 此递推关系的基本情况是F(K, K) ,其值为1对所有(K > 1)
  • 下表显示了F(N, S) = F(N – 1, S – 1) + F(N – 1, S – 2) 的分布,其中F(K, K) = 1

请按照以下步骤解决问题:

  • 声明一个函数,比如numberOfUniqueOutcomes(N, S) ,它分别以允许的硬币和翻转次数作为参数,并执行以下步骤:
    • 如果S的值小于N ,则从函数返回0
    • 如果N 的值为S1 ,则从函数返回1 ,因为这是唯一组合之一。
    • 递归返回两个递归状态的总和为:
  • 完成上述步骤后,将函数numberOfUniqueOutcomes(N, S)返回的值打印为结果的结果数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to recursively count the
// number of unique outcomes possible
// S flips are performed on N coins
int numberOfUniqueOutcomes(int N, int S)
{
    // Base Cases
    if (S < N)
        return 0;
    if (N == 1 || N == S)
        return 1;
 
    // Recursive Calls
    return (numberOfUniqueOutcomes(N - 1, S - 1)
            + numberOfUniqueOutcomes(N - 1, S - 2));
}
 
// Driver Code
int main()
{
    int N = 3, S = 4;
 
    cout << numberOfUniqueOutcomes(N, S);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to recursively count the
  // number of unique outcomes possible
  // S flips are performed on N coins
  static int numberOfUniqueOutcomes(int N, int S)
  {
 
    // Base Cases
    if (S < N)
      return 0;
    if (N == 1 || N == S)
      return 1;
 
    // Recursive Calls
    return (numberOfUniqueOutcomes(N - 1, S - 1)
            + numberOfUniqueOutcomes(N - 1, S - 2));
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int N = 3, S = 4;
    System.out.println(numberOfUniqueOutcomes(N, S));
  }
}
 
//  This code is contributed by avanitrachhadiya2155


Python3
# Python3 program for the above approach
 
# Function to recursively count the
# number of unique outcomes possible
# S flips are performed on N coins
def numberOfUniqueOutcomes(N, S):
     
    # Base Cases
    if (S < N):
        return 0
    if (N == 1 or N == S):
        return 1
 
    # Recursive Calls
    return (numberOfUniqueOutcomes(N - 1, S - 1) +
            numberOfUniqueOutcomes(N - 1, S - 2))
 
# Driver Code
if __name__ == '__main__':
     
    N, S = 3, 4
 
    print (numberOfUniqueOutcomes(N, S))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to recursively count the
// number of unique outcomes possible
// S flips are performed on N coins
static int numberOfUniqueOutcomes(int N, int S)
{
     
    // Base Cases
    if (S < N)
        return 0;
    if (N == 1 || N == S)
        return 1;
     
    // Recursive Calls
    return (numberOfUniqueOutcomes(N - 1, S - 1) +
            numberOfUniqueOutcomes(N - 1, S - 2));
}
 
// Driver Code
static public void Main()
{
    int N = 3, S = 4;
     
    Console.WriteLine(numberOfUniqueOutcomes(N, S));
}
}
 
// This code is contributed by sanjoy_62


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Dimensions of the DP table
#define size 1001
 
// Stores the dp states
int ans[size][size] = { 0 };
 
// Function to recursively count the
// number of unique outcomes possible
// by performing S flips on N coins
int numberOfUniqueOutcomes(int n, int s)
{
    // Base Case
    if (s < n)
        ans[n][s] = 0;
 
    else if (n == 1 || n == s)
        ans[n][s] = 1;
 
    // If the count for the current
    // state is not calculated, then
    // calculate it recursively
    else if (!ans[n][s]) {
        ans[n][s] = numberOfUniqueOutcomes(n - 1,
                                           s - 1)
                    + numberOfUniqueOutcomes(n - 1,
                                             s - 2);
    }
 
    // Otherwise return the
    // already calculated value
    return ans[n][s];
}
 
// Driver Code
int main()
{
    int N = 5, S = 8;
 
    cout << numberOfUniqueOutcomes(N, S);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG{
  
 // Dimensions of the DP table
static int size = 100;
static int [][]ans = new int[size][size];
   
static void initialize()
{
   
  // Stores the dp states
  for(int i = 0; i < size; i++)
  {
    for(int j = 0; j < size; j++)
    {
        ans[i][j] = 0;
    }
}
}
 
// Function to recursively count the
// number of unique outcomes possible
// by performing S flips on N coins
static int numberOfUniqueOutcomes(int n, int s)
{
    // Base Case
    if (s < n)
        ans[n][s] = 0;
 
    else if (n == 1 || n == s)
        ans[n][s] = 1;
 
    // If the count for the current
    // state is not calculated, then
    // calculate it recursively
    else if (ans[n][s] == 0) {
        ans[n][s] = numberOfUniqueOutcomes(n - 1,
                                           s - 1)
                    + numberOfUniqueOutcomes(n - 1,
                                             s - 2);
    }
 
    // Otherwise return the
    // already calculated value
    return ans[n][s];
}
 
// Driver Code
public static void main(String args[])
{
    initialize();
    int N = 5, S = 8;
    System.out.println(numberOfUniqueOutcomes(N, S));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Python3
# Python3 program for the above approach
 
# Dimensions of the DP table
size = 100
 
# Stores the dp states
ans = [[0 for i in range(size)]
          for j in range(size)]
 
# Function to recursively count the
# number of unique outcomes possible
# by performing S flips on N coins
def numberOfUniqueOutcomes(n, s):
     
    # Base Case
    if (s < n):
        ans[n][s] = 0;
     
    elif (n == 1 or n == s):
        ans[n][s] = 1;
     
    # If the count for the current
    # state is not calculated, then
    # calculate it recursively
    elif(ans[n][s] == 0):
        ans[n][s] = (numberOfUniqueOutcomes(n - 1, s - 1) +
                     numberOfUniqueOutcomes(n - 1, s - 2))
     
    # Otherwise return the
    # already calculated value
    return ans[n][s];
 
# Driver Code
N = 5
S = 8
 
print(numberOfUniqueOutcomes(N, S))
 
# This code is contributed by rag2127


C#
// C# program for the above approach
using System;
 
class GFG{
  
// Dimensions of the DP table
static int size = 100;
static int [,]ans = new int[size, size];
   
static void initialize()
{
   
    // Stores the dp states
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            ans[i, j] = 0;
        }
    }
}
 
// Function to recursively count the
// number of unique outcomes possible
// by performing S flips on N coins
static int numberOfUniqueOutcomes(int n, int s)
{
     
    // Base Case
    if (s < n)
        ans[n, s] = 0;
 
    else if (n == 1 || n == s)
        ans[n, s] = 1;
 
    // If the count for the current
    // state is not calculated, then
    // calculate it recursively
    else if (ans[n, s] == 0)
    {
        ans[n, s] = numberOfUniqueOutcomes(n - 1,
                                           s - 1) +
                    numberOfUniqueOutcomes(n - 1,
                                           s - 2);
    }
 
    // Otherwise return the
    // already calculated value
    return ans[n,s];
}
 
// Driver Code
public static void Main(string []args)
{
    initialize();
    int N = 5, S = 8;
     
    Console.WriteLine(numberOfUniqueOutcomes(N, S));
}
}
 
// This code is contributed by AnkThon


Javascript


输出:
3

时间复杂度: O(2 N )
辅助空间: O(N)

高效方法:上述方法也可以通过存储递归状态来优化,因为它包含重叠的子问题。因此,想法是使用记忆化来存储重复的状态。请按照以下步骤解决问题:

  • 初始化一个二维数组,比如维度为N*M 的dp[][] ,使得dp[i][j]存储使用i 个硬币和j次翻转的可能结果的数量。
  • 声明一个函数,比如numberOfUniqueOutcomes(N, S) ,它分别将允许的硬币数量和翻转次数作为参数,并执行以下步骤:
    • 如果S的值小于N ,则将dp[N][S]的值更新为0并从函数返回该值。
    • 如果N 的值为S1 ,则将dp[N][S]的值更新为1并从函数返回该值,因为这是唯一组合之一。
    • 如果DP的值[n]是已计算出的[S],然后从该函数返回值DP [N] [S]。
    • 递归更新两个递归状态的dp[N][S]总和的值,如下所示,并从函数返回该值。
  • 完成上述步骤后,将函数numberOfUniqueOutcomes(N, S)返回的值打印为结果的结果数。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Dimensions of the DP table
#define size 1001
 
// Stores the dp states
int ans[size][size] = { 0 };
 
// Function to recursively count the
// number of unique outcomes possible
// by performing S flips on N coins
int numberOfUniqueOutcomes(int n, int s)
{
    // Base Case
    if (s < n)
        ans[n][s] = 0;
 
    else if (n == 1 || n == s)
        ans[n][s] = 1;
 
    // If the count for the current
    // state is not calculated, then
    // calculate it recursively
    else if (!ans[n][s]) {
        ans[n][s] = numberOfUniqueOutcomes(n - 1,
                                           s - 1)
                    + numberOfUniqueOutcomes(n - 1,
                                             s - 2);
    }
 
    // Otherwise return the
    // already calculated value
    return ans[n][s];
}
 
// Driver Code
int main()
{
    int N = 5, S = 8;
 
    cout << numberOfUniqueOutcomes(N, S);
 
    return 0;
}

Java

// Java program for the above approach
 
import java.util.*;
 
class GFG{
  
 // Dimensions of the DP table
static int size = 100;
static int [][]ans = new int[size][size];
   
static void initialize()
{
   
  // Stores the dp states
  for(int i = 0; i < size; i++)
  {
    for(int j = 0; j < size; j++)
    {
        ans[i][j] = 0;
    }
}
}
 
// Function to recursively count the
// number of unique outcomes possible
// by performing S flips on N coins
static int numberOfUniqueOutcomes(int n, int s)
{
    // Base Case
    if (s < n)
        ans[n][s] = 0;
 
    else if (n == 1 || n == s)
        ans[n][s] = 1;
 
    // If the count for the current
    // state is not calculated, then
    // calculate it recursively
    else if (ans[n][s] == 0) {
        ans[n][s] = numberOfUniqueOutcomes(n - 1,
                                           s - 1)
                    + numberOfUniqueOutcomes(n - 1,
                                             s - 2);
    }
 
    // Otherwise return the
    // already calculated value
    return ans[n][s];
}
 
// Driver Code
public static void main(String args[])
{
    initialize();
    int N = 5, S = 8;
    System.out.println(numberOfUniqueOutcomes(N, S));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.

蟒蛇3

# Python3 program for the above approach
 
# Dimensions of the DP table
size = 100
 
# Stores the dp states
ans = [[0 for i in range(size)]
          for j in range(size)]
 
# Function to recursively count the
# number of unique outcomes possible
# by performing S flips on N coins
def numberOfUniqueOutcomes(n, s):
     
    # Base Case
    if (s < n):
        ans[n][s] = 0;
     
    elif (n == 1 or n == s):
        ans[n][s] = 1;
     
    # If the count for the current
    # state is not calculated, then
    # calculate it recursively
    elif(ans[n][s] == 0):
        ans[n][s] = (numberOfUniqueOutcomes(n - 1, s - 1) +
                     numberOfUniqueOutcomes(n - 1, s - 2))
     
    # Otherwise return the
    # already calculated value
    return ans[n][s];
 
# Driver Code
N = 5
S = 8
 
print(numberOfUniqueOutcomes(N, S))
 
# This code is contributed by rag2127

C#

// C# program for the above approach
using System;
 
class GFG{
  
// Dimensions of the DP table
static int size = 100;
static int [,]ans = new int[size, size];
   
static void initialize()
{
   
    // Stores the dp states
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            ans[i, j] = 0;
        }
    }
}
 
// Function to recursively count the
// number of unique outcomes possible
// by performing S flips on N coins
static int numberOfUniqueOutcomes(int n, int s)
{
     
    // Base Case
    if (s < n)
        ans[n, s] = 0;
 
    else if (n == 1 || n == s)
        ans[n, s] = 1;
 
    // If the count for the current
    // state is not calculated, then
    // calculate it recursively
    else if (ans[n, s] == 0)
    {
        ans[n, s] = numberOfUniqueOutcomes(n - 1,
                                           s - 1) +
                    numberOfUniqueOutcomes(n - 1,
                                           s - 2);
    }
 
    // Otherwise return the
    // already calculated value
    return ans[n,s];
}
 
// Driver Code
public static void Main(string []args)
{
    initialize();
    int N = 5, S = 8;
     
    Console.WriteLine(numberOfUniqueOutcomes(N, S));
}
}
 
// This code is contributed by AnkThon

Javascript


输出:
15

时间复杂度: O(N*S)
辅助空间: O(N*S)