#include <algorithm> #include <climits> #include <iostream> #include <vector> typedef long long ll; struct candy { int weight; int cost; }; const ll INF = 1e9 * (ll) 1e9; void solve() { int n, k, m; std::cin >> n >> k >> m; std::vector<std::vector<candy>> C(k+1); for (int i = 0; i < n; i++) { int color, weight, cost; std::cin >> color >> weight >> cost; C[color].push_back({weight, cost}); } std::vector<std::vector<ll>> one_of_each(2, std::vector<ll>(m, INF)); one_of_each[0][0] = 0; for (int color = 1; color <= k; color++) { for (int i = 0; i < m; i++) one_of_each[color&1][i] = INF; for (auto [weight, cost] : C[color]) { for (int i = 0; i < m; i++) { int new_weight = (i + weight) % m; if (one_of_each[(color-1)&1][i] != INF) { one_of_each[color&1][new_weight] = std::min(one_of_each[color&1][new_weight], one_of_each[(color-1)&1][i] + cost); } } } } std::vector<ll> multiple(m, INF); multiple[0] = 0; for (int i = 1; i < m; i++) for (int j = 1; j < m; j++) { if (one_of_each[k&1][i] != INF) { multiple[(i * j) % m] = std::min(multiple[(i * j) % m], one_of_each[k&1][i] * j); } } std::vector<std::vector<ll>> result(2, std::vector<ll>(m, INF)); result[0][0] = result[1][0] = 0; for (int i = 1; i < m; i++) { for (int j = 0; j < m; j++) result[i&1][j] = result[(i-1)&1][j]; for (int j = 0; j < m; j++) { int new_weight = (i + j) % m; if (result[(i-1)&1][j] != INF) { result[i&1][new_weight] = std::min(result[i&1][new_weight], result[(i-1)&1][j] + multiple[i]); } } } for (int i = 0; i < m; i++) { ll r = result[(m-1)&1][i]; std::cout << (r == INF ? -1 : r) << '\n'; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); solve(); return 0; }
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 | #include <algorithm> #include <climits> #include <iostream> #include <vector> typedef long long ll; struct candy { int weight; int cost; }; const ll INF = 1e9 * (ll) 1e9; void solve() { int n, k, m; std::cin >> n >> k >> m; std::vector<std::vector<candy>> C(k+1); for (int i = 0; i < n; i++) { int color, weight, cost; std::cin >> color >> weight >> cost; C[color].push_back({weight, cost}); } std::vector<std::vector<ll>> one_of_each(2, std::vector<ll>(m, INF)); one_of_each[0][0] = 0; for (int color = 1; color <= k; color++) { for (int i = 0; i < m; i++) one_of_each[color&1][i] = INF; for (auto [weight, cost] : C[color]) { for (int i = 0; i < m; i++) { int new_weight = (i + weight) % m; if (one_of_each[(color-1)&1][i] != INF) { one_of_each[color&1][new_weight] = std::min(one_of_each[color&1][new_weight], one_of_each[(color-1)&1][i] + cost); } } } } std::vector<ll> multiple(m, INF); multiple[0] = 0; for (int i = 1; i < m; i++) for (int j = 1; j < m; j++) { if (one_of_each[k&1][i] != INF) { multiple[(i * j) % m] = std::min(multiple[(i * j) % m], one_of_each[k&1][i] * j); } } std::vector<std::vector<ll>> result(2, std::vector<ll>(m, INF)); result[0][0] = result[1][0] = 0; for (int i = 1; i < m; i++) { for (int j = 0; j < m; j++) result[i&1][j] = result[(i-1)&1][j]; for (int j = 0; j < m; j++) { int new_weight = (i + j) % m; if (result[(i-1)&1][j] != INF) { result[i&1][new_weight] = std::min(result[i&1][new_weight], result[(i-1)&1][j] + multiple[i]); } } } for (int i = 0; i < m; i++) { ll r = result[(m-1)&1][i]; std::cout << (r == INF ? -1 : r) << '\n'; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); solve(); return 0; } |