📜  从二进制字符串中的1开始的唯一排列数

📅  最后修改于: 2021-06-25 11:11:34             🧑  作者: Mango

给定一个由0和1组成的二进制字符串。任务是找到以1开头的字符串的唯一排列数。
注意:由于答案可能非常大,请以模数10 9 + 7打印答案。
例子:

Input : str ="10101001001"
Output : 210

Input : str ="101110011"
Output : 56

这个想法是首先在给定的字符串找到1的计数和0的计数。现在让我们考虑一下字符串的长度L 并且该字符串包含至少一个1。设1为n 和0的数字是m 。在n个1中,我们必须在字符串的开头放置一个1,因此我们要在n-1 1的左边,而m 0的w必须排列这些(n-1) 1和m 0的长度(L-1)的字符串。
因此,排列数将是:

(L-1)! / ((n-1)!*(m)!)

下面是上述想法的实现:

C++
// C++ program to find number of unique permutations
// of a binary string starting with 1
 
#include 
using namespace std;
 
#define MAX 1000003
#define mod 1000000007
 
// Array to store factorial of i at
// i-th index
long long fact[MAX];
 
// Precompute factorials under modulo mod
// upto MAX
void factorial()
{
    // factorial of 0 is 1
    fact[0] = 1;
 
    for (int i = 1; i < MAX; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
}
 
// Iterative Function to calculate (x^y)%p in O(log y)
long long power(long long x, long long y, long long p)
{
    long long res = 1; // Initialize result
 
    x = x % p; // Update x if it is more than or
    // equal to 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 >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Function to find the modular inverse
long long inverse(int n)
{
 
    return power(n, mod - 2, mod);
}
 
// Function to find the number of permutation
// starting with 1 of a binary string
int countPermutation(string s)
{
    // Generate factorials upto MAX
    factorial();
 
    int length = s.length(), num1 = 0, num0;
    long long count = 0;
 
    // find number of 1's
    for (int i = 0; i < length; i++) {
        if (s[i] == '1')
            num1++;
    }
 
    // number of 0's
    num0 = length - num1;
 
    // Find the number of permuattion of
    // string starting with 1 using the formulae:
    // (L-1)! / ((n-1)!*(m)!)
    count = (fact[length - 1] *
            inverse((fact[num1 - 1] *
                    fact[num0]) % mod)) % mod;
 
    return count;
}
 
// Driver code
int main()
{
    string str = "10101001001";
 
    cout << countPermutation(str);
 
    return 0;
}


Java
// Java program to find number of unique permutations
// of a binary string starting with 1
   
public class Improve {
     
    final static int MAX = 1000003 ;
    final static int mod = 1000000007 ;
     
    // Array to store factorial of i at
    // i-th index
    static long fact[] = new long [MAX];
     
    // Pre-compute factorials under modulo mod
    // upto MAX
    static void factorial()
    {
        // factorial of 0 is 1
        fact[0] = 1;
       
        for (int i = 1; i < MAX; i++) {
            fact[i] = (fact[i - 1] * i) % mod;
        }
    }
       
    // Iterative Function to calculate (x^y)%p in O(log y)
    static long power(long x, long y, long p)
    {
        long res = 1; // Initialize result
       
        x = x % p; // Update x if it is more than or
        // equal to p
       
        while (y > 0) {
            // If y is odd, multiply x with result
            if (y % 2 != 0)
                res = (res * x) % p;
       
            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }
        return res;
    }
       
    // Function to find the modular inverse
    static long inverse(int n)
    {
       
        return power(n, mod - 2, mod);
    }
       
    // Function to find the number of permutation
    // starting with 1 of a binary string
    static int countPermutation(String s)
    {
        // Generate factorials upto MAX
        factorial();
       
        int length = s.length(), num1 = 0, num0;
        long count = 0;
       
        // find number of 1's
        for (int i = 0; i < length; i++) {
            if (s.charAt(i) == '1')
                num1++;
        }
       
        // number of 0's
        num0 = length - num1;
       
        // Find the number of permuattion of
        // string starting with 1 using the formulae:
        // (L-1)! / ((n-1)!*(m)!)
        count = (fact[length - 1] * 
                inverse((int) ((fact[num1 - 1] * 
                        fact[num0]) % mod))) % mod;
       
        return (int) count;
    }
     
    // Driver code
    public static void main(String args[])
    {
           String str = "10101001001";
            
           System.out.println(countPermutation(str));
    }
    // This Code is contributed by ANKITRAI1
}


Python 3
# Python 3 program to find number
# of unique permutations of a
# binary string starting with 1
MAX = 1000003
mod = 1000000007
 
# Array to store factorial of
# i at i-th index
fact = [0] * MAX
 
# Precompute factorials under
# modulo mod upto MAX
def factorial():
     
    # factorial of 0 is 1
    fact[0] = 1
 
    for i in range(1, MAX):
        fact[i] = (fact[i - 1] * i) % mod
 
# Iterative Function to calculate
# (x^y)%p in O(log y)
def power(x, y, p):
     
    res = 1 # Initialize result
 
    x = x % p # Update x if it is
              # more than or equal to 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 >> 1 # y = y/2
        x = (x * x) % p
     
    return res
 
# Function to find the modular inverse
def inverse( n):
    return power(n, mod - 2, mod)
 
# Function to find the number of
# permutation starting with 1 of
# a binary string
def countPermutation(s):
     
    # Generate factorials upto MAX
    factorial()
 
    length = len(s)
    num1 = 0
    count = 0
 
    # find number of 1's
    for i in range(length) :
        if (s[i] == '1'):
            num1 += 1
             
    # number of 0's
    num0 = length - num1
 
    # Find the number of permuattion
    # of string starting with 1 using
    # the formulae: (L-1)! / ((n-1)!*(m)!)
    count = (fact[length - 1] *
            inverse((fact[num1 - 1] *
                     fact[num0]) % mod)) % mod
 
    return count
 
# Driver code
if __name__ =="__main__":
    s = "10101001001"
 
    print(countPermutation(s))
 
# This code is contributed
# by ChitraNayal


C#
// C# program to find number of
// unique permutations of a
// binary string starting with 1
using System;
     
class GFG
{
static int MAX = 1000003 ;
static int mod = 1000000007 ;
 
// Array to store factorial
// of i at i-th index
static long []fact = new long [MAX];
 
// Pre-compute factorials under
// modulo mod upto MAX
static void factorial()
{
    // factorial of 0 is 1
    fact[0] = 1;
     
    for (int i = 1; i < MAX; i++)
    {
        fact[i] = (fact[i - 1] * i) % mod;
    }
}
     
// Iterative Function to calculate
// (x^y)%p in O(log y)
static long power(long x,
                  long y, long p)
{
    long res = 1; // Initialize result
     
    x = x % p; // Update x if it is more
               // than or equal to p
     
    while (y > 0)
    {
        // If y is odd, multiply
        // x with result
        if (y % 2 != 0)
            res = (res * x) % p;
     
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
     
// Function to find the modular inverse
static long inverse(int n)
{
    return power(n, mod - 2, mod);
}
     
// Function to find the number of
// permutation starting with 1 of
// a binary string
static int countPermutation(string s)
{
    // Generate factorials upto MAX
    factorial();
     
    int length = s.Length, num1 = 0, num0;
    long count = 0;
     
    // find number of 1's
    for (int i = 0; i < length; i++)
    {
        if (s[i] == '1')
            num1++;
    }
     
    // number of 0's
    num0 = length - num1;
     
    // Find the number of permuattion
    // of string starting with 1 using
    // the formulae: (L-1)! / ((n-1)!*(m)!)
    count = (fact[length - 1] *
             inverse((int) ((fact[num1 - 1] *
                    fact[num0]) % mod))) % mod;
     
    return (int) count;
}
 
// Driver code
public static void Main()
{
    string str = "10101001001";
         
    Console.WriteLine(countPermutation(str));
}
}
 
// This code is contributed
// by anuj_67


Javascript


输出:
210

时间复杂度: O(n),其中n是字符串的长度。