I thought 215B - Olympic Medal was an easy problem, I got the relation fairly easily, but then it gave me WA at test 10.
My first submission was:
double r2=sqrt( ((double)((r1*r1)*(b)*(p1)))/(a*p2+b*p1));
Where r1 and p1 are the maximum possible and p2 is the minimum possible, based on that relation, where its directly proportional with r1 and p2 and inversly proportiona with p2.
So I thought I may need long double, re-submitted with long double, WA again.
Then I thought the std sqrt function was not accurate enough, coded my own sqrt function.
Long story short, I made 11 WAs all trying to figure out whats wrong, until in the end I used the inverse of the relation, and voila AC from the first time !
long double r2=(1.0/(r1*r1))*(((a*p2)/(b*p1))+1);
r2=pow(r2,-0.5);
But it was totally useless..the 11 WAs made my rank be 877, the lowest I ever got (so far :/)
I just hate those double precision problems...
Edit: Thanks to riadwaw, I knew what was the mistake. 5000^4 > INT_MAX, I should have kept the numerator as double to avoid the overflow. I totally admit it was my mistake for overlooking the constraints. Hopefully it would be another trick which no one would fall into again :)
Full text and comments »