For a problem on Codechef - https://www.codechef.com/problems/CHFING,
I submitted two programs that differs only on a part where a division occurs.
1st Solution https://www.codechef.com/viewsolution/24978403
import math MOD = 1000000007 test = int(input()) for _ in range(test): n,k = map(int, input().split()) first=k-1; diff=n-1; t = (first+diff)//(diff) y=(2*(first)-(t-1)*(diff)) ans = (y*t)//2; ans = ans%MOD print(ans)
2nd Solution https://www.codechef.com/viewsolution/24978385
import math MOD = 1000000007 test = int(input()) for _ in range(test): n,k = map(int, input().split()) first=k-1; diff=n-1; t = int((first+diff)/(diff)) y=(2*(first)-(t-1)*(diff)) ans = (y*t)//2; ans = ans%MOD print(ans)
solution #1 passed all test cases but solution #2 fails for one test case.
The only difference is on line #8 where t is calculated as
t = (first+diff)//(diff) in the 1st solution and
t = int((first+diff)/(diff)) in the 2nd solution.
Note that inputs are always integer, so variables
diff are ints. And the code is in Python 3.6.
So does python returns different for x//y and int(x/y)? Or am I missing something else? Can someone kindly point me out why the second solution gives WA?
Edit: I came to know that these two are different for negative numbers. (https://stackoverflow.com/questions/56816677/does-x-y-and-intx-y-return-same-results-in-python). But input for this problem is non-negative and hence the variables first and diff also.
Edit #2: https://stackoverflow.com/a/56569910/7845063
the division operator behaves abnormally generally for numbers greater than 10 ^ 17.
x = 10000000000000000000006 if x / 2 == x // 2: print("Hello") else: print("World")
For the above code, World will be printed and not Hello. This is because 10000000000000000000006 / 2 will return 5e + 21, but 10000000000000000000006 // 2 will return the correct answer 5000000000000000000003. Even int(10000000000000000000006 / 2) will return 5000000000000000000000, which is incorrect.