Codeforces 295C Greg and Friends
BFS+DP.dp[i][j][0]表示有i个50kg,j个100kg的人在左岸,dp[i][j][1]表示有i个50kg,j个100kg的人在右岸。用BFS求最短路的时候记录到达该状态的可能情况。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; typedef long long LL;
#define maxn 55
#define INF 0xffffffff
const LL mod = ; struct Point{
int f, t, s;
Point(){}
Point(int _f, int _t, int _s):
f(_f), t(_t), s(_s){}
}; LL dp[maxn][maxn][];
LL C[maxn][maxn];
int hash[maxn][maxn][]; int bfs(int nf, int nt, int cap){
queue<Point> Q;
dp[nf][nt][] = ;
hash[nf][nt][] = ;
Q.push(Point(nf, nt, ));
while(!Q.empty()){
Point c = Q.front();Q.pop();
int cf = c.f, ct = c.t, cs = c.s;
for(int i = ; i <= c.f; i ++){
if(i* > cap) break;
for(int j = ; j <= c.t; j ++){
if(i* + j* > cap) continue;
if(i* + j* <= ) continue; int lf = c.f - i, lt = c.t - j;
if(hash[nf - lf][nt - lt][!cs]==INF){
hash[nf - lf][nt - lt][!cs] = hash[cf][ct][cs] + ;
LL tmp = C[cf][i]*C[ct][j]%mod;
dp[nf - lf][nt - lt][!cs] += (tmp*dp[cf][ct][cs]%mod);
dp[nf - lf][nt - lt][!cs]%=mod;
Q.push(Point(nf-lf, nt-lt, !cs));
}
else if(hash[nf - lf][nt - lt][!cs]==hash[cf][ct][cs] + ){
LL tmp = C[cf][i]*C[ct][j]%mod;
dp[nf - lf][nt - lt][!cs] += (tmp*dp[cf][ct][cs]%mod);
dp[nf - lf][nt - lt][!cs]%=mod;
}
}
}
}
return hash[nf][nt][];
} void init(){
C[][] = ;
for(int i = ; i <= ; i ++){
C[i][] = ;
for(int j = ; j <= i; j ++){
C[i][j] = (C[i-][j-] + C[i-][j])%mod;
}
}
} int main()
{
init();
//freopen("test.in", "r", stdin);
for(int n, k, nf, nt; scanf("%d%d", &n, &k)!=EOF; ){
memset(dp, , sizeof(dp));
memset(hash, 0xff, sizeof(hash));
nf = nt = ;
for(int i = , x; i < n; i ++){
scanf("%d", &x);
if(x==) nf ++;
else nt ++;
}
bfs(nf, nt, k);
printf("%d\n%I64d\n", hash[nf][nt][], dp[nf][nt][]);
}
return ;
}
Codeforces 295C Greg and Friends的更多相关文章
- Codeforces 295C Greg and Friends BFS
Greg and Friends BFS的过程中维护一下方案数. 我个人感觉不是很好想, 但是写出来之后怎么感觉这题这么SB啊啊. #include<bits/stdc++.h> #def ...
- 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 ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 296C Greg and Array
数据结构题.个人认为是比较好的数据结构题.题意:给定一个长度为n的数组a,然后给定m个操作序列,每个操作:l, r, x将区间[l, r]内的元素都增加a,然后有k个查询,查询形式是对于操作序列x,y ...
- [CodeForces - 296D]Greg and Graph(floyd)
Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...
- CodeForces 295B Greg and Graph (floyd+离线)
<题目链接> 题目大意:给定$n$个点的有向完全带权图$(n\leq500)$,现在进行$n$次操作,每次操作从图中删除一个点(每删除一个点,都会将与它相关联的边都删除),问你每次删点之前 ...
- Codeforces 295D - Greg and Caves(dp)
题意: 给出一个 \(n \times m\) 的矩阵,需对其进行黑白染色,使得以下条件成立: 存在区间 \([l,r]\)(\(1\leq l\leq r\leq n\)),使得第 \(l,l+1, ...
随机推荐
- 转:jQuery.fn.extend与jQuery.extend到底区别在哪?
还是先吐个槽,网上都都是转载抄袭,基本上就那么一两篇文章,说的还不清楚.... 正文: 其实说白了,从两个方法本身就能看出来端倪. 我们先把jQuery看成了一个类,这样好理解一些. jQuery.e ...
- WPF中的一些常用类型转换
1.string和Color的转换: //string转Color (Color)ColorConverter.ConvertFromString((string)str); //Color转stri ...
- spring mvc处理流程概述
大部分Java应用都是Web应用,展现层是Web应用不可忽略的重要环节.Spring为展现层提供了一个优秀的Web框架-Spring MVC.和众多其他Web框架一样,它基于MVC设计理念,此外,它采 ...
- BZOJ 3929 Circle of digits 解题报告
首先,我们可以得到最高位的位数为:\(\lfloor\frac{n+k-1}{n}\rfloor\),记作 \(E\). 然后给这 \(n\) 个长为 \(E\) 的数字排序,后缀数组 \(O((n+ ...
- java对xml文件做增删改查
http://www.cnblogs.com/wangchenyang/archive/2011/08/23/2150530.html http://www.blogjava.net/weishuan ...
- Jmeter 执行java脚本结束时提示:he JVM should have exitted but did not
使用jmeter执行java协议测试结束时会提示:he JVM should have exitted but did not ,jmeter2.11以后的可以 通过设置: jmeteren ...
- UL LI 布局 TAB 切换条
web页面实现tab的功能有几种实现方式,下面是使用UL LI DIV方式实现的tab. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- Markdown简单语法总结
. 标题 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 2. 列表 无序列表 * 西瓜 * 葡萄 * 香蕉 - 西瓜 - 葡萄 - ...
- free 命令解释
free 命令 buffers and cached 解释 N多人总是询问,当在linux在输入free时内存总数怎么加起来不一样啊,下面我来解释一下free命令的输出. 我们运行free命令时都会看 ...
- 使用C#在word中插入页眉页脚
//插入页脚 public void InsertFooter(string footer) { if (ActiveWindow.ActivePane.View.Type == WdViewType ...