Codeforces 295C Greg and Friends BFS

Greg and Friends

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 = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, k, cnt[];
int dp[N][N][];
LL comb[N][N];
LL ans[N][N][]; int main() {
for(int i = ; i < N; i++)
for(int j = comb[i][] = ; j <= i; j++)
comb[i][j] = (comb[i - ][j - ] + comb[i - ][j]) % mod;
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) {
int x; scanf("%d", &x);
if(x == ) cnt[]++;
else cnt[]++;
}
memset(dp, -, sizeof(dp));
queue<pair<PII, int>> que;
que.push(mk(mk(, ), ));
ans[][][] = ;
dp[][][] = ;
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 = ; i <= min(k / , x); i++) {
int up = min(y, (k - i * ) / );
for(int j = ; j <= up; j++) {
if(!i && !j) continue;
if(dp[x - i][y - j][] == - || dp[x - i][y - j][] == dp[x][y][] + ) {
if(dp[x - i][y - j][] == -) que.push(mk(mk(x - i, y - j), op ^ ));
dp[x - i][y - j][] = dp[x][y][] + ;
ans[x - i][y - j][] = (ans[x - i][y - j][] + ans[x][y][] * comb[x][i] % mod * comb[y][j] % mod) % mod;
}
}
}
} else {
for(int i = ; i <= min(k / , cnt[] - x); i++) {
int up = min(cnt[] - y, (k - i * ) / );
for(int j = ; j <= up; j++) {
if(!i && !j) continue;
if(dp[x + i][y + j][] == - || dp[x + i][y + j][] == dp[x][y][] + ) {
if(dp[x + i][y + j][] == -) que.push(mk(mk(x + i, y + j), op ^ ));
dp[x + i][y + j][] = dp[x][y][] + ;
ans[x + i][y + j][] = (ans[x + i][y + j][] + ans[x][y][] * comb[cnt[]-x][i] % mod * comb[cnt[]-y][j] % mod) % mod;
}
}
}
}
}
if(dp[cnt[]][cnt[]][] == -) {
puts("-1");
puts("");
} else {
printf("%d\n", dp[cnt[]][cnt[]][]);
printf("%lld\n", ans[cnt[]][cnt[]][]);
}
return ;
} /*
*/
上一篇:[leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)


下一篇:树莓派无法连接csi摄像头的问题分析(cm4)