TOJ-1324 Boastin' Red Socks

You have a drawer that is full of two kinds of socks: red and black. You know that there are at least 2 socks, and not more than 50000. However, you do not know how many there actually are, nor do you know how many are red, or how many are black. (Your mother does the laundry!)

You have noticed, though, that when you reach into the drawer each morning and choose two socks to wear (in pitch darkness, so you cannot distinguish red from black), the probability that you pick two red socks is exactly p/q, where 0 < q and 0 ≤ pq.

From this, can you determine how many socks of each colour are in your drawer? There may be multiple solutions - if so, pick the solution with the fewest total number of socks.

Input

Input consists of multiple problems, each on a separate line. Each problem consists of the integers p and q separated by a single space. Note that p and q will both fit into an unsigned long integer.

Input is terminated by a line consisting of two zeroes.

Output

For each problem, output a single line consisting of the number of red socks and the number of black socks in your drawer, separated by one space. If there is no solution to the problem, print "impossible".

Sample Input

1 2
6 8
12 2499550020
56 789
0 0

Sample Output

3 1
7 1
4 49992
impossible

Source: Waterloo
Local Contest Jun. 19, 1999

设红色袜子数为r,总共为t,黑色袜子数即为t-r。由题意:

r(r-1) / t(t-1) = p/q。

#include <iostream>
#include <math.h>
using namespace std;
long long gcd(long long a,long long b){
    return b==0 ? a : gcd(b,a%b);
}
int main(){
    long long p,q,g,r,t,h,d;
    while(cin>>p>>q,p,q){
        if(p == 0){
            cout<<0<<" "<<2<<endl;
            continue;
        }
        g = gcd(p,q);cout<<g<<endl;
        p /= g;
        q /= g;//让pq互质
        bool f = false;
        for(t=2;t<=50000;t++){//枚举总袜子数t
            if(t*(t-1)%q==0){//可行的 t值
                 h = t*(t-1)/q*p;
                 d = sqrt(4*h+1);//求根公式
                 if(d*d!=4*h+1) continue;
                 if((d+1)%2==0){
                     r=(d+1)/2;
                     cout<<r<<" "<<t-r<<endl;
                     f = true;
                     break;
                 }
            }

        }
        if(!f) cout<<"impossible"<<endl;
    }
    return 0;
}

奇怪的地方是,用unsigned long 得到WA,可能是涉及相乘位数不够吧。

TOJ-1324 Boastin' Red Socks

时间: 2024-08-09 06:20:22

TOJ-1324 Boastin' Red Socks的相关文章

UVA10277 - Boastin&#39; Red Socks(枚举+二分)

UVA10277 - Boastin' Red Socks(枚举+二分) 题目链接 题目大意:现在有m只红袜子,n只黑袜子,这样总袜子total = n + m;现在给你p, q,确定n和m,使得从这些袜子中取两只都是红袜子的概率等于p/q:如果没有这样的n和m满足要求输出impossible; 解题思路:m *(m - 1) / (total * (total - 1)) = p /q; 那么我们只需要枚举total,就可以解到m.但是会有精度误差,貌似有解决的办法,但是觉得没法理解.后面看了

UVA 10277 Boastin&#39; Red Socks

1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 unsigned int r,sum,p,q; 8 unsigned int st[50010][2]; 9 10 unsigned gcd(unsigned a,unsigned b) 11 { 12 return b?gcd(b,a%b):a; 13 } 14 /*哈希链表*/ 15 co

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

2017-5-7-Train: Codeforces Round #322 (Div. 2)

A. Vasya the Hipster(水题) One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red socks and b blue socks. According to the latest fashion, hipsters should wear the socks of different colors: a red one on the l

CentOS 7 安装 【shadow+socks】

最近,寡妇王的封锁越来越厉害,前阵子连Gmail都给全面封锁.实在令人气愤. 这里记载一篇搭建梯子的文章,以此表明决心. 首先购买一台国外的vps主机,土豪机有Linode.DigitalOcean,屌丝有bandwagonhost.再不济的话 去haphost弄个免费的vps,不过人品不好弄不到.我当然是属于人品好的那种了,哈哈哈哈,进入正题 啰嗦下,最好买到后重装下系统,推荐centos6, 然后 ssh连上vps后,先下载必备的几种开发工具,因为要在vps上编译安装[shadow+sock

Linux red hat 安装ansible

今日对Linux 系统是Red Hat Enterprise Linux Server release 6.5 (Santiago)对ansible进行安装. 由于系统的源为yum源,所以使用yum install ansible 进行安装,但是报错.如图.(这个错误是yum源没有注册到red hat 系统). yum源不能安装,所以换了一个思路.使用pip安装.pip是依赖python安装的. 1.检查Python版本 Python -v 检查出来为Python 2.6.6 2.检查pip 版

red hat7 系统可以ping通ip地址但是不能ping通域名

在red hat7中ifconfig后出现这样的情况,ens33是物理网卡,与eth0一样只是不同的名字.但是只能ping通ip地址不能ping通域名. 解决方法: 在文件 /etc/resolv.conf文件下更改nameserver的值 加上 nameserver 8.8.8.8  域名解析服务  nameserver 8.8.4.4为备用的 在文件 etc/sysconfig/network-scripts下修改 ifcfg-eth0 或者 ifcfg-ens33文件,设置静态ip地址,

red halt 7.3使用Cent-os7 yum 源

困惑:在使用red halt学习时,未注册的用户,有些软件是无法通过yum网络源和本地光盘下载,此时使用Centos的yum源就显得很方便,因为红帽收购了Centos 所以软件的兼容性基本不会有什么问题 1.检查是否安装yum包 查看RHEL是否安装了yum,若是安装了,那么又有哪些yum包: [[email protected] yum.repos.d]# rpm -qa |grep yumyum-3.4.3-118.el7.noarchyum-utils-1.1.31-24.el7.noar