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

Автор 650iq, история, 2 года назад, По-английски

You are given an integer x which is initially 1. In one move You can perform either of the 2 operations — make x = x-1 or x=2*x. You need to perform exactly K such operations. Find if it is possible to make number N from X. Note N and K is very high upto 10^18.

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

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

The question can be rephrased as transforming x to 1 with operations x/=2 and x++
For this we will choose first operation if x is even, second otherwise and we will reach 1 in atmost 128 operations
This looks correct but i can't prove whether this requires the min operations or not

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

I think this problem is in the problem set.

We can approach it in reverse -> try to make 1 from x. This is valid since if we can make x from 1 then reverse should be possible.

so in reverse following two operations are possible: 1) divide x by 2 (if even) 2) increment x by 1

so keep on dividing x by 2 until it is divisible by 2. else if it becomes odd then increment. keep on counting the operations.

--> NOTE: this gives you minimum number of operations (say N). if we can reach in N oper. then we can reach in any operations >= N. in start we can simply waste excess operations by 1*2 = 2 , then 2-1 = 1.

then at last compare the no. of operations taken with k and print the answer.