codeforces round#478D Ghosts

本文介绍了一个有趣的问题:预测一组在直线上运动的幽灵之间的碰撞次数。通过给出的速度和初始位置,利用数学推导和算法设计,计算出无限未来中所有可能发生的碰撞总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Open the transmission gate

Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.

There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: V⃗ =Vxi+Vyj V → = V x i → + V y j → where Vxi V x i → is its speed on the x-axis and Vyj V y j → is on the y-axis.

A ghost i i has experience value EXi , 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=1EXi G X = ∑ i = 1 n E X i will never increase.

Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T , and magically all the ghosts were aligned on a line of the form y=a⋅x+b. You have to compute what will be the experience index of the ghost kind GX G X in the indefinite future, this is your task for today.

Note that when Tameem took the picture, GX G X may already be greater than 0, because many ghosts may have scared one another at any moment between [,T] [ − ∞ , T ] .

Input

The first line contains three integers n, a and b (1≤n≤200000, 1≤|a|≤109, 0≤|b|≤109) — the number of ghosts in the universe and the parameters of the straight line.Each of the next n lines contains three integers xi x i , Vxi V x i , Vyi V y i (−109≤xi≤109, −109≤Vxi,Vyi≤109), where xi x i is the current x-coordinate of the i-th ghost (and yi=axi+b y i = a ⋅ x i + b ).

It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) xi≠xj for i≠j.

Output

Output one line: experience index of the ghost kind GX in the indefinite future.

Examples
Input

4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1

Output

8

Input

3 1 0
-1 1 0
0 0 -1
1 -1 -2

Output

6

Input

3 1 0
0 0 0
1 0 0
2 0 0

Output

0

Note

There are four collisions (1,2,T−0.5), (1,3,T−1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX=4⋅2=8.

In the second test, all points will collide when t=T+1.

这里写图片描述

The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.

translation

给你一条直线,在某个时刻的时候所有的鬼都在这个直线下,每个鬼都有自己的速度,问你有多少对鬼相遇过。

solution

其实说白了就是推公式。给一个队友的证明:传送门

code

/*
队友是用的map,我用的是数组模拟,他写起来简单,我稍微快一点点
*/
#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e5+10;
long long num[maxm];
const int mod = 1e9+7;
long long nouse[maxm];
int main() {
    //freopen("C:\\Users\\ACM2018\\Desktop\\in.txt","r",stdin);
    long long n,a;
    long long ans = 0;
    scanf("%lld%lld%*lld",&n,&a);
    for(int i = 0;i<n;i++){
        long long vx,vy;
        scanf("%*d%lld%lld",&vx,&vy);
        nouse[i] = vx*mod+vy;
        num[i] = -vy+a*vx;
    }
    sort(nouse,nouse+n);
    nouse[n] = 2e18;
    long long has = 1;
    long long now = nouse[0];
    for(int i = 1;i<=n;i++){
        if(nouse[i]==now) has++;
        else{
            ans-=has*(has-1);
            has = 1;
            now = nouse[i];
        }
    }
    sort(num,num+n);
    has = 1;
    now = num[0];
    num[n] = 2e18;
    for(int i = 1;i<=n;i++){
        if(num[i]==now){
            has++;
        }
        else{
            ans+=has*(has-1);
            has = 1;
            now = num[i];
        }
    }
    printf("%lld\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值