📌  相关文章
📜  计算长度为N且仅包含0和1的二进制字符串的数量

📅  最后修改于: 2021-06-25 14:18:39             🧑  作者: Mango

给定整数N ,任务是计算长度为N的仅具有0和1的二进制字符串的数量。
注意:由于计数可能非常大,请以10 ^ 9 + 7为模返回答案。

例子:

方法:使用置换和组合可以轻松解决该问题。在字符串的每个位置只能有两种可能性,即0或1。因此,长度为N的字符串中的0和1的置换总数由2 * 2 * 2 * …(N次)给出,即2 ^ N。答案可能非常大,因此返回以10 ^ 9 + 7为模。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long
#define mod (ll)(1e9 + 7)
 
// Iterative Function to calculate (x^y)%p in O(log y)
ll power(ll x, ll y, ll p)
{
    ll 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 count the number of binary
// strings of length N having only 0's and 1's
ll findCount(ll N)
{
    int count = power(2, N, mod);
    return count;
}
 
// Driver code
int main()
{
    ll N = 25;
 
    cout << findCount(N);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
static int mod = (int) (1e9 + 7);
 
// Iterative Function to calculate (x^y)%p in O(log y)
static int power(int x, int y, int p)
{
    int 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)==1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Function to count the number of binary
// strings of length N having only 0's and 1's
static int findCount(int N)
{
    int count = power(2, N, mod);
    return count;
}
 
// Driver code
public static void main(String[] args)
{
        int N = 25;
        System.out.println(findCount(N));
}
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python 3 implementation of the approach
mod = 1000000007
 
# 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 count the number of binary
# strings of length N having only 0's and 1's
def findCount(N):
    count = power(2, N, mod)
    return count
 
# Driver code
if __name__ == '__main__':
    N = 25
    print(findCount(N))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
    static int mod = (int) (1e9 + 7);
     
    // Iterative Function to calculate (x^y)%p in O(log y)
    static int power(int x, int y, int p)
    {
        int 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) == 1)
                res = (res * x) % p;
     
            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }
        return res;
    }
     
    // Function to count the number of binary
    // strings of length N having only 0's and 1's
    static int findCount(int N)
    {
        int count = power(2, N, mod);
        return count;
    }
     
    // Driver code
    public static void Main()
    {
            int N = 25;
            Console.WriteLine(findCount(N));
    }
}
 
// This code is contributed by Ryuga


PHP
 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 count the number of binary
// strings of length N having only 0's and 1's
function findCount($N)
{
    $count = power(2, $N);
    return $count;
}
 
// Driver code
$N = 25;
 
echo findCount($N);
     
// This code is contributed by Rajput-Ji
?>


Javascript


输出:
33554432