📌  相关文章
📜  计数二进制字符串,其前半部分具有两次零

📅  最后修改于: 2021-06-25 17:22:40             🧑  作者: Mango

我们得到一个整数N。我们需要计算这样的长度为N的二进制字符串的总数,以使长度为N / 2的第一个字符串中的“ 0”的数目是第二个字符串中的“ 0”的数目的两倍。长度N / 2。
注意:N始终是偶数正整数。
例子:

幼稚的方法:我们可以生成所有长度为N的二进制字符串,然后可以一一检查是否所选字符串遵循给定的方案。但是这种方法的时间复杂度是指数的,并且是O(N * 2 N )。
高效方法:此方法基于对该问题的一些数学分析。
此方法的先决条件:具有模函数的阶乘和组合式。
注意:令n = N / 2。
如果我们逐步执行一些分析:

  1. 含有字符串的数量在第一半字符串和在第二半字符串的一个“0”两“0”,等于n的C 2 * n个C1。
  2. 这在第二半字符串上半年字符串和两个“0”包含四个“0”字符串的数量,等于n的C 4 * n个C2。
  3. 这在第一半字符串和在第二半字符串三个“0”包含6“0”字符串的数量,等于n C 6 * N的C 3。

我们重复上述过程,直到如果n为偶数,前半部分的“ 0”数变为n,或者如果n为奇数,则为(n-1)。
因此,对于2 <= 2 * i <= n,我们的最终答案是Σ( n C (2 * i) * n C i )
现在,我们只需使用置换和组合即可计算所需的字符串数。
算法 :

int n = N/2;
for(int i=2;i<=n;i=i+2)
             ans += ( nCr(n, i) * nCr(n, i/2);

注意:您可以使用动态编程技术来预先计算CoeFunc(N,i)即n C i
如果我们预先计算O(N * N)中的CoeFunc(N,i),则时间复杂度为O(N)。

C++
// CPP for finding number of binary strings
// number of '0' in first half is double the
// number of '0' in second half of string
#include 
 
// pre define some constant
#define mod 1000000007
#define max 1001
using namespace std;
 
// global values for pre computation
long long int nCr[1003][1003];
 
void preComputeCoeff()
{
    for (int i = 0; i < max; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0 || j == i)
                nCr[i][j] = 1;
            else
                nCr[i][j] = (nCr[i - 1][j - 1] +
                             nCr[i - 1][j]) % mod;
        }
    }
}
 
// function to print number of required string
long long int computeStringCount(int N)
{
    int n = N / 2;
    long long int ans = 0;
 
    // calculate answer using proposed algorithm
    for (int i = 2; i <= n; i += 2)
        ans = (ans + ((nCr[n][i] * nCr[n][i / 2])
                      % mod)) % mod;
    return ans;
}
 
// Driver code
int main()
{
    preComputeCoeff();
    int N = 3;
    cout << computeStringCount(N) << endl;
    return 0;
}


Java
// Java program for finding number of binary strings
// number of '0' in first half is double the
// number of '0' in second half of string
class GFG {
     
    // pre define some constant
    static final long mod = 1000000007;
    static final long max = 1001;
     
    // global values for pre computation
    static long nCr[][] = new long[1003][1003];
     
    static void preComputeCoeff()
    {
        for (int i = 0; i < max; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i)
                    nCr[i][j] = 1;
                else
                    nCr[i][j] = (nCr[i - 1][j - 1] +
                                nCr[i - 1][j]) % mod;
            }
        }
    }
     
    // function to print number of required string
    static long computeStringCount(int N)
    {
        int n = N / 2;
        long ans = 0;
     
        // calculate answer using proposed algorithm
        for (int i = 2; i <= n; i += 2)
            ans = (ans + ((nCr[n][i] * nCr[n][i / 2])
                        % mod)) % mod;
        return ans;
    }
     
    // main function
    public static void main(String[] args)
    {
        preComputeCoeff();
        int N = 3;
        System.out.println( computeStringCount(N) );
         
    }
}
 
// This code is contributed by Arnab Kundu.


Python3
# Python3 for finding number of binary strings
# number of '0' in first half is double the
# number of '0' in second half of string
 
# pre define some constant
mod = 1000000007
Max = 1001
 
# global values for pre computation
nCr = [[0 for _ in range(1003)]
          for i in range(1003)]
 
def preComputeCoeff():
 
    for i in range(Max):
        for j in range(i + 1):
            if (j == 0 or j == i):
                nCr[i][j] = 1
            else:
                nCr[i][j] = (nCr[i - 1][j - 1] +
                             nCr[i - 1][j]) % mod
         
# function to print number of required string
def computeStringCount(N):
    n = N // 2
    ans = 0
 
    # calculate answer using proposed algorithm
    for i in range(2, n + 1, 2):
        ans = (ans + ((nCr[n][i] *
                       nCr[n][i // 2]) % mod)) % mod
    return ans
 
# Driver code
preComputeCoeff()
N = 3
print(computeStringCount(N))
 
# This code is contributed by mohit kumar


C#
// C# program for finding number of binary
// strings number of '0' in first half is
// double the number of '0' in second half
// of string
using System;
 
class GFG {
     
    // pre define some constant
    static long mod = 1000000007;
    static long max = 1001;
     
    // global values for pre computation
    static long [,]nCr = new long[1003,1003];
     
    static void preComputeCoeff()
    {
        for (int i = 0; i < max; i++)
        {
            for (int j = 0; j <= i; j++)
            {
                if (j == 0 || j == i)
                    nCr[i,j] = 1;
                else
                    nCr[i,j] = (nCr[i - 1,j - 1]
                          + nCr[i - 1,j]) % mod;
            }
        }
    }
     
    // function to print number of required
    // string
    static long computeStringCount(int N)
    {
        int n = N / 2;
        long ans = 0;
     
        // calculate answer using proposed
        // algorithm
        for (int i = 2; i <= n; i += 2)
            ans = (ans + ((nCr[n,i]
                * nCr[n,i / 2]) % mod)) % mod;
                 
        return ans;
    }
     
    // main function
    public static void Main()
    {
        preComputeCoeff();
        int N = 3;
        Console.Write( computeStringCount(N) );
         
    }
}
 
// This code is contributed by nitin mittal.


Javascript


输出:
0