Codeforces 1185G2 Playlist for Polycarp (hard version) 背包,暴力
题意及思路:https://www.cnblogs.com/Als123/p/11061147.html
代码:
#include <bits/stdc++.h> #define LL long long #define INF 0x3f3f3f3f #define db double #define pii pair<int, int> using namespace std; const LL mod = 1e9 + 7; LL mul(LL x, LL y) { return ((LL)x * y) % mod; } LL add(LL x, LL y) { return ((LL)x + y) % mod; } LL dp[55][55][55][3], sum[55][55][55], ssum[3]; LL f[3][55][2505], jc[55], f1[55][55][2505]; vector<int> a[3]; int n, m; LL ans = 0; void init(int x) { jc[0] = 1; for (int i = 1; i <= x; i++) { jc[i] = mul(jc[i - 1], i); } dp[1][0][0][0] = dp[0][1][0][1] = dp[0][0][1][2] = 1; for (int i = 0; i <= x; i++) for (int j = 0; j <= x; j++) for (int k = 0; k <= x; k++) { for (int t = 0; t < 3; t++) { if(t != 0) dp[i + 1][j][k][0] += dp[i][j][k][t]; if(t != 1) dp[i][j + 1][k][1] += dp[i][j][k][t]; if(t != 2) dp[i][j][k + 1][2] += dp[i][j][k][t]; } sum[i][j][k] = mul(mul(jc[i], mul(jc[j], jc[k])), add(add(dp[i][j][k][0], dp[i][j][k][1]), dp[i][j][k][2])); } } void init1() { for (int flag = 0; flag < 3; flag++) { f[flag][0][0] = 1; for (int i = 0; i < a[flag].size(); i++) for (int j = i + 1; j >= 1; j--) for (int k = ssum[flag]; k >= a[flag][i]; k--) f[flag][j][k] = add(f[flag][j][k], f[flag][j - 1][k - a[flag][i]]); } for (int i = 0; i <= a[0].size(); i++) for (int j = 0; j <= a[1].size(); j++) for (int k = 0; k <= ssum[0]; k++) { for (int t = 0; t <= ssum[1]; t++) { f1[i][j][k + t] = add(f1[i][j][k + t], mul(f[0][i][k], f[1][j][t])); } } } void solve() { for (int i = 0; i <= a[2].size(); i++) for (int j = 0; j <= a[0].size(); j++) for (int k = 0; k <= a[1].size(); k++) { for (int t = 0; t <= ssum[2]; t++) { int tmp1 = m - t; ans = add(ans, mul(sum[j][k][i], mul(f1[j][k][tmp1], f[2][i][t]))); } } printf("%lld\n", ans); } int main() { int t, x; init(50); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d%d", &t, &x); a[x - 1].push_back(t); ssum[x - 1] += t; } if(a[0].size() > a[2].size()) { swap(a[0], a[2]); swap(ssum[0], ssum[2]); } if(a[1].size() > a[2].size()) { swap(a[1], a[2]); swap(ssum[1], ssum[2]); } init1(); solve(); }