Description: red/Black so, indicating the probability of a red so being selected, that is, P/Q. The number of red so and black so before a red so is selected is calculated, assume that the number of red so is n and the number of black so is m, then n (n-1)/(m-1) = p/Q, find the number of red and black so # include <cstdio> # include <cmath> # define ll long longll gcd (ll x, ll y) {If (X % Y = 0) return y; else return gcd (Y, X % Y);} int main () {// freopen ("in.txt", "r", stdin); LL n, m; while (scanf ("% LLD", & N, & M )! = EOF) {If (! N &&! M) break; If (! N) {printf ("0 2 \ n"); continue;} else if (n = m) {printf ("2 0 \ n"); continue ;} ll p, q, V, X; X = gcd (m, n); // printf ("x = % LLD", X ); // printf ("n = % LLD", n); n = N/X; M = m/X; P = SQRT (2.0 * m + 0.5 ); // If (P % 2 = 1) + + P; // printf ("P = % LLD \ n", P ); // printf ("m = % LLD \ n", m); bool flag = 0; For (; P <= 50000; ++ P) {If (P * (p-1) % m! = 0) continue; q = SQRT (p * (p-1) * N/m + 0.5); X = gcd (p * (PM ), Q * (q + 1); If (p * (PM)/x = M & Q * (q + 1)/x = N) {flag = 1; break; }}if (FLAG) {printf ("% LLD", q + 1); printf ("% LLD \ n", p-q-1 );} else puts ("impossible"); // puts ("");} return 0 ;}