UVA10277 POJ2646 ZOJ1856 Boastin‘ Red Socks数学

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10277 POJ2646 ZOJ1856 Boastin‘ Red Socks数学相关的知识,希望对你有一定的参考价值。

Boastin’ Red Socks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2197 Accepted: 556

Description

You have a drawer that is full of two kinds of socks: red and black. You know that there are at least 2 socks, and not more than 50000. However, you do not know how many there actually are, nor do you know how many are red, or how many are black. (Your mother does the laundry!)
You have noticed, though, that when you reach into the drawer each morning and choose two socks to wear (in pitch darkness, so you cannot distinguish red from black), the probability that you pick two red socks is exactly p/q, where 0 < q and 0 <= p <= q.

From this, can you determine how many socks of each colour are in your drawer? There may be multiple solutions - if so, pick the solution with the fewest total number of socks.

Input

Input consists of multiple problems, each on a separate line. Each problem consists of the integers p and q separated by a single space. Note that p and q will both fit into an unsigned long integer.
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

1 2
6 8
12 2499550020
56 789
0 0

Sample Output

3 1
7 1
4 49992
impossible

Source

Waterloo local 1999.06.19

问题链接UVA848 Fmt
问题简述:(略)
问题分析:数学计算题,枚举解决问题,不解释。解题代码来自仙客传奇团队。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10277 POJ2646 ZOJ1856 Boastin' Red Socks */

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>

using namespace std;

typedef  long long LL;
const int N = 50000;

int main()

    LL p, q, n, m;
    while(~scanf("%lld%lld", &p, &q) && (q || q)) 
        if (p == 0) printf("0 2\\n");
        else if (p == q) printf("2 0\\n");
        else 
            LL tmp = __gcd(p , q);
            p /= tmp, q /= tmp;
            for (n = 2; n <= N; n++)
                if (n * (n - 1) % q == 0) 
                    tmp = (n * (n - 1) / q) * p;
                    m = sqrt(tmp);
                    if (m * (m + 1) == tmp && m + 1 >= 2)
                        break;
                
            if ( n <= N)
                printf("%lld %lld\\n", m + 1, n - (m + 1));
            else
                printf("impossible\\n");
        
    

    return 0;

以上是关于UVA10277 POJ2646 ZOJ1856 Boastin‘ Red Socks数学的主要内容,如果未能解决你的问题,请参考以下文章

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA10670 POJ1907 ZOJ2372 Work Reduction贪心

UVA10081 POJ2537 ZOJ1883 Tight WordsDP

POJ2689 ZOJ1842 UVA10140 Prime Distance筛选法

POJ2351 ZOJ1916 UVA10371 Time Zones时区计算