给定两个整数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的数量。这类似于Catalan Numbers的模式。在这种情况下,检查在任何时候发生的正 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)