How to solve HDU 1014 Uniform Generator problem using gcd algorithm?