//2024-03-13 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; #define f first #define s second #define ll long long const int maxN = 7002, L = 15; const ll inf = 1e18; int n, k, m; int K[maxN], M[maxN], C[maxN]; ll dp[maxN], przed[maxN], po[maxN], ruchy[maxN]; vector <pair<int, int>> kolory [maxN]; int dwa[L]; bool zle = false; void prec () { dwa[0] = 1; for (int i = 1; i < L; i++) { dwa[i] = dwa[i-1]*2; } } ll safemul (ll a, ll b) { if (a > inf/b) return inf; else return a * b; } int main () { ios_base::sync_with_stdio(0); cin.tie(0); prec(); cin >> n >> k >> m; for (int i = 1; i <= n; i++) { cin >> K[i] >> M[i] >> C[i]; kolory[ K[i] ].push_back({M[i], C[i]}); } fill(przed + 1, przed + m, inf); fill(po + 1, po + m, inf); for (int i = 1; i <= k; i++) { if (kolory[i].empty()) { zle = 1; break; } for (int j = 0; j < m; j++) { int pos = (j + kolory[i][0].f); if (pos >= m) pos -= m; po[pos] = min(inf, przed[j] + kolory[i][0].s); } int sz = kolory[i].size(); for (int l = 1; l < sz; l++) { for (int j = 0; j < m; j++) { int pos = (j + kolory[i][l].f); if (pos >= m) pos -= m; po[pos] = min(po[pos], przed[j] + kolory[i][l].s); } } for (int j = 0; j < m; j++) { przed[j] = po[j]; } } if (zle) { for (int i = 0; i < m; i++) { cout << (i ? -1 : 0) << '\n'; } return 0; } fill(ruchy, ruchy+m, inf); for (int i = 1; i < m; i++) { for (int j = 0; j < L; j++) { int pos = (1ll * dwa[j] * i) % m; ll val = safemul(po[i], dwa[j]); ruchy[pos] = min(ruchy[pos], val); } } fill(dp + 1, dp + m, inf); for (int i = 1; i < m; i++) { for (int j = 0; j < m; j++) { int pos = (j + i); if (pos >= m) pos -= m; dp[pos] = min(dp[pos], dp[j] + ruchy[i]); } } for (int i = 0; i < m; i++) { cout << (dp[i] == inf ? -1 : dp[i]) << '\n'; } 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 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 | //2024-03-13 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; #define f first #define s second #define ll long long const int maxN = 7002, L = 15; const ll inf = 1e18; int n, k, m; int K[maxN], M[maxN], C[maxN]; ll dp[maxN], przed[maxN], po[maxN], ruchy[maxN]; vector <pair<int, int>> kolory [maxN]; int dwa[L]; bool zle = false; void prec () { dwa[0] = 1; for (int i = 1; i < L; i++) { dwa[i] = dwa[i-1]*2; } } ll safemul (ll a, ll b) { if (a > inf/b) return inf; else return a * b; } int main () { ios_base::sync_with_stdio(0); cin.tie(0); prec(); cin >> n >> k >> m; for (int i = 1; i <= n; i++) { cin >> K[i] >> M[i] >> C[i]; kolory[ K[i] ].push_back({M[i], C[i]}); } fill(przed + 1, przed + m, inf); fill(po + 1, po + m, inf); for (int i = 1; i <= k; i++) { if (kolory[i].empty()) { zle = 1; break; } for (int j = 0; j < m; j++) { int pos = (j + kolory[i][0].f); if (pos >= m) pos -= m; po[pos] = min(inf, przed[j] + kolory[i][0].s); } int sz = kolory[i].size(); for (int l = 1; l < sz; l++) { for (int j = 0; j < m; j++) { int pos = (j + kolory[i][l].f); if (pos >= m) pos -= m; po[pos] = min(po[pos], przed[j] + kolory[i][l].s); } } for (int j = 0; j < m; j++) { przed[j] = po[j]; } } if (zle) { for (int i = 0; i < m; i++) { cout << (i ? -1 : 0) << '\n'; } return 0; } fill(ruchy, ruchy+m, inf); for (int i = 1; i < m; i++) { for (int j = 0; j < L; j++) { int pos = (1ll * dwa[j] * i) % m; ll val = safemul(po[i], dwa[j]); ruchy[pos] = min(ruchy[pos], val); } } fill(dp + 1, dp + m, inf); for (int i = 1; i < m; i++) { for (int j = 0; j < m; j++) { int pos = (j + i); if (pos >= m) pos -= m; dp[pos] = min(dp[pos], dp[j] + ruchy[i]); } } for (int i = 0; i < m; i++) { cout << (dp[i] == inf ? -1 : dp[i]) << '\n'; } return 0; } |