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
#include<iostream>
#include<map>
#include<cmath>
using namespace std;

long NWD(long a, long b)
{
    long  h;
    while(b!=0)
    {
        h=b;
        b=a%b;
        a=h;
    }
    return a;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int t;
    cin>>t;
    for(int y=0 ; y<t ; y++)
    {
        int n;
        cin>>n;
        int tabl[n+1],taba[n+1],tabb[n+1];
        int sume1=0,sume2=0;
        int d1=0,d2=0;
        int maks1=0,maks2=0;
        long long min1=9999999,min2=9999999;
        map<pair<int,int>,int> mapa1,mapa2;
        int wsp=0;
          for(int i=0 ; i<n ; i++)
    {
        cin>>tabl[i]>>taba[i]>>tabb[i];
        mapa1[make_pair(tabl[i],taba[i])]++;
        mapa2[make_pair(tabl[i],tabb[i])]++;
        if(taba[i]>maks1) maks1=taba[i];
        if(tabb[i]>maks2) maks2=tabb[i];
        if(taba[i]<min1) min1=taba[i];
        if(tabb[i]<min2) min2=tabb[i];
        sume1*=taba[i]*tabl[i];
        sume2*=tabb[i]*tabl[i];
        d1+=tabl[i];
        d2+=tabl[i];
    }
         for(int i=0 ; i<n ; i++)
    {
         if(mapa1[make_pair(tabl[i],tabb[i])]==mapa2[make_pair(tabl[i],tabb[i])]) wsp++;
    }
   // cout<<"WSP "<<wsp<<endl;
   // cout<<sume1<<" "<<d1<<endl;
    //  cout<<sume2<<" "<<d2<<endl;
      bool result=false;
    if(d1>=n || wsp==n) result=true;
    int h=NWD(sume1,d1);
    sume1/=h;
    d1/=h;
    h=NWD(sume2,d2);
       sume2/=h;
    d2/=h;
   // cout<<sume1<<" "<<d1<<endl;
    //  cout<<sume2<<" "<<d2<<endl;
    //cout<<min1<<" "<<min2<<endl;
    if(sume1==sume2 && d1==d2 && result && maks1>=maks2 && min1<=min2) cout<<"TAK"<<endl;
    else cout<<"NIE"<<endl;
    }

    return 0;
}