here is the problem link :
https://www.spoj.pl/problems/ROOTCIPH/
what is your idea
hm.....
try this if helps you : x^3+ax^2+bx+c=0 - Wolfram|Alpha
and here you are how to do cubic polynomial : Solving Cubic Equations
hm.... or because your are writing code for that, maybe it would be easier to test first zero, and than simplify cubic polynomial... as i recall you can use algorithm hm... can't remember right now but as soon as i do i'll write it here (long time passed since i have done any programming )