#include<bits/stdc++.h> #define LL long long #define LLL __int128 #define uint unsigned #define ldb long double #define uLL unsigned long long using namespace std; const int N=7e3+5; const LL INF=1e18; int n,k,m;vector<pair<int,int>>vec[N]; signed main(){ cin.tie(0)->sync_with_stdio(0); cin>>n>>k>>m; for(int i=1;i<=n;++i){ int k,m,c;cin>>k>>m>>c,vec[k].emplace_back(m,c); } vector<LL>f(m,INF);f[0]=0; for(int i=1;i<=k;++i){ vector<LL>g(m,INF); for(auto [w,v]:vec[i]) for(int j=0;j<m;++j) g[(j+w)%m]=min(g[(j+w)%m],f[j]+v); f=g; } vector<LL>g(m,INF);g[0]=0; for(int k=1;k<m;++k)if(f[k]<INF) for(int i=0,t=__gcd(m,k);i<t;++i) for(int j=i,c=0;c<2;c+=(j==i)){ const int p=(j+k)%m;g[p]=min(g[p],g[j]+f[k]),j=p; } for(int i=0;i<m;++i)cout<<(g[i]==INF?-1:g[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 | #include<bits/stdc++.h> #define LL long long #define LLL __int128 #define uint unsigned #define ldb long double #define uLL unsigned long long using namespace std; const int N=7e3+5; const LL INF=1e18; int n,k,m;vector<pair<int,int>>vec[N]; signed main(){ cin.tie(0)->sync_with_stdio(0); cin>>n>>k>>m; for(int i=1;i<=n;++i){ int k,m,c;cin>>k>>m>>c,vec[k].emplace_back(m,c); } vector<LL>f(m,INF);f[0]=0; for(int i=1;i<=k;++i){ vector<LL>g(m,INF); for(auto [w,v]:vec[i]) for(int j=0;j<m;++j) g[(j+w)%m]=min(g[(j+w)%m],f[j]+v); f=g; } vector<LL>g(m,INF);g[0]=0; for(int k=1;k<m;++k)if(f[k]<INF) for(int i=0,t=__gcd(m,k);i<t;++i) for(int j=i,c=0;c<2;c+=(j==i)){ const int p=(j+k)%m;g[p]=min(g[p],g[j]+f[k]),j=p; } for(int i=0;i<m;++i)cout<<(g[i]==INF?-1:g[i])<<'\n'; return 0; } /* */ |