描述:红黑袜子,给出的是红袜子被选到的概率,即为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. 电脑报2014年第43期 pdf高清版

    电脑报2014年第43期 pdf高清版

  2. 如此高效通用的分页存储过程是带有sql注入漏洞的

    原文:如此高效通用的分页存储过程是带有sql注入漏洞的 在google中搜索“分页存储过程”会出来好多结果,是大家常用的分页存储过程,今天我却要说它是有漏洞的,而且漏洞无法通过修改存储过程进行补救,如 ...

  3. 在 CentOS 上安装 Tomcat7

    1. 下载 #可以直接在官网下载然后传到服务器上,也可以直接下载#下载地址:http://tomcat.apache.org/download-70.cgi 2. 安装 # tar -xzvf apa ...

  4. centos安装zabbix集群监控(亲测无坑版)

    一. 安装lemp环境 下载安装包:wget bbs.linuxtone.org/docs/autoinstall/lemp_auto_v1.0.6.tar.gz 包解压:tar zxvf lemp_ ...

  5. HDU 1983 BFS&amp;&amp;DFS

    大多数刚需封锁4区域可以,DFS地区封锁.BFS无论是通过 #include "stdio.h" #include "string.h" #include &q ...

  6. Warning: Cannot modify header information - headers already sent by (output started at

    一般来说在header函数前不能输出html内容,类似的还有setcookie() 和 session 函数,这些函数需要在输出流中增加消息头部信息.如果在header()执行之前有echo等语句,当 ...

  7. 简洁vim配置方案Janus(1)

    最近不想在编辑器上花太多的精力,所以找到个不错的解决方案. 在不懂vim配置的前提下也能用的很开心. 1,下载安装Janus(https://github.com/carlhuda/janus) 安装 ...

  8. Asterisk 未来之路3.0_0003

    原文:Asterisk 未来之路3.0_0003 Asterisk: The Hacker's PBX 如果电信公司忽视了asterisk,那么正在处于危险中.asterisk 良好的扩展性,能够创建 ...

  9. javascript 学习总结(七)String对象

    1.string对象中可以传正则的函数介绍 /* match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastIndexOf(),但是 ...

  10. Nexus入门指南(图文)

    Nexus入门指南(图文) 博客分类: Maven JavamavenGoogleApacheTomcat Nexus介绍 Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven ...