D - Teleportation(斜率gcd变形)

D - Teleportation

The Republic of AtCoder lies on a Cartesian coordinate plane.
It has N towns, numbered 1,2,,N. Town ii is at (xi,yi), and no two different towns are at the same coordinates.

There are teleportation spells in the nation. A spell is identified by a pair of integers (a,b), and casting a spell (a, b) at coordinates (x, y) teleports you to (x+a, y+b).

Snuke is a great magician who can learn the spell (a, b) for any pair of integers (a, b) of his choice. The number of spells he can learn is also unlimited.
To be able to travel between the towns using spells, he has decided to learn some number of spells so that it is possible to do the following for every pair of different towns (i, j).

  • Choose just one spell among the spells learned. Then, repeatedly use just the chosen spell to get from Town i to Town j.

At least how many spells does Snuke need to learn to achieve the objective above?

 

 

 

Input

Input is given from Standard Input in the following format:

N
x1 y1
x2 y2
....
xN yN

Output

Print the minimum number of spells Snuke needs to learn.


Sample Input 1 Copy

Copy
3
1 2
3 6
7 4

Sample Output 1 Copy

Copy
6

The figure below illustrates the positions of the towns (along with the coordinates of the four corners).

image

If Snuke learns the six spells below, he can get from Town ii to Town jj by using one of the spells once for every pair (i,j)(i,j) (i \neq j)(i=j), achieving his objective.

  • (2, 4)(2,4)
  • (-2, -4)(2,4)
  • (4, -2)(4,2)
  • (-4, 2)(4,2)
  • (-6, -2)(6,2)
  • (6, 2)(6,2)

Another option is to learn the six spells below. In this case, he can get from Town ii to Town jj by using one of the spells twice for every pair (i,j)(i,j) (i \neq j)(i=j), achieving his objective.

  • (1, 2)(1,2)
  • (-1, -2)(1,2)
  • (2, -1)(2,1)
  • (-2, 1)(2,1)
  • (-3, -1)(3,1)
  • (3, 1)(3,1)

There is no combination of spells that consists of less than six spells and achieve the objective, so we should print 66.


Sample Input 2 Copy

Copy
3
1 2
2 2
4 2

Sample Output 2 Copy

Copy
2

The optimal choice is to learn the two spells below:

  • (1, 0)
  • (-1, 0)

Sample Input 3 Copy

Copy
4
0 0
0 1000000000
1000000000 0
1000000000 1000000000

Sample Output 3 Copy

Copy
8


题目大意:

就是给你n个坐标,假设一对数(a,b)那么一个坐标(x,y)就可以到(x+k*a,y+k*b),
问最少需要多少对(a,b)能使得这n个坐标互相都能到达
题解
这个题对于能加(x+k*a,y+k*b)这个一定要想到gcd的,还有斜率也是可以往gcd那想的,然后再用set维护一下
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
int n;
const int maxn=1e5+100;
ll x[maxn],y[maxn];
set<pair<ll,ll>>s;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x[i]>>y[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j){
                continue;
            }
            int num=__gcd(abs(x[i]-x[j]),abs(y[i]-y[j]));
            s.insert({(x[i]-x[j])/num,(y[i]-y[j])/num}); 
        }
    }
    cout<<s.size()<<endl;
}

 



posted @ 2021-11-09 13:55  lipu123  阅读(89)  评论(0)    收藏  举报