Блог пользователя prathams

Автор prathams, история, 11 лет назад, По-английски

How to count numbers whose binary representation has number of 1's multiple of 3 for all numbers between 1 to N.

For ex — N = 19 then ans = 5 Since, 7, 11, 13, 14, 19 have number of 1's multiple of 3.

Constraint : n < 1016

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Test this code...

If it was correct and you have any question you can ask...

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

It can be solved using dp

dp[i][j][k] — number of numbers with i bits(possible with zeroes in front), j shows if we used in some 1 bit of n 0 in our number, k — number of 1 bits modulo 3

  • »
    »
    11 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks vintage_Vlad_Makeev, can you please give some more details on it ?

    • »
      »
      »
      11 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Let's understand how are numbers <=n arranged.

      At first they have some prefix of n. Then there is a position i that n[i]>x[i] and then can be anything.

      For example

      10010101 10001110

      100 — is common prefix of this numbers. Then in n we have 1 and in our number we have 0. And after this position we can do anything as this number will be less or equal than n.

      So we can do a dp with states (number of bits left,number of 1 bits modulo 3, is there a position i n[i]>x[i]). So there will be only 64*3*2 states.

      My Englist is terrible, sorry((