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
#include <bits/stdc++.h>
//#include <cmath>
//#include <functional>
//#include <queue>
//#include <utility>
//#include <vector>
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,fma")
//add lzcnt later
void speedCheck(){
    //assert(__builtin_cpu_supports("avx2"));
    //assert(__builtin_cpu_supports("bmi"));
    //assert(__builtin_cpu_supports("bmi2"));
    //assert(__builtin_cpu_supports("popcnt"));
    //assert(__builtin_cpu_supports("lzcnt"));
    //assert(__builtin_cpu_supports("fma"));
}
#define SecurityChecksOn false
#define DebugOn false
#define MiniDebugOn false
#define securityCheck if constexpr (SecurityChecksOn)
#define debug if constexpr(DebugOn)
#define m_debug if constexpr(MiniDebugOn)
#define debug0 if constexpr(false and DebugOn)
#define err if constexpr(DebugOn) cerr
#define m_err if constexpr(MiniDebugOn) cerr
#define err0 if constexpr(false and DebugOn) cerr
using namespace std;
using int2 = unsigned long long;
using int1 = unsigned long long;
//using pi = pair<int,int>;//second = cost
vector<pair<int1,int1>> zelks[7001];
int1 dp[7001][7001];
//TODO CHANGE THIS INF (ig works)
constexpr int2 inf = 999'999'999'999'999'999;
constexpr int1 small_inf = 999'999'999'999'999'999;//INT32_MAX; //;1'000'000'000;//999'999'999'999'999'999;//1'000'000'000;//999'999'999'999'999'999;//1'000'000'000;//INT32_MAX;

int1 n,k,mod;

vector<pair<int1,int1>> edge[7001];
int2 costs[7001];

bitset<7001> gots(0);
int2 cheapestpathto[7001];
void dijkstra0(int1 gettable){
    int toget=gettable;
    unsigned int alrgot=toget-1;
    for(int i = 0; i <= 7000; i++){
        cheapestpathto[i]=inf;
    }
    gots[0]=1;
    costs[0]=0;
    for(auto k : edge[0]){
        cheapestpathto[k.first]=min(cheapestpathto[k.first], (int2) k.second);
    }
    while(alrgot>0/* or true*/){
        int2 minval=inf;
        int mini=-1;
        for(int i = 0; i < mod; i++){
            if(cheapestpathto[i] < minval and !gots[i]){
                mini=i;
                minval=cheapestpathto[i];
            }
        }
        securityCheck if(mini<0 or mini>mod) {cerr<<"MINI out of range" << (mini==-1) << '\n'; break;}
        gots[mini]=1;
        costs[mini]=minval;
        for(auto k : edge[mini]){
            cheapestpathto[k.first] = min(cheapestpathto[k.first],minval+k.second);
        }
        alrgot--;
    }
    /*int toget=gettable;
    unsigned int alrgot=toget-1;
    for(int1 i = 1; i <= 7000; i++){
        costs[i]=inf;
    }
    priority_queue<pair<int2,int1>, vector<pair<int2, int1>>, std::greater<pair<int2, int1>>> qi;
    for(auto k : edge[0]){
        qi.push({k.second,k.first});
    }
    gots[0]=true;
    while(alrgot>0){
        securityCheck{
            if(qi.size()==0){
                cerr << "SECURITY: PRIORITY QUEUE SIZE=0\n";
                break;
            }
        }
        auto n = qi.top();
        qi.pop();
        if(gots[n.second]) continue;
        gots[n.second]=true;
        alrgot--;
        costs[n.second]=n.first;
        for(auto k : edge[n.second]){
            if(gots[k.first]==0)qi.push({n.first+k.second, k.first});
        }
    }*/
}
int main(){
    speedCheck();
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> k >> mod;
    
    for(int1 i = 0; i < n; i++){
        int1 ki, mi, ci;
        cin >> ki >> mi >> ci;
        zelks[ki].push_back({mi,ci});
    }

    for(int1 i = 0; i <= 7000; i++){
        for(int1 j = 0; j <= 7000; j++){
            dp[i][j] = small_inf;
        }
    }
    dp[0][0]=0;
    m_err << "DP start\n";
    for(int1 i = 1; i <= k; i++){
        int1 prev=i-1;
        //for(int j = 0; j < mod; j++){
        //    dp[i][j]=dp[prev][j];
        //}
        for(auto k : zelks[i]){
            for(int1 j = 0; j < mod; j++){
                dp[i][(j+k.first)%mod]=min(dp[i][(j+k.first)%mod],(dp[prev][j]+k.second));
            }
        }
        debug{
            err << i << ": ";
            for(int1 j = 0; j < mod; j++){
                err << dp[i][j] << ' ';
            }
            err << "\n";
        }
    }
    m_err << "DP end\n";
    //cout << ""
    //dp[k][0, mod-1]=koszt;

    for(int1 i = 0; i < mod; i++){//
        for(int1 j = 0; j < mod; j++){
            if(dp[k][j]<small_inf)edge[i].push_back({(i+j)%mod, dp[k][j]});
        }
    }
    ///int1 gettableto=1;
    /*cout <<"Btw testmode on :)\n";
    for(int1 j = 0; j < mod; j++){
        if(dp[k][j]<inf){
            cout << dp[k][j] << '\n';
            //if(dp[k][j]%3!=0){
            //    cout << "NOOO\n";
            //}
        //}
    //}
    //cout << "finished\n";*/
    int2 posi = mod; //amount of cycles
    //bool ungot=true;
    for(int2 j = 1; j < mod; j++){
        if(dp[k][j]<small_inf){
            /*if(ungot){
                ungot=false;
                posi=dp[k][j];
            }*/
            posi = __gcd(posi, j);
            //gettableto = __gcd(n/gettableto, dp[k][j]);
        }
    }
    int2 gettable = mod/posi;
    //cout << "BTW YOU FORGOR\n SOME VECTS ARE UNGETTABLE-\n";
    m_err << "Dijkstra start\n";
    dijkstra0(gettable);
    m_err << "Dijkstra end\n";
    for(int1 i = 0; i < mod-1; i++){
        cout << ((gots[i]==1) ? to_string(costs[i]) : "-1") << '\n';
    }
    cout << ((gots[mod-1]==1) ? to_string(costs[mod-1]) : "-1") << '\n' << std::flush;

}