WHY I AM GETTING THIS ERROR ... ? SIGFPE Error ...?

Two Numbers

Problem Code: TWONMS

https://www.codechef.com/viewsolution/42978722

This is my solution…i think we get SIGFPE error when we try to divide something by 0 …

Optimise your code with the following two cases:

  1. If N is even , then both the players will have equal number of turns i.e. N/2. Then,

C= A x 2^(N/2)
D=Bx 2^(N/2)

When you divide them, powers of 2 get cancelled out. To get integer division, if A>B then print A/B else B/A

  1. When N is odd, observe that A plays one turn more than B. Let B plays q turns.

Thus, q+1+q=N
Or, q=(N-1)/2

After N turns, C=Ax2^(q+1) and D=Bx2^q

When you divide them, C = Ax2 and D=B.
For integer division, if C>D print C/D else D/C.

1 Like

Yes SIGFPE Does occurs when you try to divide the numbers by ZERO or NAN(Not a Number)

  • Long Long has 64 bits and in your implementation you are using left-shifting of bit to multiply by 2 but if you will shift more than 64 times it eventually becomes zero… hence the final C or D are zero… thus you get the error …
  • You don’t need to actually find C and D… proof is really simple
if(n is even)
    cout << max(A,B)/min(A,B)<<endl;
else
    cout << max(2*A,B)/min(2*A,B)<<endl;  
1 Like

thanks a lot.

thank you.