150 likes | 262 Vues
This guide explores methods for computing the total number of 1s in the binary representations of all integers from 1 to N. We illustrate a brute force approach with a time complexity of O(N log N) and an optimized strategy with O(log N) complexity, derived through a deeper understanding of binary representation and its periodicities. The guide provides insights into how to structure calculations efficiently and presents various approaches, ensuring clarity for readers tackling similar problems in binary counting.
E N D
Count 1s Solution for HKOI2008 Junior Q3
Problem Description • Every non-negative integer can be written as a sequence of 0s and 1s. • Given N, find the number of occurrences of 1s in the binary representation of all integers from 1 to N. • e.g. when N= 5, 110=12 210=102 310=112 410=1002 510=1012 So, the answer is 7.
Notations • Denote P[m] to be the number of 1s in the binary representation of all integers from 0 to m-1. • Why m-1 instead of m? • Notice that there are exactly m integers from 0 to m-1. • Our goal is to find P when m=N+1. • For example, P[6]=7.
Doesn’t seem interesting? Some Interesting Facts
How about this? Some Interesting Facts
Statistics • #Att: 64 • Max: 100 • #Max: 5 • Min: 0 • Std Dev(Att): 29.07 • Mean(Att): 37.97
Solution 1 • Brute Force • Iterate from 1 to N, For each integer, Calculate the number of 1s • Time Complexity: O(N log N) • Expected Score: 50
Solution 1 • This is an illustration of Solution 1 with N=8 1: 1 1 2: 10 1 3: 11 2 4: 100 1 5: 101 2 6: 110 2 7: 111 3 8: 1000 1 Sum=13
Solution 2 • How about we exchange the columns and rows? For simplicity, we write out the zeros. • When m=N+1=9, 0 1 2 3 4 5 6 7 8 bit[3]: 0 0 0 0 0 0 0 0 1 1 bit[2]: 0 0 0 0 1 1 1 1 0 4 bit[1]: 0 0 1 1 0 0 1 1 0 4 bit[0]: 0 1 0 1 0 1 0 1 0 4 Sum=13 • How can we calculate the number of occurrences of 1s for bit[k]?
Solution 2 • Observe the periodicity. • For example, bit[1]: 0 01 10 01 10 0 • We can divide them into block of 0s and 1s, where 0-block and 1-block alternates. • The first block must be an 0-block. • The number of bits in each full block = 2k • The number of full blocks = m div 2k • Every block must be full except possibly the last one. • The remaining non-full block has (m mod 2k) bits.
Solution 2 • Iterate through all bit positions For each bit[k], calculate number of 1s • Time Complexity: O(log n) • Expected Score: 100
Other Approaches • Consider all the integers from 0 to m-1. • For all binary integers ending with ‘0’, if we discard the last bit, we get all integers from 0 to m/2-1. • For all binary integers ending with ‘1’, if we discard the last bit, we get all integers from 0 to m/2-1.
Other Approaches • Consider all the integers from 0 to m-1. • Let r= log2m. • For integers from 2r to m-1, if we discard the leading bit, we get all integers from 0 to m-2r-1.
Mistakes • What is the total number of possible bit positions?
Skills • Binary representation of non-negative integers. • Observation about the periodicity • 64-bit integer