UVA 10277 Boastin' Red Socks
#include <iostream>
#include<cstdio>
#include<cstring> using namespace std; unsigned int r,sum,p,q;
unsigned int st[][]; unsigned gcd(unsigned a,unsigned b)
{
return b?gcd(b,a%b):a;
}
/*哈希链表*/
const int HASH=;
int head[HASH],next1[HASH];
int hash1(unsigned int &s)
{
return s%HASH;
}
int insert(int s)
{
int h=hash1(st[s][]);
int u=head[h];
while(u)//链表尾一直是0,所以s不能为0
u=next1[u];
next1[s]=head[h];//原来的链表头成为s的next
head[h]=s;//s成为head[h]的链表头
return ;
}
int find(unsigned int s)
{
int h=hash1(s);
int u=head[h];
while(u)
{
if(st[u][]==s) return u;
u=next1[u];
}
return ;
}
int main()
{
//freopen("/home/user/桌面/in","r",stdin);
while(scanf("%u%u",&p,&q)==&&(p||q))
{
memset(head,,sizeof(head));
unsigned int i=,x,j=;
if(p==)
{
printf("%d %d\n",,);
continue;
}
x=gcd(p,q);
p/=x;q/=x;
for(;i<=;i++)
{
unsigned int t=i*i-i;
if(t%p==)
{
st[j][]=t/p;
st[j][]=i;
insert(j);
j++;
}
if(t%q==&&(x=find(t/q)))
{
printf("%u %u\n",st[x][],i-st[x][]);
break;
}
}
if(i>) puts("impossible");
}
return ;
}
UVA 10277 Boastin' Red Socks的更多相关文章
- 10277 - Boastin' Red Socks
描述:红黑袜子,给出的是红袜子被选到的概率,即为p/q,要计算的是在挑选出一对红袜子之前的红袜子和黑袜子的数目,假设红袜子数为n,黑袜子数为m,那么n(n-1)/(m(m-1))=p/q,求出红袜子数 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 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 ...
- 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 ...
- 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 ...
- uva 725 Division(暴力模拟)
Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...
- UVA 253 Cube painting(暴力打表)
Cube painting Problem Description: We have a machine for painting cubes. It is supplied with three d ...
- UVa 11729 - Commando War(贪心)
"Waiting for orders we held in the wood, word from the front never came By evening the sound of ...
随机推荐
- s=a+aa+aaa+aaaa+aa...aaaa
main(){ int a,n,count=1; long int sn=0,tn=0; cout<<"input a and n:"; cin>>a> ...
- Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
在mac上面安装mysql之后,输入mysql一直报错,可以通过下面的方法解决. mysqld stop mysql.server start http://stackoverflow.com/q ...
- Python之深浅拷贝&函数
一.深浅拷贝 深浅拷贝是指copy模块下的copy()和deepcopy()方法. 1.浅拷贝 示例: >>> import copy >>> a = 'hello ...
- NOIP2001-普及组复赛-第一题-数的计算
题目描述 Description 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n<=1000),然后对此自然数按照如下方法进行处理: 1.不作任何处理; 2.在 ...
- robotium和appium的一些区别
Appium是基于UIAutomator框架实现的.Appium测试进程与目标应用进程是分开的,所以Appium不能直接访问目标应用的各种element属性进行copy&paste,而只能模拟 ...
- python--tile函数
1.函数的定义与说明 tile函数位于python模块 numpy.lib.shape_base中,功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组. 2.函数操作 ...
- 正则表达式 替换 <img > 标签
/** * 正则表达式过滤<img > 标签 * @param str * @return */ public static String cutOutImgPrefix(String s ...
- Quartz(任务调度)- 入门学习
参照:http://blog.csdn.net/szwangdf/article/details/6158646 1.自定义定时任务管理类 QuartzManager 参照大神基础上新增:1.添加jo ...
- Python学习之旅--第一周--初识Python
一:Python是一种什么样的语言? 1.语言的分类: a.编译型语言和解释性语言: 通常所说的计算机语言分为编译型和解释型语言.编译型语言典型的如C,C++,通常在程序执行之前必须经由编译器编译成机 ...
- 1.0 Python 学习网站
w3cschool : http://www.runoob.com/python/python-tutorial.html cnblog Python 从入门到精通: http://www.cnbl ...