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

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

Can anyone please help me with this Codechef August Long Challenge 2018 problem QUESTION why my solution is giving Wrong Answer and for which test case it is failing and why..... solution link Solution It will be a great help for me....thank you.

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

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

it is a difficult to say why you have wrong answer ,but the main idea to solve this problem is different. You must precalculate this numbers . The size of this numbers is not big. Maximum 31*31. You can add this numbers to vector and then sort it. After that ,with binary search you must to take three numbers and then print minimum difference.

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

    Even binary search is not required, linear search would work according to constraints.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      Definitely, linear search would work here. But it's no harm to use optimisations.In the worst case (my favourite Big Oh :p), it only helps you discover something new eventually, which one never knows, might help in the future.

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

        It is good for learning but sometimes constraints make problem and code more simple :P

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

      Yeah, but those 105 upper limit of constraint was a headache. I too went with the binary search for it.

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

      yeah ,really ))) i forgot about that,when i was solving a problem.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Your code doesn't work for the these numbers 15 63 127 255 511 have you got the pattern? These are one less than the power of two and greater than 7. Your code returns 1 but the actual answer is 2.

»
6 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Bro, just an advice. Don't go with so much complex logics. It never helps but just increases chances of getting WA as evident in your case. Even I suffered a lot because of the same habit. So try to think easy.

Coming to the code, I'll think on this and let you know if I find the mistake.

Till then. What I thought of the solution to the problem is that till 10^9, there will be at most 450 such numbers which are the sum of two numbers (different powers of 2). And note the optimal number can even exceed 10^9. So for safety take till 2*10^9. And then apply binary search to find the closest such number or the optimal number and accordingly calculate the answer.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

There is really no need for such complex code or binary search. You can just iterate through the values of 2^x + 2^y for (x != y), then you simply take the minimum difference between n and the computed sum. It should run in O(30*30).

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

    Nice, I went with binary search during the contest. I like your approach greatly. Tell me if it worked, because if I ever thought of that I would have died thinking if something near 10^8 calculation will throw TLE or not.