#include "bits/stdc++.h" #define int long long #define pi pair<int, int> #define inf 6e16 using namespace std; int m; void mnoz_2(pi &a){ a = {(a.first * 2)%m, (a.second * 2)}; } void dodaj(pi &a, pi b){ a = {(a.first + b.first)%m, a.second + b.second}; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k >> m; vector<vector<pi>> zielki(k); while(n--) { int typ, masa, cena; cin >> typ >> masa >> cena; zielki[typ-1].push_back({masa, cena}); } vector<int> plecak(m, inf); plecak[0]=0; for(auto kolor : zielki) { vector<int> tmp(m, inf); for(auto zelk : kolor) for(int i=0; i<m; i++) tmp[(i + zelk.first)%m] = min(tmp[(i + zelk.first)%m], plecak[i] + zelk.second); plecak = tmp; } plecak[0]=0; vector<int> nowy_plecak = plecak; for(int i=1; i<m; i++) { if(plecak[i] >= inf) continue; pi curr = {i, plecak[i]}; int pot = 13; while(pot--) { mnoz_2(curr); nowy_plecak[curr.first] = min(nowy_plecak[curr.first], curr.second); } } plecak = nowy_plecak; plecak[0]=0; vector<int> final_plecak(m, inf); final_plecak[0]=0; for(int i=1; i<m; i++) for(int j=0; j<m; j++) if(plecak[i] != inf) final_plecak[(i+j)%m] = min(final_plecak[(i+j)%m], final_plecak[j] + plecak[i]); for(auto res : final_plecak) if(res >= inf) cout << "-1\n"; else cout << res << '\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 | #include "bits/stdc++.h" #define int long long #define pi pair<int, int> #define inf 6e16 using namespace std; int m; void mnoz_2(pi &a){ a = {(a.first * 2)%m, (a.second * 2)}; } void dodaj(pi &a, pi b){ a = {(a.first + b.first)%m, a.second + b.second}; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k >> m; vector<vector<pi>> zielki(k); while(n--) { int typ, masa, cena; cin >> typ >> masa >> cena; zielki[typ-1].push_back({masa, cena}); } vector<int> plecak(m, inf); plecak[0]=0; for(auto kolor : zielki) { vector<int> tmp(m, inf); for(auto zelk : kolor) for(int i=0; i<m; i++) tmp[(i + zelk.first)%m] = min(tmp[(i + zelk.first)%m], plecak[i] + zelk.second); plecak = tmp; } plecak[0]=0; vector<int> nowy_plecak = plecak; for(int i=1; i<m; i++) { if(plecak[i] >= inf) continue; pi curr = {i, plecak[i]}; int pot = 13; while(pot--) { mnoz_2(curr); nowy_plecak[curr.first] = min(nowy_plecak[curr.first], curr.second); } } plecak = nowy_plecak; plecak[0]=0; vector<int> final_plecak(m, inf); final_plecak[0]=0; for(int i=1; i<m; i++) for(int j=0; j<m; j++) if(plecak[i] != inf) final_plecak[(i+j)%m] = min(final_plecak[(i+j)%m], final_plecak[j] + plecak[i]); for(auto res : final_plecak) if(res >= inf) cout << "-1\n"; else cout << res << '\n'; return 0; } |