Planet Communcation Gym - 101466C (Analog + GCD)

 Lately the communication between planets has been an important issue, which is why the Earth has made many efforts to be connected to every other planet in the universe.

 The Universal Network Army of Lasers (UNAL) is trying to connect the Earth with other planets in the universe. In order to make this, the UNAL uses a laser machine which each time it is used, it sends a communication signal in the form of a ray of light in the direction of the planet.

 This laser machine is so powerful, that a ray of light generated by it is capable to cross all the planets it touches until the end of the universe. Moreover, when it fires a ray of light in one direction it also fires a ray of light in the opposite direction.

 Given the coordinates of all the planets in the universe, help the UNAL to design the way to communicate the Earth with all other planets in the universe using the machine the minimum number of times.

 

Input

 The first line of input contains one integer n (1 ≤ n ≤ 5000), the number of planets.

 The next n lines contains the list of coordinates of the planets. In particular, the i - th line contains three integers xiyizi ( - 109 ≤ xi, yi, zi ≤ 109), the coordinates of the i - th planet, respectively. The Earth is the first planet on the list. It is guaranteed that all the planets have different coordinates.

 

Output

 Output a single integer, the minimun number of times the machine should be used to communicate the Earth with all other planets in the universe

 

Examples

Input
3
0 0 0
1 1 0
-1 -1 0
Output
1

Input
4
0 0 0
1 0 0
0 1 0
0 0 1
Output
3
 
Meaning of the questions:
In the three-dimensional space to n coordinates, the first coordinate is the Earth, the Earth would like to ask the other n-1 transmit signals planet, requires a minimum of number of transmit
It means asking the n-1 respectively connect with the earth planet, there are several requirements collinear, minus the total number of lines is the minimum number of common
 
Ideas:
Gcd will coordinate with simplification, see how many duplicates, lose it
The GCD, the Euclidean algorithm, also known as the Euclidean algorithm, the algorithm for seeking the greatest common multiple of Fuji
 
  . 1 #include <stdio.h>
   2 #include < String .h>
   . 3  
  . 4  struct Node
   . 5  {
   . 6      Long  Long A, B, C;
   . 7 } STR [ 5005 ];
   . 8  
  . 9  Long  Long VIS [ 5005 ];
 10  
. 11  Long  Long GCD ( Long  Long a, Long  Long B) // key luxury connotation GCD
 12 is  {
 13 is      IF (B == 0 ) return a;
 14     else return gcd(b, a%b);
 15 }
 16 
 17 int main()
 18 {
 19     long long n, i, j, x, m;
 20     long long a, b, c, eara, earb, earc;
 21     m = 0;
 22     scanf("%lld", &n);
 23     memset(vis, 0, sizeof(vis));
 24     scanf("%lld %lld %lld", &eara, &earb, &earc);   //地球坐标
 25     N-- ; // remove earth, n-1 star ball
 26 is      for (I = 0 ; I <n-; I ++ )
 27      {
 28          Scanf ( " % LLD% LLD% LLD " , & A, & B, & C);
 29  
30          a - = SA; // relative coordinates of the earth
 31 is          B - = SB;
 32          C - = SC;
 33 is  
34 is          IF (a < 0 ) // the reverse is also collinear, in order to facilitate subsequent processing of the data, a variable number , unified
 35          {
 36              A = - A;
 37 [              B = - B;
 38 is             = C - C;
 39          }
 40          IF (A == 0 && B == 0 ) // big thick length is determined to get the coordinates of the most simple, special consideration 0
 41 is          {
 42 is              STR [I] II.A STR = [I ] .B = 0 ;
 43 is              STR [I] .c = . 1 ;
 44 is          }
 45          the else  IF (B == 0 && C == 0 )
 46 is          {
 47              STR [I] .B STR = [I] .c = 0 ;
 48              STR [I] = II.A . 1 ;
 49          }
 50         else if(a==0&&c==0)
 51         {
 52             str[i].a = str[i].c = 0;
 53             str[i].b = 1;
 54         }
 55         else if(a==0)
 56         {
 57             str[i].a = 0;
 58             x = gcd(b, c);
 59             str[i].b = b / x;
 60             str[i].c = c / x;
 61         }
 62         else if(b==0)
 63         {
 64             str[i].b = 0;
 65             x = gcd(a, c);
 66             str[i].a = a / x;
 67             str[i].c = c / x;
 68         }
 69         else if(c==0)
 70         {
 71             str[i].c = 0;
 72             x = gcd(a, b);
 73             str[i].a = a / x;
 74             STR [I] .B = B / X;
 75          }
 76          the else 
77          {
 78              X = GCD (A, GCD (B, C));
 79              STR [I] = II.A A / X;
 80              STR [I]. B = B / X;
 81              STR [I] .c = C / X;
 82          }
 83          
84      }
 85  
86      for (I = 0 ; I <n-; I ++ ) // look at the most simple coordinate recurring number ( i.e., colinear)
 87      {
 88          IF (VIS [I] == . 1 ) Continue; // vis 1 had already judged, for this line of thinking in terms of the answer will not write error
 89          VIS [I] = 1 ;
 90          for (J = I; J <n-; J ++ )
 91 is          {
 92              IF (STR [I] == II.A STR [J] && II.A STR [I] .B == STR [J] .B && STR [I] == STR .c [J] .c && VIS [J] == 0 )
 93              {
 94                  VIS [J] = . 1 ;
 95                  m ++ ;
 96              }
 97          }
 98      }
 99      
100      the printf ( " % LLD \ n- " , N- m);
 101     return 0;
102 }

 

 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/0xiaoyu/p/11346751.html
gcd