#include<bits/stdc++.h> using namespace std; #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<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long #define INF (int)1e18 + (int)7 int n, k, m; vector<int> convolution(vector<int>& v1, vector<int>& v2) { vector<int> result(m, INF); for(int i=0;i<m;i++) for(int j=0;j<m;j++) { int d = i+j; if(d >= m) d-=m; result[d] = min(result[d], v2[i] + v1[j]); } for(int i=0;i<m;i++) result[i] = min({result[i], v1[i], v2[i]}); return result; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>k>>m; vector<int> dp(m, INF); dp[0] = 0; vector<vector<pair<int, int> > > noms(k); for(int i=0;i<n;i++) { int c, g, w; cin>>c>>g>>w; c--; g%=m; noms[c].push_back({g, w}); } for(int i=0;i<k;i++) { vector<int> res(m, INF); for(auto v : noms[i]) for(int j=0;j<m;j++) { int d = j + v.first; if(d >= m) d -= m; res[(j + v.first)%m] = min(res[(j+v.first)%m], v.second+dp[j]); } dp = res; } for(int p=0;p<ceil(log2(m))+1;p++) { dp = convolution(dp, dp); } cout<<0<<"\n"; for(int i=1;i<dp.size();i++) if(dp[i] == INF) cout<<-1<<"\n"; else cout<<dp[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 | #include<bits/stdc++.h> using namespace std; #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<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long #define INF (int)1e18 + (int)7 int n, k, m; vector<int> convolution(vector<int>& v1, vector<int>& v2) { vector<int> result(m, INF); for(int i=0;i<m;i++) for(int j=0;j<m;j++) { int d = i+j; if(d >= m) d-=m; result[d] = min(result[d], v2[i] + v1[j]); } for(int i=0;i<m;i++) result[i] = min({result[i], v1[i], v2[i]}); return result; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>k>>m; vector<int> dp(m, INF); dp[0] = 0; vector<vector<pair<int, int> > > noms(k); for(int i=0;i<n;i++) { int c, g, w; cin>>c>>g>>w; c--; g%=m; noms[c].push_back({g, w}); } for(int i=0;i<k;i++) { vector<int> res(m, INF); for(auto v : noms[i]) for(int j=0;j<m;j++) { int d = j + v.first; if(d >= m) d -= m; res[(j + v.first)%m] = min(res[(j+v.first)%m], v.second+dp[j]); } dp = res; } for(int p=0;p<ceil(log2(m))+1;p++) { dp = convolution(dp, dp); } cout<<0<<"\n"; for(int i=1;i<dp.size();i++) if(dp[i] == INF) cout<<-1<<"\n"; else cout<<dp[i]<<"\n"; } |