1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <bits/stdc++.h>

using u32 = uint32_t;
using u64 = uint64_t;
using i32 = int32_t;
using i64 = int64_t;

struct Cup
{
    u32 L;
    u32 A;
    const u32 B;
};

int main()
{
    std::ios_base::sync_with_stdio(0);

    u32 tcases, n, l, a, b;

    std::cin >> tcases;

    for (u32 i = 0; i < tcases; i++)
    {
        std::vector<Cup> vec;
        std::cin >> n;
        for (u32 j = 0; j < n; j++)
        {
            std::cin >> l >> a >> b;
            vec.push_back(Cup{l, a, b});
        }

        u32 maxCurrent = 0, maxExpected = 0, minCurrent = UINT32_MAX, minExpected = UINT32_MAX,
            weightOfMaxCurr = 0, weightOfMaxExp = 0, weightOfMinCurr = 0, weightOfMinExp = 0, tmp_weightSum = 0;

        u64 la_ratio = 0, lb_ratio = 0;

        for (const auto &cup : vec)
        {
            maxCurrent = std::max(maxCurrent, cup.A);
            maxExpected = std::max(maxExpected, cup.B);
            minCurrent = std::min(minCurrent, cup.A);
            minExpected = std::min(minExpected, cup.B);
            la_ratio += cup.L * cup.A;
            lb_ratio += cup.L * cup.B;
            tmp_weightSum += cup.L;
        }

        if (maxExpected > maxCurrent or
            minExpected < minCurrent or
            la_ratio != lb_ratio)
        {
            std::cout << "NIE" << std::endl;
            continue;
        }

        for (const auto &cup : vec)
        {
            if (maxCurrent == maxExpected)
            {
                if (cup.A == maxCurrent)
                {
                    weightOfMaxCurr += cup.L;
                }
                if (cup.B == maxExpected)
                {
                    weightOfMaxExp += cup.L;
                }
            }
            if (minCurrent == minExpected)
            {
                if (cup.A == minCurrent)
                {
                    weightOfMinCurr += cup.L;
                }
                if (cup.B == minExpected)
                {
                    weightOfMinExp += cup.L;
                }
            }
        }

        if (weightOfMaxCurr < weightOfMaxExp or
            weightOfMinCurr < weightOfMinExp)
        {
            std::cout << "NIE" << std::endl;
            continue;
        }

        long double average = la_ratio / tmp_weightSum;
        long double currentDeviation = 0, expectedDeviation = 0;
        for (const auto &cup : vec)
        {
            currentDeviation += std::abs(average - cup.A) * cup.L;
            expectedDeviation += std::abs(average - cup.B) * cup.L;
        }
        if (currentDeviation < expectedDeviation)
        {
            std::cout << "NIE" << std::endl;
            continue;
        }

        for (const auto &c : vec)
        {
            currentDeviation = 0, expectedDeviation = 0;
            for (const auto &cup : vec)
            {
                currentDeviation += std::abs(static_cast<i32>(c.B) - static_cast<i32>(cup.A)) * cup.L;
                expectedDeviation += std::abs(static_cast<i32>(c.B) - static_cast<i32>(cup.B)) * cup.L;
            }
            if (currentDeviation < expectedDeviation)
            {
                std::cout << "NIE" << std::endl;
                break;
            }
        }

        if (currentDeviation < expectedDeviation)
        {
            continue;
        }

        std::cout << "TAK" << std::endl;
    }

    return 0;
}