Tom and Jerry Game Solution

Here is my explanation for the Tom and Jerry question asked in Codechef June Long Challenge:
tutorial link:

4 Likes

Even I had made the exact same logic and submitted my code during the contest but I got a Wrong Answer Error.
Below given is my Python code:

test_case = int(input())

for i in range(test_case):
    tom_strength = int(input())
    
    while tom_strength % 2 == 0:     
        tom_strength = int(tom_strength / 2)
        
    print(tom_strength // 2)

Is there anything wrong that I’m doing, if not then why my code got a Wrong Answer error and your solution worked completely fine?

sorry bro, i don’t know anything about python,but still i will try to debug it,and if find a bug then i will surely reply you

1 Like

at first look i think that you are not using if else condition for checking whether tom_strength is odd or even

1 Like

Change tom_strength = int(tom_strength/2) to tom_strength = tom_strength // 2.

@anon53253486
Both will yield the same result.
-> int(3 / 2) >> 1
-> 3 // 2 >> 1
Can you please explain what difference this change will make?

And sorry I forget to mention that I’ve passed the SubTask #1 and got a Wrong Answer error for SubTask #2.

@moh1t I think you are missing some cases.Have a look at my solution.Check the solve function.It was pattern problem i think and taking care of a long answer.
https://www.codechef.com/viewsolution/33870294

Try it once. I’ve been using python for a while, I know how stuff works.

1 Like

i am a c++ user but but may be you are wrong because you are not considering tom_strength odd or even separately

If you are still not convinced, here is the AC solution which I told you to do.

@anon53253486 is correct. Look at my code in python(3 lines) : CodeChef: Practical coding for everyone

int() gives precision errors. That is the only issue.

@kp99
We don’t need the if condition here to check the number is even or not. We will get inside while loop only when the number is even. If the number is odd then simply we will print floor(number / 2).

@anon53253486
I tried the changes that you’ve suggested. My code worked completely fine and I’ve passed all the test cases.
Please explain why this is happening?

int() gives precision errors. Try to use // rather than int().

1 Like

When you use /, it performs a float division and floats in python are only precise upto 16 digits. However, the constraints for the second task mean that you would need 18 digits of precision.
That is why it is better to use // instead of /.

1 Like

Ya , your code don’t need if else condition ,it seems to be a python language specific issue, but still if there is a confusion please read the explanation below:
In favour of victory of jerry (as mentioned in the question statement , that, in order to win the game by jerry, TS must be odd and JS must be even), so,
for an odd value of TS (say TS=9) jerry can take any even value from 1 to 9 (i.e., 2,4,6,8)
so answer becomes 4 (i.e. (int)9/2)
On the other hand for all even values of TS,
we have to reduce both JS(even values only) and TS to their lowest term and then check for the conditions stated in the question.

@anon53253486 @anon11076894 Thank You for teaching me this great concept.
@kp99 Thank You for the video and for your help towards finding the error even when you were not familiar with python, appreciate your efforts.

as has been said in various topics…
int(tom_strength/2) gives precision errors. So use tom_strength//2 instead

also avoid repetitive threads/topics

yeah you need to consider that case separately when tom_strength is even . if tom_strength is an odd number tom_strength // 2 is the correct answer , but if tom_strength is even , this wont give you the correct answer , Let’s say tom_strength is 24 , then possible solutions for jerry would be only one , i.e , 16 , so you need to find that . You can approach this by finding the number of two’s, say x , that tom_strength has in its factorization . then add 1 to it . so you have x+1 two’s . let value = pow(2 , x+1 ) . This is the minimum number that jerry can have . Now you need to find all those numbers which are multiple of value and still less than tom_strength . you can do so by tom_strength//value . This will give you the count of numbers where jerry can win the game .
Yeah I hope i helpled . This couldn’t be the optimal solution , but this is my approach . and I love jerry .