UVA 10277 Boastin' Red Socks

题意:已知p/q是连续取的两次红袜子的概率,因为p/q=a/b(a-1)/(b-1),枚举,排除是double型时的可能就可以了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

long  gcd(long long a,long long b){
    return b==0?a:gcd(b,a%b);
}

int main(){
    long long p,q,i;
    while (scanf("%lld%lld",&p,&q) != EOF && p+q){
        if (p == 0){
            printf("0 2\n");
            continue;
        }
        long long d = gcd(p,q);
        p /= d;
        q /= d;
        for (i = 2; i <= 50000; i++){
            if (i*(i-1) % q == 0){
                long long t = i*(i-1) / q;
                long long s = (long long)sqrt((double)t*p);
                if (s*(s+1) == t*p){
                    printf("%lld %lld\n",s+1,i-s-1);
                    break;
                }
            }
        }
        if (i > 50000)
            printf("impossible\n");
    }
    return 0;
}



你可能感兴趣的:(UVA 10277 Boastin' Red Socks)