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

typedef long long int lli;

using namespace std;

#define MAX4 10010
#define MAX5 100010
#define MAX6 1000010

#define FOR(n) for (int i = 0; i < n; i++)

using vti = vector<int>;
using vts = vector<string>;

template <typename T> void dbg(T last) { cout << last << "\n"; }

template <typename T, typename... args> void dbg(T current, args... next) {
  cout << current << ' ';
  dbg(next...);
}

template <typename T> void getInput(vector<T> &v, int n, int x = 0) {
  for (int i = x; i < n; i++) {
    cin >> v[i];
  }
}

//-----------------------------------------------------------------------------------------------//

// struct data {
//   int color, mass, cost;
// };

// vector<data> input;
vector<pair<int, int>> tab[MAX5];
lli dp[MAX5] = {-1};
void solve() {
  int n, k, m;
  cin >> n >> k >> m;

  for (int i = 0; i < n; i++) {
    int color, mass, cost;
    cin >> color >> mass >> cost;
    // input.push_back({color, mass, cost});
    tab[color].push_back({mass, cost});
  }
  vector<lli> oldDp(8000, -1);
  vector<lli> newDp(8000, -1);

  oldDp[0] = 0;

  for (int i = 1; i <= k; i++) {
    for (auto [mass, cost] : tab[i]) {

      for (int j = 0; j <= m; j++) {
        if (oldDp[j] > -1) {
          if (newDp[(j + mass) % m] == -1) {
            // dbg("(1 ", i, (j + mass) % m, j, mass, oldDp[j], cost);
            newDp[(j + mass) % m] = oldDp[j] + cost;
          } else {
            //     dbg("(2 ", i, (j + mass) % m, oldDp[j] + cost);
            newDp[(j + mass) % m] = min(newDp[(j + mass) % m], oldDp[j] + cost);
          }
        }
      }
    }
    swap(oldDp, newDp);
    for (int j = 0; j <= m; j++) {
      newDp[j] = -1;
    }
  }
  for (int i = 1; i <= m; i++) {
    // dbg("* ", i, oldDp[i]);
    dp[i] = LONG_LONG_MAX;
  }
  for (lli i = 1; i <= m; i++) {
    if (oldDp[i] > -1) {
      for (lli j = 1; j <= m; j++) {
        lli newJ = (i * j) % m;
        lli newCost = oldDp[i] * j;
        dp[newJ] = min(dp[newJ], newCost);
      }
    }
  }
  // for (int i = 1; i <= m; i++) {
  //   dbg("- ", i, dp[i]);
  // }
  dp[0] = 0;
  for (lli i = 1; i <= m; i++) {
    // oldDp[i] = dp[i];

    if (dp[i] == LONG_LONG_MAX) {
      continue;
    }
    queue<pair<lli, lli>> q;
    for (lli j = 0; j <= m; j++) {
      if (dp[j] < LONG_LONG_MAX) {
        q.push({j, dp[j]});
      }
    }
    lli mass = i;
    lli cost = dp[i];
    while (!q.empty()) {
      auto [newMass, newCost] = q.front();
      q.pop();
      if (dp[(mass + newMass) % m] == LONG_LONG_MAX ||
          dp[(mass + newMass) % m] > dp[newMass] + cost) {
        //  dbg("& ", (mass + newMass) % m, dp[newMass] + cost);
        dp[(mass + newMass) % m] = dp[newMass] + cost;
        q.push({(mass + newMass) % m, dp[newMass] + cost});
      }
    }
  }
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    if (dp[i] == LONG_LONG_MAX) {
      cout << "-1\n";
    } else {
      cout << dp[i] << "\n";
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);

  solve();

  return 0;
}