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 ...
随机推荐
- 获取本机IP地址的小脚本
获取本机私网地址(1个) #!/bin/bash # Author : standby # Date : -- # Description : Get private ip address of lo ...
- ubuntu 18.04下character_set_server设置为utf8
打开/etc/mysql/mysql.conf.d/mysql.cnf添加以下代码: character-set-server = utf8 然后重启mysql即可
- Managers经理/代理形式的数据共享
代理方式实现进程间共享字典和列表形式的数据, 代码块: import os from multiprocessing import Process,Manager def f(d,l,normalLi ...
- vue2.0环境安装
参考网站http://www.open-open.com/lib/view/open1476240930270.html (以上博客vue init webpack-simple 工程名字<工程 ...
- 如何用MoveIt快速搭建机器人运动规划平台?
MoveIt = RobotGo,翻译成中文就是“机器人,走你!”所以,MoveIt的主要就是一款致力于让机器人能够自主运动及其相关技术的软件,它的所有模块都是围绕着运动规划的实现而设计的. 两个月前 ...
- Docker镜像命令
①docker images [Options] 用途:列出本地主机上的镜像 Options说明: -a:列出本地所有的镜像(含中间映像层) -q:只显示镜像ID --digests:显示镜像的摘要信 ...
- SpringBoot注册Servlet、Filter、Listener
SpringBoot默认是以jar包的方式启动嵌入式的Servlet容易来启动SpringBoot的Web应用,没有web.xml文件 因此我们可以使用以下方式来注册Servlet.Filter.Li ...
- python3字符串与文本处理
每个程序都回涉及到文本处理,如拆分字符串.搜索.替换.词法分析等.许多任务都可以通过内建的字符串方法来轻松解决,但更复杂的操作就需要正则表达式来解决. 1.针对任意多的分隔符拆分字符串 In [1]: ...
- springboot系列十、springboot整合redis、多redis数据源配置
一.简介 Redis 的数据库的整合在 java 里面提供的官方工具包:jedis,所以即便你现在使用的是 SpringBoot,那么也继续使用此开发包. 二.redidTemplate操作 在 Sp ...
- Ajax jsonp 跨域请求实例
跨域请求 JSONP的缺点则是:它只支持GET请求而不支持POST等其它类型的HTTP请求:它只支持跨域HTTP请求这种情况,不能解决不同域的两个页面之间如何进行JavaScript调用的问题. $. ...