/* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #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 LL inf = (LL) 1e18 + 5; struct DP { int m; vector<LL> new_vals, old_vals; bool saved = true; DP(int _m) { m = _m; new_vals.resize(m, inf); old_vals.resize(m, inf); old_vals[0] = new_vals[0] = 0; } void add(int x, LL v) { REP(i, m) { int j = (i + x) % m; new_vals[j] = min(new_vals[j], old_vals[i] + v); } saved = false; } void save() { if (!saved) { old_vals = new_vals; saved = true; } } void clear() { fill(new_vals.begin(), new_vals.end(), inf); } void process(int x) { LL v = old_vals[x]; int g = __gcd(x, m); REP(i, g) { int curr = i; REP(j, m / g * 2) { int nxt = (curr + x) % m; old_vals[nxt] = min(old_vals[nxt], old_vals[curr] + v); curr = nxt; } } } void prepare() { REP(i, m) { if (old_vals[i] >= inf) { old_vals[i] = -1; } } } }; struct Data { int k, m, c; }; bool operator<(Data &a, Data &b) { return tie(a.k, a.m, a.c) < tie(b.k, b.m, b.c); } bool init_check(int k, vector<Data> &data) { int cnt = 0; vector<bool> used(k); for (auto p : data) { if (!used[p.k-1]) { cnt++; } used[p.k-1] = true; } return cnt == k; } int main() { cin.tie(0)->sync_with_stdio(0); int n, k, m; cin >> n >> k >> m; vector<Data> data(n); for (auto &d : data) { cin >> d.k >> d.m >> d.c; } if (!init_check(k, data)) { cout << 0 << "\n"; REP(i, m-1) { cout << -1 << "\n"; } return 0; } sort(data.begin(), data.end()); int prv = 0; DP singles(m); for (auto &d : data) { if (d.k != prv) { singles.save(); singles.clear(); prv = d.k; } singles.add(d.m, d.c); } singles.save(); singles.old_vals[0] = 0; debug(singles.old_vals); FOR(i, 1, m-1) { singles.process(i); debug(i, singles.old_vals); } singles.prepare(); REP(i, m) { cout << singles.old_vals[i] << "\n"; } }
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 | /* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #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 LL inf = (LL) 1e18 + 5; struct DP { int m; vector<LL> new_vals, old_vals; bool saved = true; DP(int _m) { m = _m; new_vals.resize(m, inf); old_vals.resize(m, inf); old_vals[0] = new_vals[0] = 0; } void add(int x, LL v) { REP(i, m) { int j = (i + x) % m; new_vals[j] = min(new_vals[j], old_vals[i] + v); } saved = false; } void save() { if (!saved) { old_vals = new_vals; saved = true; } } void clear() { fill(new_vals.begin(), new_vals.end(), inf); } void process(int x) { LL v = old_vals[x]; int g = __gcd(x, m); REP(i, g) { int curr = i; REP(j, m / g * 2) { int nxt = (curr + x) % m; old_vals[nxt] = min(old_vals[nxt], old_vals[curr] + v); curr = nxt; } } } void prepare() { REP(i, m) { if (old_vals[i] >= inf) { old_vals[i] = -1; } } } }; struct Data { int k, m, c; }; bool operator<(Data &a, Data &b) { return tie(a.k, a.m, a.c) < tie(b.k, b.m, b.c); } bool init_check(int k, vector<Data> &data) { int cnt = 0; vector<bool> used(k); for (auto p : data) { if (!used[p.k-1]) { cnt++; } used[p.k-1] = true; } return cnt == k; } int main() { cin.tie(0)->sync_with_stdio(0); int n, k, m; cin >> n >> k >> m; vector<Data> data(n); for (auto &d : data) { cin >> d.k >> d.m >> d.c; } if (!init_check(k, data)) { cout << 0 << "\n"; REP(i, m-1) { cout << -1 << "\n"; } return 0; } sort(data.begin(), data.end()); int prv = 0; DP singles(m); for (auto &d : data) { if (d.k != prv) { singles.save(); singles.clear(); prv = d.k; } singles.add(d.m, d.c); } singles.save(); singles.old_vals[0] = 0; debug(singles.old_vals); FOR(i, 1, m-1) { singles.process(i); debug(i, singles.old_vals); } singles.prepare(); REP(i, m) { cout << singles.old_vals[i] << "\n"; } } |