Binary Sorting (Easy)
InterviewTime limit1sMemory limit1024 MB
Given a binary string of length N, find its 0-based position after all length-N binary strings are sorted by popcount, then by their reversed value.
- Level
Easy3 of 10
- Topics
- Bit manipulation, Sorting
- Solved
- No attempts yet
Problem
Hanbyeol collects every binary string of length and sorts them in a special way before using them. The sorting rules are as follows.
- Sort by the number of 1s in the binary string, in ascending order.
- If two strings have the same number of 1s, sort them by the ascending order of the string after it is reversed.
For example, when is 3, the strings are sorted in this order.
- 000
- 100
- 010
- 001
- 110
- 101
- 011
- 111
One day, his friend Sunlin came to him with a binary string of length and asked for its position in the sorted list. Hanbyeol has been busy and has not sorted the strings yet. Help him!
Input
The first line contains the length of the binary strings.
The second line contains a binary string of length .
The binary string may start with 0.
Output
Print the position of the given binary string after sorting all binary strings of the same length, counting from 0.