Editorial for Binary Strings


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: hselenal

Python

MOD = 10**9 + 7

N = int(input())
result = pow(2, N, MOD)

print(result)

Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        final int MOD = 1000000007;
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        scanner.close();

        long result = modPow(2, N, MOD);
        System.out.println(result);
    }

    private static long modPow(long base, int exp, int mod) {
        long result = 1;
        long power = base % mod;
        while (exp > 0) {
            if ((exp & 1) == 1) {
                result = (result * power) % mod;
            }
            power = (power * power) % mod;
            exp >>= 1;
        }
        return result;
    }
}

C++

#include <iostream>
using namespace std;

const int MOD = 1000000007;

int main() {
    int N;
    cin >> N;

    long long result = 1;
    long long base = 2;
    int exponent = N;

    while (exponent > 0) {
        if (exponent & 1) {
            result = (result * base) % MOD;
        }
        base = (base * base) % MOD;
        exponent >>= 1;
    }

    cout << result << "\n";
    return 0;
}

Comments

There are no comments at the moment.