描述:红黑袜子,给出的是红袜子被选到的概率,即为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;
}

10277 - Boastin' Red Socks的更多相关文章

  1. UVA 10277 Boastin' Red Socks

    #include <iostream> #include<cstdio> #include<cstring> using namespace std; unsign ...

  2. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  3. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  4. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题

    A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...

  5. cf581A Vasya the Hipster

    One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red so ...

  6. Vasya the Hipster

    One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red so ...

  7. 使用Red Gate Sql Data Compare 数据库同步工具进行SQL Server的两个数据库的数据比较、同步

    Sql Data Compare 是比较两个数据库的数据是否相同.生成同步sql的工具. 这一款工具由Red Gate公司出品,我们熟悉的.NET Reflector就是这个公司推出的,它的SQLTo ...

  8. 使用Red Gate Sql Compare 数据库同步工具进行SQL Server的两个数据库的结构比较、同步

    将测试版的项目同步(部署)到正式版的时候,两个数据库的结构比较与同步时,如果修改数据库的时候没有记录好修改了那些表,很难将两个数据库进行同步 RedGate Sql Compare使用简介说明: 1. ...

  9. 新年抢红包效果(New Year Red Packet)

    新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...

随机推荐

  1. SQL代理执行EXE可执行程序

    原文:SQL代理执行EXE可执行程序  1.如果没有启用xp_cmdshell安全配置是不可以使用的-- 启用xp_cmdshellEXEC sp_configure 'xp_cmdshell', 1 ...

  2. JavaScript中,关于new的那些事

    这篇文章是自己对new学习过程中的一些理解,有不对的地方希望指出,接受组织的批评教育. 导火线,前段时间学习jQuery的时候,看到源码中有这样一段: jQuery = function(select ...

  3. LeetCode——Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. dom03

    鼠标事件: 键盘事件: //通过class获取元素,封装一个通过class获取元素的方法 //IE10以下不支持document.getElementByClass() function getByC ...

  5. 11g R2RAC Dynamic remastering

    In this post, I will demonstrate dynamic remastering of the resources in RAC . In RAC, every data bl ...

  6. 终于有人把O2O、C2C、B2B、B2C的区别讲透了!

    终于有人把O2O.C2C.B2B.B2C的区别讲透了! 一.O2O.C2C.B2B.B2C的区别在哪里? O2O是online to offline分为四种运营模式: 1.online to offl ...

  7. JavaScript语言基础知识8

    这篇文章是对前面学习的知识进行总结: 1.JavaScript支持多种数据类型,如数值类型.字符串类型.布尔类型等. 2.在JavaScript中,字符串是用引號括起来的字符系列,转义字符能够用来表示 ...

  8. 【MS SQL】查看任务执行进度

    原文:[MS SQL]查看任务执行进度 备份或还原数据库时,我一般是用MS SQL工具向导来进行,由于工具向导没有显示任务执行过程的状态, 如果数据库比较大执行时间较长的话,对任务啥时候执行完成比较迷 ...

  9. Kinect for Windows V2.0 新功能

    系统要求: win8  or  win8.1 硬件要求: 64位(x64)处理器 i7 2.5-GHz或更快的处理器 内置USB 3.0总线 4 GB RAM DX11图形适配器 外观: 第二代Kin ...

  10. Linux环境下搭建php开发环境的操作步骤

    本文主要记载了通过编译方式进行软件/开发环境的安装过程,其他安装方式忽略! 文章背景: 因为php和Apache等采用编译安装方式进行安装,然而编译安装方式,需要c,c++编译环境, 通过apt方式安 ...