//Jakub Nowak XIV LO Wroclaw #include<bits/stdc++.h> using namespace std; #define boost ios_base::sync_with_stdio(false); cin.tie(0); #define int long long #define vi vector<int> #define pii pair<int,int> #define pb push_back #define st first #define nd second const int inf = (int)(1e18)+7; /*void wypisz(auto &X) { for(auto &x : X) cout << x << " "; cout << "\n"; }*/ void solve() { int n, k, m; vector<vi> DP(1, vi(1, 0));//DP[kolor][reszta] = minimalna cena kupna paska o kolorach od 1 do kolor, który daje reszte reszta vector<vector<pii>> ZEL(1, vector<pii>(0)); vi ANS(0); cin >> n >> k >> m; ZEL = vector<vector<pii>>(k+1, vector<pii>(0)); for(int i=1; i <= n; i++) { int k_i, m_i, c_i; cin >> k_i >> m_i >> c_i; ZEL[k_i].pb({m_i, c_i}); } {//DP DP[0] = vi(m, -1); DP[0][0] = 0; for(int i=1; i<=k; i++) { DP.pb(vi(m, -1)); for(auto &u : ZEL[i]) { for(int j=0; j<=m-1; j++) { int &stary = DP[DP.size()-2][(j-u.st+m)%m]; int &nowy = DP.back()[j]; if(stary == -1) continue; if(nowy == -1 || nowy > stary + u.nd) nowy = stary + u.nd; //DP.back()[j] = min(DP.back()[j], DP[DP.size()-2][(j-u.st+m)%m]+u.nd);//co gdy -1 } } } } /*//for(auto &u : ZEL) for(auto &v : u) cout << v.st << "|" << v.nd << " "; for(int i=0; i<ZEL.size(); i++) { cout << i << ": "; for(auto &v : ZEL[i]) cout << v.st << "|" << v.nd << " "; cout << "\n"; } cout << "\n"; for(auto &u:DP) wypisz(u); return;*/ //wypisz(DP.back()); {//Wydawanie reszty /*function<int(int)> next = [&](int x) { return (x+r)%m; }*/ ANS = vi(m, -1); ANS[0] = 0; for(int r = 1; r<m; r++) {//i to reszta mod m if(DP.back()[r] == -1) continue; /*int x = i;//pozycja startowa? while(x != 0) {// ANS[x] = min(ANS[x], ANS[(x-i+m)%m] + DP.back()[i]); x += i; }*/ vi BEEN(m, 0); /*function(void(int)) update = [&](int &x) { if(next(x)) }*/ //#define next(x) ((x+r)%m) for(int i=0; i<m; i++) { if(BEEN[i] != 0) continue; for(int x = i; BEEN[x] != 2; x = (x+r)%m) { //update(x); BEEN[x]++; int &stary = ANS[(x-r+m)%m]; int &nowy = ANS[x]; int &koszt = DP.back()[r]; if(stary == -1) continue; if(nowy == -1 || nowy > stary + koszt) nowy = stary + koszt; } } //wypisz(ANS); //#undef next(x) } } for(int i=0; i<m; i++) cout << ANS[i] << "\n"; } int32_t main() { boost //cout << inf; 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 | //Jakub Nowak XIV LO Wroclaw #include<bits/stdc++.h> using namespace std; #define boost ios_base::sync_with_stdio(false); cin.tie(0); #define int long long #define vi vector<int> #define pii pair<int,int> #define pb push_back #define st first #define nd second const int inf = (int)(1e18)+7; /*void wypisz(auto &X) { for(auto &x : X) cout << x << " "; cout << "\n"; }*/ void solve() { int n, k, m; vector<vi> DP(1, vi(1, 0));//DP[kolor][reszta] = minimalna cena kupna paska o kolorach od 1 do kolor, który daje reszte reszta vector<vector<pii>> ZEL(1, vector<pii>(0)); vi ANS(0); cin >> n >> k >> m; ZEL = vector<vector<pii>>(k+1, vector<pii>(0)); for(int i=1; i <= n; i++) { int k_i, m_i, c_i; cin >> k_i >> m_i >> c_i; ZEL[k_i].pb({m_i, c_i}); } {//DP DP[0] = vi(m, -1); DP[0][0] = 0; for(int i=1; i<=k; i++) { DP.pb(vi(m, -1)); for(auto &u : ZEL[i]) { for(int j=0; j<=m-1; j++) { int &stary = DP[DP.size()-2][(j-u.st+m)%m]; int &nowy = DP.back()[j]; if(stary == -1) continue; if(nowy == -1 || nowy > stary + u.nd) nowy = stary + u.nd; //DP.back()[j] = min(DP.back()[j], DP[DP.size()-2][(j-u.st+m)%m]+u.nd);//co gdy -1 } } } } /*//for(auto &u : ZEL) for(auto &v : u) cout << v.st << "|" << v.nd << " "; for(int i=0; i<ZEL.size(); i++) { cout << i << ": "; for(auto &v : ZEL[i]) cout << v.st << "|" << v.nd << " "; cout << "\n"; } cout << "\n"; for(auto &u:DP) wypisz(u); return;*/ //wypisz(DP.back()); {//Wydawanie reszty /*function<int(int)> next = [&](int x) { return (x+r)%m; }*/ ANS = vi(m, -1); ANS[0] = 0; for(int r = 1; r<m; r++) {//i to reszta mod m if(DP.back()[r] == -1) continue; /*int x = i;//pozycja startowa? while(x != 0) {// ANS[x] = min(ANS[x], ANS[(x-i+m)%m] + DP.back()[i]); x += i; }*/ vi BEEN(m, 0); /*function(void(int)) update = [&](int &x) { if(next(x)) }*/ //#define next(x) ((x+r)%m) for(int i=0; i<m; i++) { if(BEEN[i] != 0) continue; for(int x = i; BEEN[x] != 2; x = (x+r)%m) { //update(x); BEEN[x]++; int &stary = ANS[(x-r+m)%m]; int &nowy = ANS[x]; int &koszt = DP.back()[r]; if(stary == -1) continue; if(nowy == -1 || nowy > stary + koszt) nowy = stary + koszt; } } //wypisz(ANS); //#undef next(x) } } for(int i=0; i<m; i++) cout << ANS[i] << "\n"; } int32_t main() { boost //cout << inf; solve(); } |