UVa 10277 boastin ' Red Socks (enum)

Source: Internet
Author: User
Tags gcd time limit

10277-boastin ' Red Socks

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=1218

You have a drawer this is all two kinds of socks:red and black. You are know that there are in least 2 socks, and not more than 50000. However, you don't know how many there actually are,

Nor do your know how many are red, or to many are black. (Your mother does the laundry!)

You have noticed, though, which is the drawer each morning and choose two to socks (in wear pitch Cannot distinguish red from black), the probability

Pick two red socks is exactlyp/Q, where 0 < Q and 0 <= p <= Q.

From this, can your determine how many socks of each colour are in your drawer? There May is multiple solutions-if so pick the solution with the fewest total number of socks, but still allowing

You have to wear a couple of same color socks.

Input

Input consists of multiple problems, each on a separate line. Each problem consists of the integersp andQ separated by a. Note thatP and Q would both fit into a unsigned long integer.

The Input is terminated by a line consisting of two zeroes.

Output

For each problem, output a single line consisting of the number of red socks and the number of black socks in your drawer, separated by one space. If There is no solution to the problem, print "impossible".

Sample Input

See more highlights of this column: http://www.bianceng.cn/Programming/sjjg/

1 2
6 8
2499550020
789
0 0

Sample Output

3 1
7 1
4 49992
Impossible

Train of thought: Set red socks have x only, black sock has y only, then have

C (x,2)/C (x+y,2) =x (x-1)/((X+y) (x+y-1)) =p/q

So enumerate the denominator I to see if it satisfies q| (i* (I-1)), and then see if the corresponding p can be split into j* (j-1) Form

PS: This problem can be translated into a two-yuan two-time Diophantine equation

[(Q-p) (2x-1)-P (2y)]^2-PQ (2y) ^2 = (q-p) ^2, when the q-p!=1 is not necessarily a solution, more specific analysis? Pit

Full code: (Run 0ms because of a UVA server failure at submission time (commonly known as Aunt))

 01./*0.000s*/02.  
#include <cstdio> #include <cmath> 05.typedef unsigned int ll;  
06.const ll maxn = 50000; 08.ll gcd (ll A, ll b) 09.  
{A. return b gcd (b, a% B): A;  
11.} 12. 13.int Main () 14.  
{ll p, q, G, I, pp;  
Double J;    while (scanf ("%u%u", &p, &q), Q) 18.        {. if (P = = 0)///probability is 0 20.            {puts ("0 2");///at least 2 socks 22.  
Continue        23.} 24.  
g = gcd (p, q);  
P/= G, Q/= g;            for (i = 2; I <= MAXN, ++i)///not more than 50000 27.            if (i * (i-1)% q = = 0)///The denominator 28 of the condition.  
{pp = i * (i-1)/q * p;  
j = sqrt (0.25 + pp) + 0.5;                if (round (j) = j) The///molecule also meets the condition 32.  
{A. printf ("%u%u\n", (LL) J, I-(LL) j);  
break;       35.} 36.     } 37.  
if (i > Maxn) puts ("impossible");    38.} 39.  
return 0; 
.}

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.