Uva10277-boastin ' Red Socks (enumeration + dichotomy)
Topic links
The main idea: now there are m red socks, n black socks, so total socks totals = n + M; now give you P, q, OK N and M, make two from these socks is the probability of red socks equals p/q; if there is no such n and M to meet the requirements of the output impossible;
Problem-solving ideas: M*(m-1)/(Total*(TOTAL-1)) = p/q; Then we just need to enumerate total, and we can solve the M. But there will be precision error, there seems to be a solution, but feel unable to understand. After looking at another type of problem, two m, because M > 1 is monotonically increasing. So as long as the special case is disposed of M = 0, that is, p = 0 is the case.
Code:
#include <cstdio>#include <cstring>#include <cmath>typedef Long LongLl;ll gcd (ll A, ll b) {return(b = =0) ? A:GCD (b, a%b);}intMain () {ll p, q, ans, p, q;BOOLFlag while(scanf("%lld%lld", &p, &q)! = EOF && (P | | Q)) {flag =0;if(P = =0) {printf("0 2\n");Continue; }if(P = = Q) {printf("2 0\n");Continue; } ans = gcd (P, Q); P/= ans; Q/= ans; ll M, total;intL, R; for(total =3; Total <=50000; total++) {ll N = total * (Total-1) * P, M;if(N% Q! =0)Continue; N/= Q; L =1; R = Total; while(L < R) {m = (L + r)/2; m = m * (M-1);if(M < N) L = M +1;Else if(M > N) r = M;Else{flag =1; Break; } }if(flag) Break; }if(flag)printf("%lld%lld\n", M, total-m);Else printf("impossible\n"); }return 0;}
Uva10277-boastin ' Red Socks (enumeration + dichotomy)