给定两个整数M和X中,任务是找到可以生成包含X和-X长度M的序列的数目使得它们各自的计数相等,前缀和高达所得到的序列的每个索引是非负面的。
例子:
Input: M = 4, X = 5
Output: 2
Explanation:
There are only 2 possible sequences that have all possible prefix sums non-negative:
- {+5, +5, -5, -5}
- {+5, -5, +5, -5}
Input: M = 6, X = 2
Output: 5
Explanation:
There are only 5 possible sequences that have all possible prefix sums non-negative:
- {+2, +2, +2, -2, -2, -2}
- {+2, +2, -2, -2, +2, -2}
- {+2, -2, +2, -2, +2, -2}
- {+2, +2, -2, +2, -2, -2}
- {+2, -2, +2, +2, -2, -2}
天真的方法:最简单的方法是使用给定的整数+ X和-X生成大小为M的所有可能排列,并找到形成的每种排列的前缀和,并对前缀和数组仅包含非负元素的那些序列进行计数。在上述步骤之后,打印此序列的计数。
时间复杂度: O((M *(M!))/((M / 2)!) 2 )
辅助空间: O(M)
高效的方法:想法是观察形成任何序列的模式 在每个索引处出现的正X数始终大于或等于在出现的负X数。这类似于加泰罗尼亚语数字的模式。在这种情况下,请检查在任何时候出现的正X的数量始终大于或等于出现的负X的数量(这是加泰罗尼亚语数字的形式)。因此,任务是找到第N个加泰罗尼亚数字,其中N = M / 2 。
where, KN is Nth the Catalan Number and is the binomial coefficient.
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Binomial
// Coefficient C(n, r)
unsigned long int binCoff(unsigned int n,
unsigned int r)
{
// Stores the value C(n, r)
unsigned long int val = 1;
int i;
// Update C(n, r) = C(n, n - r)
if (r > (n - r))
r = (n - r);
// Find C(n, r) iteratively
for (i = 0; i < r; i++) {
val *= (n - i);
val /= (i + 1);
}
// Return the final value
return val;
}
// Function to find number of sequence
// whose prefix sum at each index is
// always non-negative
void findWays(int M)
{
// Find n
int n = M / 2;
unsigned long int a, b, ans;
// Value of C(2n, n)
a = binCoff(2 * n, n);
// Catalan number
b = a / (n + 1);
// Print the answer
cout << b;
}
// Driver Code
int main()
{
// Given M and X
int M = 4, X = 5;
// Function Call
findWays(M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the Binomial
// Coefficient C(n, r)
static long binCoff(long n, long r)
{
// Stores the value C(n, r)
long val = 1;
int i;
// Update C(n, r) = C(n, n - r)
if (r > (n - r))
r = (n - r);
// Find C(n, r) iteratively
for(i = 0; i < r; i++)
{
val *= (n - i);
val /= (i + 1);
}
// Return the final value
return val;
}
// Function to find number of sequence
// whose prefix sum at each index is
// always non-negative
static void findWays(int M)
{
// Find n
int n = M / 2;
long a, b, ans;
// Value of C(2n, n)
a = binCoff(2 * n, n);
// Catalan number
b = a / (n + 1);
// Print the answer
System.out.print(b);
}
// Driver Code
public static void main(String[] args)
{
// Given M and X
int M = 4, X = 5;
// Function Call
findWays(M);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
# Function to find the Binomial
# Coefficient C(n, r)
def binCoff(n, r):
# Stores the value C(n, r)
val = 1
# Update C(n, r) = C(n, n - r)
if (r > (n - r)):
r = (n - r)
# Find C(n, r) iteratively
for i in range(0, r):
val *= (n - i)
val //= (i + 1)
# Return the final value
return val
# Function to find number of sequence
# whose prefix sum at each index is
# always non-negative
def findWays(M):
# Find n
n = M // 2
# Value of C(2n, n)
a = binCoff(2 * n, n)
# Catalan number
b = a // (n + 1)
# Print the answer
print(b)
# Driver Code
if __name__ == '__main__':
# Given M and X
M = 4
X = 5
# Function Call
findWays(M)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the Binomial
// Coefficient C(n, r)
static long binCoff(long n, long r)
{
// Stores the value C(n, r)
long val = 1;
int i;
// Update C(n, r) = C(n, n - r)
if (r > (n - r))
r = (n - r);
// Find C(n, r) iteratively
for(i = 0; i < r; i++)
{
val *= (n - i);
val /= (i + 1);
}
// Return the final value
return val;
}
// Function to find number of sequence
// whose prefix sum at each index is
// always non-negative
static void findWays(int M, int X)
{
// Find n
int n = M / 2;
long a, b;
// Value of C(2n, n)
a = binCoff(2 * n, n);
// Catalan number
b = a / (n + 1);
// Print the answer
Console.WriteLine(b);
}
// Driver Code
public static void Main()
{
// Given M and X
int M = 4;
int X = 5;
// Function Call
findWays(M, X);
}
}
// This code is contributed by akhilsaini
Javascript
2
时间复杂度: O(M)
辅助空间: O(1)