Back to General discussions forum
This problem requires calculation of the expectation number of moves in a stone moving game. A good algorithm is needed to ensure the required degree of accuracy.
A good algorithm is needed to ensure the required degree of accuracy.
That's an understatement. Requesting 10 digits after the decimal point pushes float64 to its absolute limits.
I struggled with a tiny round-off error manifesting as a 1-digit shift in the 10th decimal place.
Agreed, the current check is far from ideal,
and the expected answer can also include some level of inaccuracy itself.
On other sites, a criteria like the following is often used: if x is your answer and y is the expected answer,
x will be accepted if abs(x-y) / max(1,y) <= 10^-9.
On reflection I should probably have asked for 9 digits. However, I was trying to set a challenge regarding the accuracy. Three people have solved the problem so far so it clearly can be done. The reference to some inaccuracy in the checker is not correct. I was very careful to have the checker gernerate a value which is far more accurate than the 10 digits requested. The checker code works with 28 digits so should have an accuracy well in excess of 10 digits.
From python.org: "Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine."
An interesting read on this topic: Examples of floating point problems.
In the end, I used mpmath, a floating-point arithmetic library with arbitrary precision. And as I wrote, other sites (CodeForces!) use the aforementioned approach to judge a solution for those reasons.