Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are nn ghosts in the universe, they move in the OXYOXY plane, each one of them has its own velocity that does not change in time: →V=Vx→i+Vy→jV→=Vxi→+Vyj→ where VxVx is its speed on the xx-axis and VyVy is on the yy-axis.
A ghost ii has experience value EXiEXi, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX=∑ni=1EXiGX=∑i=1nEXi will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time TT, and magically all the ghosts were aligned on a line of the form y=a⋅x+by=a⋅x+b. You have to compute what will be the experience index of the ghost kind GXGX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GXGX may already be greater than 00, because many ghosts may have scared one another at any moment between [−∞,T][−∞,T].
The first line contains three integers nn, aa and bb (1≤n≤2000001≤n≤200000, 1≤|a|≤1091≤|a|≤109, 0≤|b|≤1090≤|b|≤109) — the number of ghosts in the universe and the parameters of the straight line.
Each of the next nn lines contains three integers xixi, VxiVxi, VyiVyi (−109≤xi≤109−109≤xi≤109, −109≤Vxi,Vyi≤109−109≤Vxi,Vyi≤109), where xixi is the current xx-coordinate of the ii-th ghost (and yi=a⋅xi+byi=a⋅xi+b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j)(i,j) xi≠xjxi≠xj for i≠ji≠j.
Output one line: experience index of the ghost kind GXGX in the indefinite future.
4 1 1 1 -1 -1 2 1 1 3 1 1 4 -1 -1
8
3 1 0 -1 1 0 0 0 -1 1 -1 -2
6
3 1 0 0 0 0 1 0 0 2 0 0
0
There are four collisions (1,2,T−0.5)(1,2,T−0.5), (1,3,T−1)(1,3,T−1), (2,4,T+1)(2,4,T+1), (3,4,T+0.5)(3,4,T+0.5), where (u,v,t)(u,v,t) means a collision happened between ghosts uu and vv at moment tt. At each collision, each ghost gained one experience point, this means that GX=4⋅2=8GX=4⋅2=8.
In the second test, all points will collide when t=T+1t=T+1.
大致题意:给出N个点(每个点均在直线y=ax+b上)和每个点前进的速度/方向,求能遇见多少次。
解法:推公式,由于起始点为(xi,axi+b),若i,j能相遇,则应满足(xi+t*vxi = xj+t*vxj, (a*xi+b) + t*vyi = (a*xj+b)+t*vyj)
该公式化简后可发现,能否相遇仅与a*vx-vy的值有关。该值相等,则两个点可相交。
注意,需要处理平行的情况(vxi = vxj,vyi = vyj)
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,a,b,ans,para;
map<long long,int> m;
map< pair<int,int>,int> p;
int x,Vx,Vy;
int main()
{
cin>>n>>a>>b;
for(int i=1; i<=n; i++){
scanf("%d%d%d",&x,&Vx,&Vy);
ll key = a*Vx-Vy;
ans += m[key];
m[a*Vx-Vy]++;
para += p[make_pair(Vx,Vy)];
p[make_pair(Vx,Vy)]++;
}
cout<<(ans-para)*2<<endl;
return 0;
}
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are nn ghosts in the universe, they move in the OXYOXY plane, each one of them has its own velocity that does not change in time: →V=Vx→i+Vy→jV→=Vxi→+Vyj→ where VxVx is its speed on the xx-axis and VyVy is on the yy-axis.
A ghost ii has experience value EXiEXi, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX=∑ni=1EXiGX=∑i=1nEXi will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time TT, and magically all the ghosts were aligned on a line of the form y=a⋅x+by=a⋅x+b. You have to compute what will be the experience index of the ghost kind GXGX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GXGX may already be greater than 00, because many ghosts may have scared one another at any moment between [−∞,T][−∞,T].
The first line contains three integers nn, aa and bb (1≤n≤2000001≤n≤200000, 1≤|a|≤1091≤|a|≤109, 0≤|b|≤1090≤|b|≤109) — the number of ghosts in the universe and the parameters of the straight line.
Each of the next nn lines contains three integers xixi, VxiVxi, VyiVyi (−109≤xi≤109−109≤xi≤109, −109≤Vxi,Vyi≤109−109≤Vxi,Vyi≤109), where xixi is the current xx-coordinate of the ii-th ghost (and yi=a⋅xi+byi=a⋅xi+b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j)(i,j) xi≠xjxi≠xj for i≠ji≠j.
Output one line: experience index of the ghost kind GXGX in the indefinite future.
4 1 1 1 -1 -1 2 1 1 3 1 1 4 -1 -1
8
3 1 0 -1 1 0 0 0 -1 1 -1 -2
6
3 1 0 0 0 0 1 0 0 2 0 0
0
There are four collisions (1,2,T−0.5)(1,2,T−0.5), (1,3,T−1)(1,3,T−1), (2,4,T+1)(2,4,T+1), (3,4,T+0.5)(3,4,T+0.5), where (u,v,t)(u,v,t) means a collision happened between ghosts uu and vv at moment tt. At each collision, each ghost gained one experience point, this means that GX=4⋅2=8GX=4⋅2=8.
In the second test, all points will collide when t=T+1t=T+1.