UVA 10277 Boastin' 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 const int HASH=50007; 16 int head[HASH],next1[HASH]; 17 int hash1(unsigned int &s) 18 { 19 return s%HASH; 20 } 21 int insert(int s) 22 { 23 int h=hash1(st[s][0]); 24 int u=head[h]; 25 while(u)//链表尾一直是0,所以s不能为0 26 u=next1[u]; 27 next1[s]=head[h];//原来的链表头成为s的next 28 head[h]=s;//s成为head[h]的链表头 29 return 1; 30 } 31 int find(unsigned int s) 32 { 33 int h=hash1(s); 34 int u=head[h]; 35 while(u) 36 { 37 if(st[u][0]==s) return u; 38 u=next1[u]; 39 } 40 return 0; 41 } 42 int main() 43 { 44 //freopen("/home/user/桌面/in","r",stdin); 45 while(scanf("%u%u",&p,&q)==2&&(p||q)) 46 { 47 memset(head,0,sizeof(head)); 48 unsigned int i=2,x,j=1; 49 if(p==0) 50 { 51 printf("%d %d\n",0,2); 52 continue; 53 } 54 x=gcd(p,q); 55 p/=x;q/=x; 56 for(;i<=50000;i++) 57 { 58 unsigned int t=i*i-i; 59 if(t%p==0) 60 { 61 st[j][0]=t/p; 62 st[j][1]=i; 63 insert(j); 64 j++; 65 } 66 if(t%q==0&&(x=find(t/q))) 67 { 68 printf("%u %u\n",st[x][1],i-st[x][1]); 69 break; 70 } 71 } 72 if(i>50000) puts("impossible"); 73 } 74 return 0; 75 }