Codeforces 295C Greg and Friends BFS
BFS的过程中维护一下方案数。 我个人感觉不是很好想, 但是写出来之后怎么感觉这题这么SB啊啊。
#include<bits/stdc++.h> #define LL 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 ull unsigned long long using namespace std; const int N = 50 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); int n, k, cnt[2]; int dp[N][N][2]; LL comb[N][N]; LL ans[N][N][2]; int main() { for(int i = 0; i < N; i++) for(int j = comb[i][0] = 1; j <= i; j++) comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % mod; scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) { int x; scanf("%d", &x); if(x == 50) cnt[0]++; else cnt[1]++; } memset(dp, -1, sizeof(dp)); queue<pair<PII, int>> que; que.push(mk(mk(0, 0), 0)); ans[0][0][0] = 1; dp[0][0][0] = 0; while(!que.empty()) { int x = que.front().fi.fi; int y = que.front().fi.se; int op = que.front().se; que.pop(); if(op) { for(int i = 0; i <= min(k / 50, x); i++) { int up = min(y, (k - i * 50) / 100); for(int j = 0; j <= up; j++) { if(!i && !j) continue; if(dp[x - i][y - j][0] == -1 || dp[x - i][y - j][0] == dp[x][y][1] + 1) { if(dp[x - i][y - j][0] == -1) que.push(mk(mk(x - i, y - j), op ^ 1)); dp[x - i][y - j][0] = dp[x][y][1] + 1; ans[x - i][y - j][0] = (ans[x - i][y - j][0] + ans[x][y][1] * comb[x][i] % mod * comb[y][j] % mod) % mod; } } } } else { for(int i = 0; i <= min(k / 50, cnt[0] - x); i++) { int up = min(cnt[1] - y, (k - i * 50) / 100); for(int j = 0; j <= up; j++) { if(!i && !j) continue; if(dp[x + i][y + j][1] == -1 || dp[x + i][y + j][1] == dp[x][y][0] + 1) { if(dp[x + i][y + j][1] == -1) que.push(mk(mk(x + i, y + j), op ^ 1)); dp[x + i][y + j][1] = dp[x][y][0] + 1; ans[x + i][y + j][1] = (ans[x + i][y + j][1] + ans[x][y][0] * comb[cnt[0]-x][i] % mod * comb[cnt[1]-y][j] % mod) % mod; } } } } } if(dp[cnt[0]][cnt[1]][1] == -1) { puts("-1"); puts("0"); } else { printf("%d\n", dp[cnt[0]][cnt[1]][1]); printf("%lld\n", ans[cnt[0]][cnt[1]][1]); } return 0; } /* */