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 ...
随机推荐
- webpack 相关资料
github webpack Youtube: Advanced Webpack code splitting list of plugins webpack examples What's new ...
- 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> ...
- xampp 搭建 web mac上
1.安装 2.修改数据库密码,删除phpmyadmin ,用navicat 控制数据库 3.修改/Applications/XAMPP/xamppfiles/etc/extra/httpd-vhos ...
- NOIP2002-普及组复赛-第三题-选数
题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整 ...
- 洛谷-陶陶摘苹果(升级版)-BOSS战-入门综合练习1
题目描述 Description 又是一年秋季时,陶陶家的苹果树结了n个果子.陶陶又跑去摘苹果,这次她有一个a公分的椅子.当他手够不着时,他会站到椅子上再试试. 这次与NOIp2005普及组第一题不同 ...
- Mac 命令行中进入带有空格的文件夹
http://blog.sina.com.cn/s/blog_5e8392b10100jkvg.html 今天在公司用mac的时候,有个文件夹的名字有空格,怎么都进不去,在网上一查原来不能直接cd 文 ...
- 【ADT】链表的基本C语言实现
什么是抽象数据类型?首先,这一概念是软件开发人员在力求编写的代码健壮.易维护且可以复用的过程中产生的.英文是AbstractData Type.有人将其比作"抽象"的墙壁,&quo ...
- 《C++ Primer》之指向函数的指针
函数指针是指指向函数而非指向对象的指针.像其他指针一样,函数指针也指向某个特定的类型.函数类型由其返回类型以及形参表确定,而与函数名无关: // pf points to function retur ...
- mysql 1053错误,无法启动的解决方法
mysql 1053错误,无法启动的解决方法 windows2003服务器中,服务器重启后Mysql却没有启动,手动启动服务时提示1053错误. 尝试了以下方法,终于解决. 1.在DOS命令行使用 第 ...
- js 学习总结
new array()[] []表示数组new object(){} {}表示对象 JavaScript 对象 对象由花括号分隔.在括号内部,对象的属性以名称和值对的形式 (name : value) ...