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 ...
随机推荐
- Javaweb学习笔记——(八)——————常见系统体系结构,Tomcat,以及web的内部外部应用,http协议概述
·软件系统体系结构: 1.常见软件系统体系结构B/S.C/S C/S 1.C/S结构即客户端/服务器(Client/Server),列如QQ: 2.需要编写服务器端程序,以及客户端程序,列如我们安装的 ...
- rest framework错误笔记——身份验证和权限
按照官网教程(http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/)走到最后一步验证时,命令窗 ...
- <转载>Mac下,使用sshpass让iterm2支持多ssh登录信息保存
windows里有个Xshell非常的方便好使,因为它能保存你所有的ssh登录帐号信息.MAC下并没有xshell,有些也提供这样的功能,但效果都不好.iterm2是很好的终端,但却不能很好的支持多p ...
- POJ 1035 Spell checker (模拟)
题目链接 Description You, as a member of a development team for a new spell checking program, are to wri ...
- 2018-2019-2 网络对抗技术 20165230 Exp4 恶意代码分析
目录 1.实验内容 2.实验过程 任务一:系统运行监控 每隔五分钟记录自己的电脑,并进行分析 安装配置sysinternals里的sysmon工具 任务二:恶意软件分析 静态分析工具 ViruScan ...
- Linux下的换行符\n\r以及txt和word文档的使用
Linux doc WINDOWS下记事本编写的文档和LINUX下VIM或者GEDIT等编写的文档的不同! 例如WINDOWS下编写的SH脚本,放到LINUX下执行可能会出错. 解决方法: 原因是:W ...
- k-means 图像分割
经典的无监督聚类算法,不多说,上代码. import numpy as np import pandas as pd import copy import matplotlib.pyplot as p ...
- 无责任共享 Coursera、Udacity 等课程视频(转载)
转载链接:https://www.zybuluo.com/illuz/note/71868 B站计划:https://www.zybuluo.com/illuz/note/832995#cs基础课程
- 五、regularized线性回归练习(转载)
转载链接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964515.html 前言: 本节主要是练习regularization项的使 ...
- How to Repair GRUB2 When Ubuntu Won’t Boot
Ubuntu and many other Linux distributions use the GRUB2 boot loader. If GRUB2 breaks—for example, if ...