uva 10277 Boastin' Red Socks
来源:互联网 发布:tmt 知乎 编辑:程序博客网 时间:2024/05/17 06:08
原题:
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 ≤ p ≤ q. 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, but still allowing you to wear a couple of same color 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
大意:
给你两个数p/q表示在一堆红色和白色袜子中取出两只袜子为为红色袜子的概率,告诉你总袜子数不超过50000,现在问你红袜子和黑袜子有多少个?如果答案有多个,找出总数最小的那个。
#include <bits/stdc++.h>using namespace std;//fstream in,out;long long q,p;long long gcd(long long a,long long b){ return b==0 ? a:gcd(b,a%b);}bool IsSqure(long long a){ long long tmp=sqrt(a); return tmp*tmp==a;}int main(){ ios::sync_with_stdio(false); long long tot,red,tmp; while(cin>>p>>q,q+p) { if(p==0) { cout<<"0 2"<<endl; continue; } if(p==q) { cout<<"2 0"<<endl; continue; } long long g=gcd(p,q); q/=g; p/=g; bool flag=0; for(long long tot=2;tot<=50000;++tot) { if((tot*(tot-1))%q==0) { tmp=tot*(tot-1)/q; if(IsSqure(4*p*tmp+1)) { tmp=sqrt(4*p*tmp+1); if((1+tmp)%2==0) { red=(1+tmp)/2; cout<<red<<" "<<tot-red<<endl; flag=1; break; } } } } if(!flag) cout<<"impossible"<<endl; } return 0;}
解答:
这题应该不能算是数学题吧,首先枚举总数,然后根据方程判断能否整除和开根号之类的,如果都满足就输出结果就可以了。
- uva 10277 Boastin' Red Socks
- UVA 10277 Boastin' Red Socks
- uva 10277 Boastin' Red Socks
- UVA 10277 Boastin' Red Socks
- Boastin' Red Socks - UVa 10277
- uva 10277 Boastin' Red Socks
- uva 10277 - Boastin' Red Socks(概率)
- UVa 10277 Boastin' Red Socks (枚举)
- UVa 10277 - Boastin' Red Socks (枚举)
- 10277 - Boastin' Red Socks
- 10277 Boastin' Red Socks(概率 暴力枚举)
- Boastin' Red Socks
- poj 2645-Boastin' Red Socks-算组合
- UVA10277 - Boastin' Red Socks(枚举+二分)
- uva10277 - Boastin' Red Socks(炫耀红白袜子)
- POJ 2645 Boastin' Red Socks(组合数学)
- socks
- SOCKs
- 树的最大独立集——树形dp
- 关于APUE第十六章的客户端程序返回Servname not supported for ai_socktype错误的解决方法
- matlab练习程序(非负矩阵分解)
- 树的重心——树形dp
- 矩阵分解的推荐算法入门-好好看
- uva 10277 Boastin' Red Socks
- 按首字母分类并排序显示的列表(二)--给BaseExpandableListAdapter提供数据的HashList
- 【NYOJ】[268]荷兰国旗问题
- 怎样的学术导师是好导师(Nature)
- 【Leetcode】House Robber II
- 51Nod 1013 3的幂的和(快速幂+逆元)
- 81. Search in Rotated Sorted Array II
- 俊哥的HADOOP之路
- iOS中的两种搜索方式UISearchDisplayController和UISearchController