s=$3{for(y=$1;(x=y*$2+1)%s||$1<x/=s;y--);print x,y}
1 1 1
1 0
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!