[closed]WA in BEENUMS SPOJ

problem: http://www.spoj.com/problems/BEENUMS/

my solution: http://ideone.com/CvvsjO#stdin

If anyone could tell me the cause behind the WA,it would be really helpful.
Thanks in advance.

So basic idea is right. But you’ve got problem when you computing that formula.

First, you say, that beehive with 7, 8, 9 cells are valid. For 7 it’s correct, but not for others.
This is caused in line

long hold=n/3;

You want to use double variable, because you use integer division.

Second mistake, when you divide by some number and want double result, you must divide by double. And number 3 is not double but integer. But 3.0 is double.

So correct code should looks like:

double hold=n/3.0;
double ans=(Math.sqrt(1+4*hold)-1)/2.0;

Now you will be rewarded by Accepted :slight_smile:

1 Like

made the corrections,got AC! thanks a lot! :smiley:
but couldn’t understand what you meant by this line: “you say, that beehive with 7, 8, 9 cells are valid.”
i don’t understand how the previous line (long hold=n/3;) accepted 8 and 9 as valid numbers. Could you please elaborate?

Well. Take n=7. You substract 1 and hold=6/3=2. Than you compute (sqrt(1+4*hold)-1)/2 and get ans=1. But when you’ve got n=8, than hold=7/3 but that is still 2, because you using integer not double arithmetic. So you again get ans=1. Similary with n=9. And when you’ve got n=10, than hold=9/3=3 and ans=1,302775638 what not integer number and you correctly say ‘N’. Do you understand now?

Main difference a/b with integer numbers is floor(a/b) with doubles.

1 Like

yes now got it! thanks for the explanation :slight_smile: