10277 - Boastin' Red Socks

描述:红黑袜子,给出的是红袜子被选到的概率,即为p/q,要计算的是在挑选出一对红袜子之前的红袜子和黑袜子的数目,假设红袜子数为n,黑袜子数为m,那么n(n-1)/(m(m-1))=p/q,求出红袜子数和黑袜子数即可
#include<cstdio>
#include <cmath>
#define LL long long
LL gcd(LL x,LL y)
{
    if(x%y==0) return y;
    else return gcd(y,x%y);
}
int main()
{
 //   freopen("in.txt","r",stdin);
    LL n,m;
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        if(!n&&!m) break;
        if(!n)
        {
            printf("0 2\n");
            continue;
        }
        else if(n==m)
        {
            printf("2 0\n");
            continue;
        }
        LL p,q,v,x;
        x=gcd(m,n);
      //  printf("x=%lld ",x);
      //  printf("n=%lld ",n);
        n=n/x;
        m=m/x;
        p=sqrt(2.0*m+0.5);
       // if(p%2==1) ++p;
      //  printf("p=%lld\n",p);
        //    printf("m=%lld\n",m);
        bool flag=0;
        for( ; p<=50000 ; ++p)
        {
            if((p*(p-1))%m!=0) continue;
            q=sqrt(p*(p-1)*n/m+0.5);
            x=gcd(p*(p-1),q*(q+1));
            if(p*(p-1)/x==m&&q*(q+1)/x==n)
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            printf("%lld ",q+1);
            printf("%lld\n",p-q-1);
        }
        else puts("impossible");
        //puts("");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值