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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <algorithm>
#include <vector>

typedef long long int I;

using namespace std;

#define MAX_N 7010
#define MAX 201
#define MAX_LICZ 7010*140

#define BIG 9223372036854775807LL

struct Zel {
    I kol,mas,cen;
};

int n2,n,k,m;

#define D(x) 


bool cmp1(const Zel &a, const Zel &b) {
    if(a.kol == b.kol){ 
        if (a.mas == b.mas) return a.cen<b.cen;
        return a.mas < b.mas;
    }
    return a.kol<b.kol;
}
bool cmp2(const Zel &a, const Zel &b) {
    if(a.kol == b.kol){ 
        return a.cen < b.cen;
    }
    return a.kol<b.kol;
}

Zel in2[MAX_N];
Zel in[MAX_N];

void uniq_mas() {
    int j=0;
    int kol=-1,mas=-1;
    for(int i=0;i<n2;i++) {
        if (in2[i].kol!=kol || in2[i].mas!=mas) {
            kol = in2[i].kol;
            mas = in2[i].mas;
            in[j++] = in2[i];
        }
    }
    n = j;
}

int ile_kol[MAX_N];
int poz_kol[MAX_N];



struct Licz{
    I Scen = 0;
    int r = 0;
    int grup = 0;
    int A[MAX];
};

struct Licz2 {
    I SS = 0;
    Licz *L;
    void print() {
        printf("Scen:%lld+%lld r:%d g:%d  A=", L->Scen, SS, L->r, L->grup);
        for(int i=0;i<n;i++) {
            printf("%d ", L->A[i]);
        }
        printf("\n");
    }
};

bool operator<(const Licz2 &a, const Licz2 &b) {
    return a.L->Scen+a.SS > b.L->Scen+b.SS;
}
bool operator==(const Licz2 &a, const Licz2 &b) {
    return a.L->Scen+a.SS == b.L->Scen+b.SS;
}

Licz licz[MAX_LICZ];
int licz_ile=0;

bool copy(Licz *L, Licz *&cL) {
    if(licz_ile >= MAX_LICZ) return false;
    for(int i=0;i<n;i++)
        licz[licz_ile].A[i] = L->A[i];
    licz[licz_ile].grup = L->grup;
    licz[licz_ile].Scen = L->Scen;
    licz[licz_ile].r = L->r;
    cL = &licz[licz_ile++];
    return true;
}

Licz2 createG(int g) {
    Licz * L = &licz[licz_ile++];
    L->grup = g;
    for(int i=0;i<k;i++) {
        L->A[poz_kol[i]]=g;
        L->Scen += in[poz_kol[i]].cen*g;
        L->r += in[poz_kol[i]].mas*g;
        L->r%=m;
    }
    return Licz2{0,L};
}

std::vector<Licz2> createL(Licz2 l2) {
    Licz &L = *l2.L;
    I SS = l2.SS;
    std::vector<Licz2> r;
    for(int i=0;i<n;i++) {
        if (L.A[i] > 0 && in[i+1].kol == in[i].kol && in[i+1].cen-in[i].cen == SS) {
            Licz *cL; 
            if(!copy(&L, cL)) break;
            cL->A[i]--;
            cL->A[i+1]++;
            cL->Scen += in[i+1].cen-in[i].cen;
            cL->r += in[i+1].mas-in[i].mas + m;
            cL->r %=m;
            r.push_back(Licz2{0,cL});
        }
    }
    return r;
}

std::vector<Licz2> getPossib(Licz2 l2) {
    Licz &L = *l2.L;
    std::vector<Licz2> r;
    for(int i=0;i<n;i++) {
        if (L.A[i] && in[i+1].kol == in[i].kol) r.push_back(Licz2{in[i+1].cen-in[i].cen, &L});
    }
    sort( r.begin(), r.end() );
    r.erase( unique( r.begin(), r.end() ), r.end() );
    return r;
}

I result[MAX] = {BIG};
int result_ile=0;

void add_roz(int r, I Scen) {
    if (result[r] == BIG) result_ile++;
    result[r] = min(result[r], Scen);
}

void print_in(){
    for(int i=0;i<n;i++) 
        cout << in[i].kol << ": m:" <<  in[i].mas << " c:" << in[i].cen << "\n";
}

void print_in2(){
    for(int i=0;i<n2;i++) 
        cout << in2[i].kol << ": m:" <<  in2[i].mas << " c:" << in2[i].cen << "\n";
}


int main()
{
    cin >> n2 >> k >> m;
    for(int i=0;i<n2;i++) {
       cin >> in2[i].kol >> in2[i].mas >> in2[i].cen;
       in2[i].mas%=m;
    }
    sort(&in2[0],&in2[n2], cmp1);
    uniq_mas();
    sort(&in[0],   &in[n], cmp2);
    for(int i=0;i<n;i++) ile_kol[in[i].kol]++;
    for(int i=0;i<k;i++) poz_kol[i+1]=poz_kol[i]+ile_kol[i+1];


    std::fill_n(result, m, BIG);
    result[0]=0;
    result_ile=1;

    bool ok = true;
    for(int i=1;i<=k;i++) if(ile_kol[i]==0) ok = false;

    if (ok) {

        std::vector<Licz2> q;
        //TODO:i<2*m+2 (??)
        for(int i=1;i<m+1;i++) {
            auto L2 = createG(i);
            D(L2.print());
            q.push_back(L2);
        }
        std::make_heap(q.begin(), q.end());

        while(result_ile != m && !q.empty()) {
            std::pop_heap(q.begin(), q.end());
            auto top1 = q.back();
            q.pop_back();
            D(printf("wyjete:########################\n");
            top1.print();)
            if(top1.SS == 0) {
                add_roz(top1.L->r, top1.L->Scen);
                D(printf("roz: %d\n",result_ile);
                top1.print());
                auto wL = getPossib(top1);
                for(auto w: wL)  {
                    if (w.SS == 0) {
                        auto vL = createL(w);
                        for(auto v: vL) {
                            D(printf("dodajZ:\n");
                            v.print());
                            q.push_back(v);
                            std::push_heap(q.begin(), q.end());
                        }
                    } else {
                        D(printf("dodaj:\n");
                        w.print());
                        q.push_back(w);
                        std::push_heap(q.begin(), q.end());
                    }
                }
            } else {
                auto vL = createL(top1);
                for(auto w: vL) {
                    D(printf("dodaj:\n");
                    w.print());
                    q.push_back(w);
                    std::push_heap(q.begin(), q.end());
                }
            
            }
        }
    }

    for(int i=0;i<m;i++) {
        if (result[i] == BIG) result[i]=-1;
        printf("%lld\n", result[i]);
    }

    
}