poj2002Squares(hash)

http://poj.org/problem?id=2002

刚开始hash的两点 TLE了 又改为一点 已知对角两点 求令两点

View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 using namespace std;

 5 #define MOD 977777

 6 int head[1000010],v[1000010],next[1000010],num[1000010][2],t,f[1010][1010];

 7 struct node

 8 {

 9     int x,y;

10 }co[1010];

11 int judge(int s,struct node x)

12 {

13     int i;

14     for(i = head[s] ; i!= -1 ; i = next[i])

15         if(co[v[i]].x==x.x&&co[v[i]].y==x.y)

16         return v[i];

17     return 0;

18 }

19 int DoneSq(struct node a, struct node c)//求令两点 网上搜的公式

20 {

21      struct node b, d;

22      double x,y,mx, my,tx,ty;

23      int s1,s2,i;

24      mx = (a.x+c.x)/2.0, my = (a.y+c.y)/2.0;

25      x = a.x - mx;    y = a.y - my;

26      tx = -y + mx;   ty = x + my;

27      if((tx-(int)tx)!=0||(ty-(int)ty)!=0)

28          return 0;

29      b.x = tx;b.y = ty;

30      x = c.x - mx;    y = c.y - my;

31      tx = - y + mx; ty = x + my;

32      if((tx-(int)tx)!=0||(ty-(int)ty)!=0)

33          return 0;

34      d.x = tx;d.y=ty;

35      s1 = b.x*977+b.y; s1 = s1%MOD;

36      if(s1<0) s1+=MOD;

37      s2 = d.x*977+d.y; s2 = s2%MOD;

38      if(s2<0) s2+=MOD;

39      if(judge(s1,b)&&judge(s2,d))

40      {

41          f[judge(s1,b)][judge(s2,d)] = 1;

42          f[judge(s2,d)][judge(s1,b)] = 1;

43          return 1;

44      }

45      return 0;

46 

47 }

48 void init()

49 {

50     t = 0;

51     memset(head,-1,sizeof(head));

52 }

53 void add(int a,int b)

54 {

55     next[t] = head[a];

56     head[a] = t;

57     v[t] = b;

58     t++;

59 }

60 int main()

61 {

62     int i,j,k,n,g,s,numm;

63     while(cin>>n)

64     {

65         if(n==0)

66         break;

67         init();

68         memset(f,0,sizeof(f));

69         g = 0;numm=0;

70         for(i = 1; i <= n ; i++)

71         {

72             cin>>co[i].x>>co[i].y;

73             s = co[i].x*977+co[i].y;

74             s = s%MOD;

75             if(s<0)

76             s+=MOD;

77             add(s,i);

78         }

79         for(i = 1; i <= n ; i++)

80             for(j = i+1 ; j <= n ; j++)

81             {

82                 if(!f[i][j])

83                 numm+=DoneSq(co[i],co[j]);

84             }

85         cout<<numm<<endl;

86     }

87     return 0;

88 }

 

你可能感兴趣的:(hash)