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
#include <bits/stdc++.h>
using namespace std;

#define pil pair<int, long long>
#define ll long long

vector<pil> color[7007];

bool pos[7007][7007];
ll cost[7007][7007];

ll iterations(int m) {
    ll it = 0;
    for (int r = 1; r < m; r++) {
        // cout << r << ": " << m / gcd(r, m) << "\n";
        it += (ll)(m / gcd(r, m));
    }
    return it;
}

ll dp[7007];

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    // pil it = {0, 0};
    // for (int m = 1; m <= 7000; m++)
    //     it = max(it, {iterations(m), m});
    // cout << it.first << " " << it.second << "\n";

    // return 0;

    int N, K, M;
    cin >> N >> K >> M;

    for (int i = 0; i < N; i++) {
        int k, m;
        ll c;
        cin >> k >> m >> c;
        color[k].push_back({m, c});
    }

    pos[0][0] = true;
    int maks = 0;
    for (int i = 1; i <= K; i++) { // prefiks użytych kolorów
        int j = maks;
        maks = -1;
        while (j >= 0) {
            if (pos[i - 1][j]) {
                for (const auto& [m, c] : color[i]) {
                    if (!pos[i][(j + m) % M]) {
                        maks = max(maks, (j + m) % M);
                        pos[i][(j + m) % M] = true;
                        cost[i][(j + m) % M] = cost[i - 1][j] + c;
                    }
                    else {
                        cost[i][(j + m) % M] = min(cost[i][(j + m) % M], cost[i - 1][j] + c);
                    }
                }
            }            
            j--;
        }
    }

    // cost[K] - składniki. Każdego używamy max M razy

    for (int i = 1; i < M; i++) {
        dp[i] = -1;
    }
    int max_mul = 0;
    for (int i = 1; i < M; i++) { // reszta
        if (pos[K][i]) {
            ll c = cost[K][i];
            for (int j = i; j != 0; j = (j + i) % M) {
                if (dp[j] == -1) {
                    dp[j] = c;
                    max_mul = max(max_mul, j);
                }
                else {
                    dp[j] = min(dp[j], c);
                }
                c += cost[K][i];
            }
        }
    }

    int last = max_mul;
    for (int i = 1; i <= max_mul; i++) {
        if (dp[i] != -1) {
            for (int j = last; j >= 0; j--) {
                if (dp[j] != -1) {
                    if (dp[(i + j) % M] == -1) {
                        dp[(i + j) % M] = dp[i] + dp[j];
                        last = max(last, (i + j) % M);
                    }
                    else {
                        dp[(i + j) % M] = min(dp[(i + j) % M], dp[i] + dp[j]);
                    }
                }
            }
        }
    }

    for (int i = 0; i < M; i++) {
        cout << dp[i] << "\n";
    }
}