#include <ios> #include <functional> #include <cassert> #include <vector> #define REP(i, n) for(int i=0; i<(n); ++i) #define SREP(n) REP(_ ## n, n) #define FOR(i, p, n) for(int i=(p); i<=(n); ++i) #define RFOR(i, p, n) for(int i=(p); i>=(n); --i) #define pb push_back #define eb emplace_back #define C const #define V std::vector #define F std::function #define R std::ranges #define RV std::ranges::views #define sz(A) int(A.size()) #define all(A) A.begin(), A.end() #define rall(A) A.rbegin(), A.rend() typedef long long ll; typedef long double ld; typedef V <int> vi; typedef V <vi> vvi; typedef V <bool> vb; typedef V <char> vc; typedef V <ll> vll; typedef const int ci; typedef const ll cll; int I(){ int z; while ((z=getchar_unlocked())<'0'||z>'9'); int r=z-'0'; while ((z=getchar_unlocked())>='0'&&z<='9') r=r*10+z-'0'; return r; } void M(ll &i, cll j){ if (i>j) i=j; } int main(){ ci n=I(),k=I(),m=I(); cll inf=1e18; vll baz(m, inf); baz[0]=0; { // bazgen V <V <std::pair <int, int>>> kub(k); SREP(n){ ci p=I()-1,r=I()%m,c=I(); kub[p].eb(r, c); } REP(i, k){ static vll nbaz; nbaz.assign(m, inf); for (C auto &[r, c] : kub[i]) REP(a, m) M(nbaz[(a+r)%m], baz[a]+c); std::swap(nbaz, baz); } baz[0]=0; } auto sconv=[&](vll &v) -> void{ static vll r; r.assign(m, inf); REP(i, m){ FOR(j, i, m-i-1) M(r[i+j], v[i]+v[j]); FOR(j, std::max(m-i, i), m-1) M(r[i+j-m], v[i]+v[j]); } std::swap(r, v); }; for (int p=0; (1<<p)<=m; ++p,sconv(baz)); REP(i, m) printf("%lld\n", baz[i]==inf ? -1 : baz[i]); }
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 | #include <ios> #include <functional> #include <cassert> #include <vector> #define REP(i, n) for(int i=0; i<(n); ++i) #define SREP(n) REP(_ ## n, n) #define FOR(i, p, n) for(int i=(p); i<=(n); ++i) #define RFOR(i, p, n) for(int i=(p); i>=(n); --i) #define pb push_back #define eb emplace_back #define C const #define V std::vector #define F std::function #define R std::ranges #define RV std::ranges::views #define sz(A) int(A.size()) #define all(A) A.begin(), A.end() #define rall(A) A.rbegin(), A.rend() typedef long long ll; typedef long double ld; typedef V <int> vi; typedef V <vi> vvi; typedef V <bool> vb; typedef V <char> vc; typedef V <ll> vll; typedef const int ci; typedef const ll cll; int I(){ int z; while ((z=getchar_unlocked())<'0'||z>'9'); int r=z-'0'; while ((z=getchar_unlocked())>='0'&&z<='9') r=r*10+z-'0'; return r; } void M(ll &i, cll j){ if (i>j) i=j; } int main(){ ci n=I(),k=I(),m=I(); cll inf=1e18; vll baz(m, inf); baz[0]=0; { // bazgen V <V <std::pair <int, int>>> kub(k); SREP(n){ ci p=I()-1,r=I()%m,c=I(); kub[p].eb(r, c); } REP(i, k){ static vll nbaz; nbaz.assign(m, inf); for (C auto &[r, c] : kub[i]) REP(a, m) M(nbaz[(a+r)%m], baz[a]+c); std::swap(nbaz, baz); } baz[0]=0; } auto sconv=[&](vll &v) -> void{ static vll r; r.assign(m, inf); REP(i, m){ FOR(j, i, m-i-1) M(r[i+j], v[i]+v[j]); FOR(j, std::max(m-i, i), m-1) M(r[i+j-m], v[i]+v[j]); } std::swap(r, v); }; for (int p=0; (1<<p)<=m; ++p,sconv(baz)); REP(i, m) printf("%lld\n", baz[i]==inf ? -1 : baz[i]); } |