果然讨论有助于发现问题, 上面开始的排序写错了 重新改了如下 #include<bits/stdc++.h> using namespace std; int main(){ int m, n, v, t; while (cin >> v >> n) { vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> m; vector<int> f(n); for (int i = 0; i < m; i++) { cin >> t; f[i] = a[t - 1]; a[t - 1] = -1; } for (int i = 0, j = m; j < n; i++) { if (a[i] > 0) { f[j++] = a[i]; } } a = move(f); vector<vector<int> > dp(v / a[0] + 1, vector<int>(v + 1, 0)); int res = 0; // 先挑选最喜欢的零食, 无约束 for (int i = 0; i < v / a[0] + 1; i++) { dp[i][i * a[0]] = 1; } // 按喜欢程度依次挑选其他特别喜欢的零食, for(int i = 1; i < m; i++) { vector<vector<int>> t = move(dp); int size = v / a[i] + 1; dp = vector<vector<int> >(size, vector<int>(v + 1, 0)); for (int j = 1; j < t.size(); j++) { // 每次取的要比前一种要少 for (int k = min(size - 1, j - 1); k >= 0; k--) { for (int l = k * a[i]; l <= v; l++){ dp[k][l] = (dp[k][l] + t[j][l - k * a[i]]) % 10000007; } } } } // 统计各花费的方案数 vector<int> cur(v + 1, 0); for (int i = 0; i <= v; i++) { for (int j = 0; j < dp.size(); j++) { cur[i] = (cur[i] + dp[j][i]) % 10000007; } } // 按完全背包更新其余零食 for (int i = m; i < n; i++) { for (int j = a[i]; j <= v; j++) { cur[j] = (cur[j] + cur[j - a[i]]) % 10000007; } } cout << cur[v] << endl; } return 0; }
点赞 评论

相关推荐

努力学习的小绵羊:我反倒觉得这种挺好的,给不到我想要的就别浪费大家时间了
点赞 评论 收藏
分享
牛客网
牛客企业服务