Tea
Time limit2sMemory limit256 MB
Given n cups with amounts and initial temperatures, decide whether repeated splitting and mixing can produce the required amounts and target temperatures.
- Level
Medium6 of 10
- Topics
- Math, Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
Bytemommy whole-heartedly loves her Bytekids. However she is kinda forgetful, so instead of giving them proper names, she numbered them with consecutive integers from to . Every day she prepares a tea for each of her Bytekids in their favourite cups. One peculiar property of all tea cups in their home is that they have infinite capacity, even though they take finite space only. However, this is for our simplicity only. Bytekid number prefers to drink exactly bitres of tea every day. However, the amount of tea is not their only requirement. Its temperature has to be properly adjusted as well. Bytekid number would like its tea to have exactly Bytesius degrees.
Unfortunately, one day scatterbrained Bytemommy messed up the tea temperatures and the temperature of the tea in the -th cup was exactly Bytesius degrees, instead of (however the -th kid still got bitres in its cup). Nothing is lost yet. Bytekids are very clever and, using some auxiliary cups, started to mix up their teas trying to get cups with appropriate amounts and temperatures of teas. You need to determine whether it is possible for Bytekids to reach their goal, that is to get teas so that the -th tea has exactly bitres and Bytesius degrees.
Formally, Bytekids are allowed to perform the following steps arbitrarily many times:
- Partitioning the tea. Given a cup with bitres of tea with temperature , create two cups of tea with and bitres of tea with temperature for some arbitrary real value of such that (the initial cup of tea will no longer exist, obviously).
- Mixing the tea. Given two cups of tea with and bitres of tea with temperatures and , respectively, create one cup of tea with bitres of tea with temperature that is, the weighted mean of the initial temperatures (again, the initial two cups of tea will no longer exist).
Input
The first line of input contains one integer () denoting the number of testcases.
The description of each testcase starts with a line containing one integer () denoting the number of Bytekids. The following lines describe the Bytekids: the -th of them contains three integers , and () denoting the amount of tea in the -th cup in bitres (both the initial and the required final one) and the initial and required temperature of that tea, respectively.
The sum of the values of over all testcases will not exceed .
Output
You need to print lines. The -th of them should contain the word TAK if it is possible for Bytekids to reach their goal in the -th testcase, or NIE otherwise.
Hint
Denote cups of tea as a pair of numbers. The pair denotes a cup with bitres of tea with temperature Bytesius degrees.
In the first testcase Bytekids have cups and . Using the operation of partitioning the tea they can get cups , , and .
Then, by mixing cups and , they get bitres with temperature
that is, the cup . Similarly, by mixing the cup with , they get . In the end, Bytekids will have two cups with appropriate amounts and temperatures of tea.
In the second testcase both teas are too hot. We can't do much here.
However, in the third testcase it is sufficient for Bytekids to swap their cups.