Krishnatejaswi S

Bit Strings

Link to the problem : Bit Strings

Intuition

Problem is asking us to find the number of combinations of 0s and 1s of length n. Basically its 2^n combinations.

Solution

1def main():
2    n = int(input())
3    print(2**n % 1000000007)
4
5if __name__ == "__main__":
6    main()

In python it feels cheating to use the power operator **, but it is a valid way to solve the problem.

In lower level languages like C, C++, Java, we need to use a loop to calculate the power of 2 and each time we take modulo 1e9+7.

 1#include <stdio.h>
 2#include <stdlib.h>
 3
 4int main() {
 5    int n;
 6    scanf("%d", &n);
 7    long long result = 1;
 8    for(int i = 0; i < n; i++) {
 9        result = (result * 2) % 1000000007;
10    }
11    printf("%lld\n", result);
12    return 0;
13}

Reply to this post by email ↪