#include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define ssize(x) int(x.size()) #define pb push_back #define fi first #define se second #define ld long double typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair <ld, ld> pd; using LL=long long; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "()" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif const ll INF = 1e17; const int MAXM = 7007; ll dp[MAXM][2]; ll res[MAXM], oneuse[MAXM], twouse[MAXM]; vector<pii> in_colors[MAXM], colors[MAXM]; bool visited[MAXM]; /// BLESS RNG void solve() { int N, K, M; cin >> N >> K >> M; auto no = [&] { for (int i = 0; i < M; i++) { cout << -1 + (i == 0) << '\n'; } exit(0); }; REP(i, N) { int k, m, c; cin >> k >> m >> c; in_colors[k].pb({m, c}); } for (int i = 1; i <= K; i++) { if (in_colors[i].empty()) { no(); } sort(all(in_colors[i])); colors[i].pb(in_colors[i][0]); for (int j = 1; j < in_colors[i].size(); j++) { if (in_colors[i][j].fi != in_colors[i][j - 1].fi) { colors[i].pb(in_colors[i][j]); } } } REP(i, M) { dp[i][0] = dp[i][1] = INF; res[i] = INF; oneuse[i] = INF; } res[0] = 0; for (auto item : colors[1]) { int cur_mod = item.fi % M; dp[cur_mod][0] = min(dp[cur_mod][0], (ll)item.se); } for (int i = 2; i <= K; i++) { for (auto item : colors[i]) { for (int j = 0; j < M; j++) { int cur_mod = (j + item.fi) % M; dp[cur_mod][1] = min(dp[cur_mod][1], dp[j][0] + item.se); } } for (auto j = 0; j < M; j++) { dp[j][0] = dp[j][1]; dp[j][1] = INF; } } for (int i = 0; i < M; i++) { res[i] = min(res[i], dp[i][0]); oneuse[i] = dp[i][0]; twouse[i] = oneuse[i]; } if (K <= 10) { int iterations = min(20, ((245000000 / M) / M)); int lowebound = 7; for (int num_items = 2; num_items <= max(lowebound, iterations); num_items++) { for (int i = 0; i < M; i++) { for (int j = 0; j < M; j++) { int cur_mod = (j + i) % M; dp[cur_mod][1] = min(dp[cur_mod][1], dp[j][0] + oneuse[i]); } } for (auto j = 0; j < M; j++) { dp[j][0] = dp[j][1]; res[j] = min(res[j], dp[j][0]); dp[j][1] = INF; } } for (int i = 0; i < M; i++) { res[i] = min(res[i], dp[i][0]); dp[i][0] = oneuse[i]; } } for (int i = 1; i < M; i++) { int cur_mod = i; ll cur_cost = oneuse[i]; if (cur_cost == INF) continue; fill(visited, visited + M, false); while (!visited[cur_mod]) { visited[cur_mod] = true; twouse[cur_mod] = min(twouse[cur_mod], cur_cost); cur_mod = (cur_mod + i) % M; cur_cost += oneuse[i]; } } for (int i = 0; i < M; i++) { res[i] = min(res[i], twouse[i]); dp[i][0] = twouse[i]; } for (int i = 0; i < M; i++) { if (twouse[i] == INF) continue; for (int j = 0; j < M; j++) { int cur_mod = (j + i) % M; dp[cur_mod][1] = min(dp[cur_mod][1], dp[j][0] + twouse[i]); } } for (int j = 0; j < M; j++) { res[j] = min(res[j], dp[j][1]); } for (int j = 0; j < M; j++) { cout << (res[j] == INF ? -1 : res[j]) << '\n'; } } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while(t--) { solve(); } }
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 181 182 183 184 185 186 187 | #include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define ssize(x) int(x.size()) #define pb push_back #define fi first #define se second #define ld long double typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair <ld, ld> pd; using LL=long long; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "()" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif const ll INF = 1e17; const int MAXM = 7007; ll dp[MAXM][2]; ll res[MAXM], oneuse[MAXM], twouse[MAXM]; vector<pii> in_colors[MAXM], colors[MAXM]; bool visited[MAXM]; /// BLESS RNG void solve() { int N, K, M; cin >> N >> K >> M; auto no = [&] { for (int i = 0; i < M; i++) { cout << -1 + (i == 0) << '\n'; } exit(0); }; REP(i, N) { int k, m, c; cin >> k >> m >> c; in_colors[k].pb({m, c}); } for (int i = 1; i <= K; i++) { if (in_colors[i].empty()) { no(); } sort(all(in_colors[i])); colors[i].pb(in_colors[i][0]); for (int j = 1; j < in_colors[i].size(); j++) { if (in_colors[i][j].fi != in_colors[i][j - 1].fi) { colors[i].pb(in_colors[i][j]); } } } REP(i, M) { dp[i][0] = dp[i][1] = INF; res[i] = INF; oneuse[i] = INF; } res[0] = 0; for (auto item : colors[1]) { int cur_mod = item.fi % M; dp[cur_mod][0] = min(dp[cur_mod][0], (ll)item.se); } for (int i = 2; i <= K; i++) { for (auto item : colors[i]) { for (int j = 0; j < M; j++) { int cur_mod = (j + item.fi) % M; dp[cur_mod][1] = min(dp[cur_mod][1], dp[j][0] + item.se); } } for (auto j = 0; j < M; j++) { dp[j][0] = dp[j][1]; dp[j][1] = INF; } } for (int i = 0; i < M; i++) { res[i] = min(res[i], dp[i][0]); oneuse[i] = dp[i][0]; twouse[i] = oneuse[i]; } if (K <= 10) { int iterations = min(20, ((245000000 / M) / M)); int lowebound = 7; for (int num_items = 2; num_items <= max(lowebound, iterations); num_items++) { for (int i = 0; i < M; i++) { for (int j = 0; j < M; j++) { int cur_mod = (j + i) % M; dp[cur_mod][1] = min(dp[cur_mod][1], dp[j][0] + oneuse[i]); } } for (auto j = 0; j < M; j++) { dp[j][0] = dp[j][1]; res[j] = min(res[j], dp[j][0]); dp[j][1] = INF; } } for (int i = 0; i < M; i++) { res[i] = min(res[i], dp[i][0]); dp[i][0] = oneuse[i]; } } for (int i = 1; i < M; i++) { int cur_mod = i; ll cur_cost = oneuse[i]; if (cur_cost == INF) continue; fill(visited, visited + M, false); while (!visited[cur_mod]) { visited[cur_mod] = true; twouse[cur_mod] = min(twouse[cur_mod], cur_cost); cur_mod = (cur_mod + i) % M; cur_cost += oneuse[i]; } } for (int i = 0; i < M; i++) { res[i] = min(res[i], twouse[i]); dp[i][0] = twouse[i]; } for (int i = 0; i < M; i++) { if (twouse[i] == INF) continue; for (int j = 0; j < M; j++) { int cur_mod = (j + i) % M; dp[cur_mod][1] = min(dp[cur_mod][1], dp[j][0] + twouse[i]); } } for (int j = 0; j < M; j++) { res[j] = min(res[j], dp[j][1]); } for (int j = 0; j < M; j++) { cout << (res[j] == INF ? -1 : res[j]) << '\n'; } } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while(t--) { solve(); } } |