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 = + ;
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 ;
} /*
*/
Codeforces 295C Greg and Friends BFS的更多相关文章
- codeforces 295C Greg and Friends(BFS+DP)
One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...
- Codeforces 295C Greg and Friends
BFS+DP.dp[i][j][0]表示有i个50kg,j个100kg的人在左岸,dp[i][j][1]表示有i个50kg,j个100kg的人在右岸.用BFS求最短路的时候记录到达该状态的可能情况. ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
- codeforces 1283D. Christmas Trees(bfs)
链接: https://codeforces.com/contest/1283/problem/D 题意:给定n个不同的整数点,让你找m个不同的整数点,使得这m个点到到这n个点最小距离之和最小. 思路 ...
- codeforces Gargari and Permutations(DAG+BFS)
/* 题意:求出多个全排列的lcs! 思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边! 最后用bfs遍历整个图, ...
- CodeForces 689B Mike and Shortcuts (BFS or 最短路)
题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- 1.springboot:入门程序
一.Spring Boot 简介 官网英文: Spring Boot makes it easy to create stand-alone, production-grade Spring base ...
- Linux - 文件操作
touch file # 创建空白文件 rm -rf 目录名 # 不提示删除非空目录(-r:递归删除 -f强制) dos2unix # windows文本转linux文本 unix2dos # lin ...
- [C++]指针与引用(定义辨析)
1.定义: 1.1 &-----取地址运算符 功能:返变量的内存地址 Eg:int *p,m; 定义p为指向int类型变量的指针,同时定义变量m ...
- (原创)高仿360云盘android端的UI实现
前些日子几大互联网巨头展开了一轮网盘空间大战.一下子从G级别提高到了T级别.以后谁的空间没有1T估计都不好意思开口了~~~ 试用了一下360云盘的客户端,比较小清新(不是给360打广告~~~).刚好U ...
- Jquery对当前日期的操作(格式化当前日期)
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- 用Quartz 2D画小黄人
第一步: 先创建一个OneView类,并在storyboard里边拖拽一个UIview,将这个UIview的类改成OneView.如图所示: 第二步: 在新创建的Oneview里,补齐下列代码: // ...
- OGG实现两台Oracle数据库的同步
今天通过最简单的一个例子,给大家讲解下 goldengate 实现两台Oracle数据库的同步.内容如下:1.配置数据库信息.2.安装golden gate.3.配置golden gate.4.测试同 ...
- c++动态库封装及调用(3、windows下动态库调用)
1.DLL的隐式调用 隐式链接采用静态加载的方式,比较简单,需要.h..lib..dll三件套.新建“控制台应用程序”或“空项目”.配置如下: 项目->属性->配置属性->VC++ ...
- 每天一个linux命令【转】
转自:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每 ...
- windows环境变量PATH顺序的重要性
PATH是路径的意思,PATH环境变量中存放的值,就是一连串的路径.不同的路径之间,用英文的分号间隔开.系统在执行用户命令时,若用户未给出绝对路径,则首先在当前目录下寻找相应的可执行文件.批处理文件等 ...