最关键的一点就是怎么判方案是否存在。。 只要存在若干个坦克之和的sum % k == v % k 就有解, 否则无解。
我怎么想不到呢。。。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 5000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, k, v, sum, now1, now2, a[N]; bool dp[N][N]; bool f[N][N]; vector<int> vc[2]; int main() { scanf("%d%d%d", &n, &k, &v); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += a[i]; } if(sum < v) return puts("NO"), 0; dp[0][0] = true; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { if(!dp[i][j]) continue; dp[i + 1][(j + a[i + 1]) % k] = true; f[i + 1][(j + a[i + 1]) % k] = true; dp[i + 1][j] = true; f[i + 1][j] = false; } } if(!dp[n][v % k]) return puts("NO"), 0; int tmp = v % k; for(int i = n; i >= 1; i--) { if(f[i][tmp]) { vc[0].push_back(i); tmp = ((tmp - a[i]) % k + k) % k; } else { vc[1].push_back(i); } } sort(ALL(vc[0])); sort(ALL(vc[1])); for(auto& t : vc[0]) now1 += a[t]; now2 = sum - now1; puts("YES"); for(int i = 1; i < SZ(vc[0]); i++) printf("100000 %d %d ", vc[0][i], vc[0][0]); for(int i = 1; i < SZ(vc[1]); i++) printf("100000 %d %d ", vc[1][i], vc[1][0]); if(!SZ(vc[1])) vc[1].push_back(vc[0].back()); if(!SZ(vc[0])) vc[0].push_back(vc[1].back()); if(now1 < v) printf("%d %d %d ", (v - now1) / k, vc[1][0], vc[0][0]); if(now1 > v) printf("%d %d %d ", (now1 - v) / k, vc[0][0], vc[1][0]); return 0; } /* */