Uva10277-boastin ' Red Socks (enumeration + dichotomy)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.