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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 #include #include #include #include #include #include using namespace std; using L = long long; const double EPS = 1e-5; struct F { explicit F(double d):d(d){} private: double d; public: friend F operator+(F a, F b) { return F(a.d + b.d); } friend F operator*(F a, F b) { return F(a.d * b.d); } friend F operator-(F a, F b) { return F(a.d - b.d); } friend F operator-(F a) { return F(-a.d); } friend F& operator-=(F& a, F b) { a = a - b; return a; } friend F operator/(F a, F b) { return F(a.d / b.d); } friend bool operator< (F a, F b) { return (a-b).d < -EPS; } friend bool operator> (F a, F b) { return (a-b).d > EPS; } friend bool operator>= (F a, F b) { return (a-b).d >= -EPS; } friend bool operator<= (F a, F b) { return (a-b).d <= EPS; } friend bool operator==(F a, F b){ return abs((a-b).d) < EPS; } friend bool operator!=(F a, F b){ return !(a == b); } friend ostream& operator<<(ostream& o, F b){ o << b.d; return o; } template friend ostream& operator<<(ostream& o, const pair& d){ o << "(" << d.first << ", " << d.second << ")"; return o; } template friend ostream& operator<<(ostream& o, const deque& d){ o << "["; for (const T&i:d) o << i << ", "; o <<"]"; return o; } }; struct fail {}; pair mix(pair a, pair b) { return make_pair( (a.first * a.second + b.first * b.second) / (b.second+a.second), b.second+a.second); } #define VOL(x) x.second #define TEMP(x) x.first void solve(deque>inputs, deque> outputs) { std::sort(inputs.begin(), inputs.end()); std::sort(outputs.begin(), outputs.end()); auto to_temp_1 = [&](F t) { if (inputs.front().first > t) throw fail(); auto curr = inputs.front(); inputs.pop_front(); //cout << inputs << endl; while (inputs.size() > 0 and TEMP(inputs.front()) <= t) { auto nxt = inputs.front(); inputs.pop_front(); curr = mix(curr, nxt); } if (inputs.size() == 0) throw fail(); F next_temp = inputs.front().first; F next_vol = inputs.front().second; inputs.pop_front(); // curr_temp * curr_vol + next_temp * x = (x + curr_vol) * t auto x = VOL(curr)*(-TEMP(curr) + t) / (next_temp - t); //cout << "curr " << curr << " x " << x << " next_vol " << next_vol << endl; //assert curr_temp * curr_vol + next_temp * x == (x + curr_vol) * t if (x > next_vol) x = next_vol; if (x != next_vol) { inputs.push_front(make_pair(next_temp, next_vol - x)); } auto new_temp = (TEMP(curr) * VOL(curr) + next_temp * x) / (VOL(curr) + x); //cout << "new_temp " << new_temp << " x " << x << endl; inputs.push_front(make_pair(new_temp, VOL(curr) + x)); }; auto to_temp = [&](F t) { //cout << "need " << t << endl; while (TEMP(inputs.front()) != t) { //cout << "inputs " << inputs << endl; to_temp_1(t); } }; for (auto out : outputs) { F out_temp = TEMP(out); F out_vol = VOL(out); //cout << "temp " << out_temp << " vol " << out_vol << endl; while (out_vol > F(0)) { to_temp(out_temp); auto curr = inputs.front(); inputs.pop_front(); if (VOL(curr) > out_vol) { VOL(curr) -= out_vol; inputs.push_front((curr)); break; } else { out_vol -= VOL(curr); } } } } int main() { ios_base::sync_with_stdio(0); int z; cin >> z; for (int i=0; i < z; i++) { int n; cin >> n; deque> inputs; deque> outputs; for (int j=0; j> l>>a>>b; inputs.push_back(make_pair(F(a), F(l))); outputs.push_back(make_pair(F(b), F(l))); } try { solve(inputs, outputs); cout<<"TAK"<<"\n"; } catch(fail) { cout<<"NIE"<<"\n"; } } }