結果

問題 No.461 三角形はいくつ?
ユーザー ヒッキープログラミングするスレ GitHub ガチ
提出日時 2016-12-12 07:31:33
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 2,142 bytes
コンパイル時間 763 ms
コンパイル使用メモリ 67,740 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-29 15:30:23
合計ジャッジ時間 4,568 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 11 WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

template <typename T>
T gcd(T x, T y) {
    T z; while (x) { z = x; x = y % x; y = z; } return y;
}

template <typename T>
struct rational {
    T a, b;
    rational():a(0),b(1) {}
    rational(T _a):a(_a),b(1) {}
    rational(T _a, T _b):a(_a),b(_b) {}
    rational<T> operator +(const rational<T> o) const { 
        return b == o.b ? rational<T>(a + o.a, b) : rational<T>(a * o.b + o.a * b, b * o.b);
    }
    rational<T> operator -(const rational<T> o) const {
        return b == o.b ? rational<T>(a - o.a, b) : rational<T>(a * o.b - o.a * b, b * o.b);
    }
    bool operator <(const rational<T> o) const {
        return b == o.b ? a < o.a : a * o.b < o.a * b;
    }
    rational<T> diff1() const { return rational<T>(b - a, b); }
};

int sfun(const void* p, const void* q) {
    return ((std::vector<rational<long long> >*)p)->size() - ((std::vector<rational<long long> >*)q)->size();
}

int main() {
    
    using std::vector;
    using std::lower_bound;
    typedef rational<long long> rational;
    
    int n;
    vector<rational> ps[3];
    
    std::cin >> n;
    for (auto i = 0; i < n; i++) {
        int p, a, b;
        std::cin >> p >> a >> b;
        int k = std::max(a, b);
        int g = gcd(k, a + b);
        ps[p].push_back({k / g, (a + b) / g});
    }
    
    for (auto i = 0; i < 3; i++) {
        ps[i].push_back({1, 1});
        std::sort(ps[i].begin(), ps[i].end());
    }
    
    qsort(ps, sizeof(ps)/sizeof(ps[0]), sizeof(ps[0]), sfun);
    
    auto &p0 = ps[0], &p1 = ps[1], &p2 = ps[2];
    auto ans = 0;
    
    for (auto it0 = p0.begin(); it0 != p0.end(); it0++) {
        auto dx = it0->diff1();
        for (auto it1 = lower_bound(p1.begin(), p1.end(), dx); it1 != p1.end(); it1++) {
            auto dy = it1->diff1();
            auto it2 = lower_bound(p2.begin(), p2.end(), std::max(dx, dy));
            ans += (p2.end() - it2) - 1;
            if (!std::binary_search(it2, p2.end(), dx + dy)) {
                ans++;
            }
        }
    }
    
    std::cout << ans << std::endl;
    
    
    return 0;
}
0