Editorial for Funny Bits
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Editorial
Let us go with the example where , , and .
In other words, summing all positive integers that can be represented with at most bits with exactly bits that equal to , average it then round it down will get .
Question 1: How many numbers are there where and ?**
We notice that there will be a total of non-negative integers that can be represented with at most bits.
Each bit can be either set or unset (either or ). There are digits for each number, and we are choosing exactly of those bits to be set. This is a combination problem, and the number of ways to choose bits out of is given by the combination formula .
Question 2: What is the sum of all numbers where and ?**
A brute-force solution that generates all required numbers then summing it could work for smaller numbers of and , but anything above would likely kill your computer.
Instead, we make a few observations:
- There are numbers in total.
- Out of every single digit in all numbers, only of all digits will be set.
- For all numbers, each column of digits will have exactly the same number of set bits.
Knowing the above, we can deduce that there will be a total of bits set for every digit position. In other words, the sum of all numbers can be calculated as .
Question 3: How do we find the rounded-down average?
We have the sum and count from above. Dividing the sum by the count with a floor function attached to it would
This formula can be further simplified to .
Question 4: How do we find from and ?
Now that we have a formula that calculatees from and , ignoring the floor
function, the formula can be rearranged as . Adding a round
function to this would pass all test cases.
Final Implementation
k, a = map(int, input().split())
print(round(a * k / ((1 << k) - 1)))
Comments