Precision problem(Bigdecimal)

How to solve the precision problems in questions.
When I am using Bigdecimal it gives Tle.
if I take double it gives wrong answer

anyone know how to solve such problem