CF334 div1 D :Ruminations on Ruminants


题目描述:

D. Ruminations on Ruminants
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Kevin Sun is ruminating on the origin of cows while standing at the origin of the Cartesian plane. He notices n lines on the plane, each representable by an equation of the form ax + by = c. He also observes that no two lines are parallel and that no three lines pass through the same point.

For each triple (i, j, k) such that 1 ≤ i < j < k ≤ n, Kevin considers the triangle formed by the three lines . He calls a triangle original if the circumcircle of that triangle passes through the origin. Since Kevin believes that the circles of bovine life are tied directly to such triangles, he wants to know the number of original triangles formed by unordered triples of distinct lines.

Recall that the circumcircle of a triangle is the circle which passes through all the vertices of that triangle.

Input
The first line of the input contains a single integer n (3 ≤ n ≤ 2000), the number of lines.

The next n lines describe lines . The i-th of these lines contains three space-separated integers ai, bi, ci (|ai|, |bi|, |ci| ≤ 10 000, ai2 + bi2 > 0), representing the equation aix + biy = ci of line .

Output
Print a single integer, the number of triples (i, j, k) with i < j < k such that lines form an original triangle.

Sample test(s)
input
4
1 0 0
0 1 0
1 1 -1
1 -1 2
output
2

题解:

枚举出三个点之后判定是否过原点还是可以的.但是不能枚举.
只知道相加180这个限制是不好想办法的.
所以转化成:远点向直线做垂线,只有三个垂足在同一条直线上的时候,才行.
那么我们先做找出来垂足,然后数三个点在一条直线的三元组个数.
转化一下,就是两点确定一条直线,然后再看每一条直线上有多少个点,用组合数算.
但是这样不好写(还要转成直线的方程),i j k(i < j < k), 我们枚举i,要求j和k相对于i的斜率必须一样. 如果有重合的点,那么任意一个第三个点都可以.
注意,要全部用整数,先联立方程算出来垂足,用分子分母表示,然后求斜率减之后也化成标准的形式.

重点:

1.转化限制条件,判定改成好用的限制.
2.不用double,联立方程解除垂足
3.不用搞出来直线,枚举一个点,要求之后斜率一样.

代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>

using namespace std;

typedef long long ll;

const int maxn = 2000 + 100;
int n, a[maxn], b[maxn], c[maxn], ac[maxn], bc[maxn], a2b2[maxn];
ll res;
struct Point {
    ll x, y;
};

ll gcd(ll x, ll y) {
    if(y == 0)
    return x;
    return gcd(y, x % y);
}
Point getStand(Point t) {
    if(t.x == 0) {
    if(t.y == 0)
        return t;
    t.y = 1;
    return t;
    }
    else if(t.y == 0) {
    t.x = 1;
    return t;
    }
    ll d = gcd(abs(t.x), abs(t.y));
    t.x /= d;
    t.y /= d;
    if(t.x < 0 && t.y < 0 || t.x > 0 && t.y < 0) {
    t.x = -t.x;
    t.y = -t.y;
    }
    return t;
}
void solve() {
    for(int i = 1; i <= n; i++) {
    ac[i] = a[i] * c[i];
    bc[i] = b[i] * c[i];
    a2b2[i] = a[i] * a[i] + b[i] * b[i];
    }
    res = 0;
    for(int i = 1; i <= n; i++) {
    map<ll, map<ll, ll> > mp;
    int same = 0, ans = 0;
    for(int j = i + 1; j <= n; j++) {
        Point t;
        t.x = (ll)a2b2[j] * (ll)ac[i] - (ll)a2b2[i] * (ll)ac[j];
        t.y = (ll)a2b2[j] * (ll)bc[i] - (ll)a2b2[i] * (ll)bc[j];
        t = getStand(t);
        if(t.x == 0 && t.y == 0) {
        same++;
        ans += (j - i - 1);
        }
        else {
        ans += mp[t.x][t.y] + same;
        mp[t.x][t.y]++;
        }
    }
    res += ans;
    }
    printf("%I64d\n", res);
}

int main() {
  //  freopen("D.txt", "r", stdin);
    while(scanf("%d", &n) != EOF) {
    for(int i = 1; i <= n; i++) {
        scanf("%d%d%d", &a[i], &b[i], &c[i]);
    }
    solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值