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;
}

浙公网安备 33010602011771号