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

using namespace std;

#define ll long long

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;

typedef pair<int,int> ii;

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

const int MOD = 998244353;

ll read(){
    ll i;
    cin>>i;
    return i;
}

vi readvi(int n,int off=0,int shift=0){
    vi v(n+shift);
    rep(i,shift)v[i]=0;
    rep(i,n)v[i+shift]=read()+off;
    return v;
}

void YesNo(bool condition, bool do_exit=true) {
    if (condition)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    if (do_exit)
        exit(0);
}

int main(void ) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k, m;

    cin >> n >> k >> m;


    vvi masa(k);
    vvl cena(k);

    rep(i,n) {
        int ki,mi,ci;
        cin >> ki >> mi >> ci;
        --ki;
        masa[ki].push_back(mi % m);
        cena[ki].push_back(ci);
    }

    const ll MAX_CENA = 1e9;
    const ll INFTY = std::numeric_limits<ll>::max();

    vvl dp (k, vl(m, INFTY));

    // faza 1: obliczamy dp[i][r] == minimalna cena dla kolorów 0..i, o całk masie przystającej do r.

    rep(j,masa[0].size())
        dp[0][masa[0][j]] = min(dp[0][masa[0][j]], cena[0][j]);

    rng(i,1,k)
        rep(r,m)
            if (dp[i-1][r] < INFTY)
                rep(j,masa[i].size())
                    dp[i][(r + masa[i][j]) % m] = min(dp[i][(r + masa[i][j]) % m], dp[i-1][r] + cena[i][j]);

    vl cena_paczki = dp[k-1];
    cena_paczki[0] = 0;

    vector<bool> sprawdz_wiel(m, true);

    rng(i,1,m)
        if (sprawdz_wiel[i] and cena_paczki[i] < INFTY)
            rng(j,2,1+lcm(i,m)/i)
                if (j*cena_paczki[i] < cena_paczki[(i*j)%m]) {
                    cena_paczki[(i * j) % m] = j * cena_paczki[i];
                    sprawdz_wiel[(i * j) % m] = false;
                }

    vvl dp2(m,vl(m, INFTY));
    dp2[0][0] = 0;
    rng(i,1,m) {
        dp2[i] = dp2[i - 1];
        if (cena_paczki[i] < INFTY)
            rng(j, 0, m)
                if (dp2[i - 1][j] < INFTY)
                    dp2[i][(j+i)%m] = min(dp2[i][(j+i)%m], dp2[i - 1][j] + cena_paczki[i]);
    }

    rep(i,m)
        if (dp2[m-1][i] == INFTY)
            cout << -1 << "\n";
        else
            cout << dp2[m-1][i] << "\n";

    return 0;
}