One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold people with the total weight of at most k kilograms.

Greg immediately took a piece of paper and listed there the weights of all people in his group (including himself). It turned out that each person weights either 50 or 100 kilograms. Now Greg wants to know what minimum number of times the boat needs to cross the river to transport the whole group to the other bank. The boat needs at least one person to navigate it from one bank to the other. As the boat crosses the river, it can have any non-zero number of passengers as long as their total weight doesn't exceed k.

Also Greg is wondering, how many ways there are to transport everybody to the other side in the minimum number of boat rides. Two ways are considered distinct if during some ride they have distinct sets of people on the boat.

Help Greg with this problem.

Input

The first line contains two integers nk (1 ≤ n ≤ 50, 1 ≤ k ≤ 5000) — the number of people, including Greg, and the boat's weight limit. The next line contains n integers — the people's weights. A person's weight is either 50 kilos or 100 kilos.

You can consider Greg and his friends indexed in some way.

Output

In the first line print an integer — the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer-1.

In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (109 + 7). If transporting everyone to the other bank is impossible, print integer 0.

题目大意:n个人,有人的体重是50,有的人体重是100,船只只能承受k的重量。现在只有一条船,问最少须要渡河多少次,才能把这n个人都送到对岸(渡河时船上不能没人),在最小次数下问有多少种方案渡河,同一体重的两个人视为不同的人。

思路:设dis[x][y][boat]代表原来的岸上有x个体重为50的人,y个体重为100的人,有一条船在原来的岸上(其实boat不是0就是1),到结束状态dis[0][0][0]的最短距离。从状态dis[0][0][0]开始做最短路,每一步代价为1,故使用BFS,转移的时候只需要注意船上至少有一个人,重量不要超过k就好了。

那么dis[][][]数组求好了,从起始状态dis[n1][n2][1]到最终状态的最少渡河次数就能求出来了,其中n1代表原来有n1个体重为50的人,n2为原来用n2个体重为100的人。

然后求次数,dp就可以了。状态类似用dp[x][y][boat]表示到达某个状态的方案,对于下一个方案dp[i][j][!boat],当且仅当dis[i][j][!boat] + 1 = dis[x][y][boat],船不为空,不超载时转移。

dp[i][j][!boat] += dp[x][y][boat] * (可以移动的体重为50的人选需要移动的体重为50的人的方案数) * (可以移动的体重为100的人选需要移动的体重为100的人的方案数)

复杂度为O(n^4)

不过实际上可以在求dis的时候直接倒着把dp也算了……

代码(30MS):

 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII; const int MAXN = ;
const int INF = 0x3f3f3f3f;
const int MOD = ; struct State {
int x, y, boat;
State() {}
State(int x, int y, int boat):
x(x), y(y), boat(boat) {}
}; int dis[MAXN][MAXN][], vis[MAXN][MAXN][];
LL dp[MAXN][MAXN][], c[MAXN][MAXN];
int n, k, n1, n2; 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 a, b; while(cin>>a>>b) cout<<c[a][b]<<endl;
} void bfs() {
memset(dis, 0x3f, sizeof(dis));
queue<State> que; que.push(State(, , ));
dis[][][] = ;
while(!que.empty()) {
State p = que.front(); que.pop();
if(p.boat == )
for(int i = p.x; i <= n1; ++i)
for(int j = p.y; j <= n2; ++j) {
if(i == p.x && j == p.y) continue;
if(dis[i][j][] != INF || (i - p.x) + * (j - p.y) > k) continue;
dis[i][j][] = dis[p.x][p.y][] + ;
que.push(State(i, j, ));
}
else
for(int i = ; i <= p.x; ++i)
for(int j = ; j <= p.y; ++j) {
if(i == p.x && j == p.y) continue;
if(dis[i][j][] != INF || (p.x - i) + * (p.y - j) > k) continue;
dis[i][j][] = dis[p.x][p.y][] + ;
que.push(State(i, j, ));
}
}
} LL solve() {
if(dis[n1][n2][] == INF) {
puts("-1");
return ;
}
queue<State> que; que.push(State(n1, n2, ));
dp[n1][n2][] = ;
while(!que.empty()) {
State p = que.front(); que.pop();
if(p.boat == )
for(int i = ; i <= p.x; ++i)
for(int j = ; j <= p.y; ++j) {
if(i == p.x && j == p.y) continue;
if((p.x - i) + * (p.y - j) > k) continue;
if(dis[i][j][] + != dis[p.x][p.y][]) continue;
dp[i][j][] = (dp[i][j][] + dp[p.x][p.y][] * (c[p.x][p.x - i] * c[p.y][p.y - j]) % MOD) % MOD;
if(!vis[i][j][]) {
vis[i][j][] = true;
que.push(State(i, j, ));
}
}
else
for(int i = p.x; i <= n1; ++i)
for(int j = p.y; j <= n2; ++j) {
if(i == p.x && j == p.y) continue;
if((i - p.x) + * (j - p.y) > k) continue;
if(dis[i][j][] + != dis[p.x][p.y][]) continue;
dp[i][j][] = (dp[i][j][] + dp[p.x][p.y][] * (c[n1 - p.x][i - p.x] * c[n2 - p.y][j - p.y]) % MOD) % MOD;
if(!vis[i][j][]) {
vis[i][j][] = true;
que.push(State(i, j, ));
}
}
}
cout<<dis[n1][n2][]<<endl;
return dp[][][];
} int main() {
init();
scanf("%d%d", &n, &k);
k /= ;
for(int i = ; i <= n; ++i) {
int x;
scanf("%d", &x);
n1 += (x == );
}
n2 = n - n1;
bfs();
cout<<solve()<<endl;
}

codeforces 295C Greg and Friends(BFS+DP)的更多相关文章

  1. Codeforces Gym101201B:Buggy Robot(BFS + DP)

    题目链接 题意 给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作. 思路 和上次比赛做的 ...

  2. ZOJ 3596Digit Number(BFS+DP)

    一道比较不错的BFS+DP题目 题意很简单,就是问一个刚好包含m(m<=10)个不同数字的n的最小倍数. 很明显如果直接枚举每一位是什么这样的话显然复杂度是没有上限的,所以需要找到一个状态表示方 ...

  3. CodeForces - 1073E :Segment Sum (数位DP)

    You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...

  4. 【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)

    \(IDA^*\) 说实话,这道题我一开始没想出正解,于是写了一个\(IDA^*\)... 但神奇的是,这个\(IDA^*\)居然连字符串长度分别为\(2500,4000\)的数据都跑得飞快,不过数据 ...

  5. codeforces 591 E. Three States(bfs+思维)

    题目链接:http://codeforces.com/contest/591/problem/E 题意:有3个数字表示3个城市,每种城市都是相互连通的,然后不同种的城市不一定联通,'.'表示可以建设道 ...

  6. Codeforces 633F - The Chocolate Spree(树形 dp)

    Codeforces 题目传送门 & 洛谷题目传送门 看来我这个蒟蒻现在也只配刷刷 *2600 左右的题了/dk 这里提供一个奇奇怪怪的大常数做法. 首先还是考虑分析"两条不相交路径 ...

  7. CodeForces 690C2 Brain Network (medium)(树上DP)

    题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...

  8. Codeforces 758D Ability To Convert(区间DP)

    题目链接:http://codeforces.com/problemset/problem/758/D 题意:一个n进制下的数k,其中k不会用字母,如果有A就用10代替了.求k这个数对应的,在10进制 ...

  9. codeforces 633F The Chocolate Spree (树形dp)

    题目链接:http://codeforces.com/problemset/problem/633/F 题解:看起来很像是树形dp其实就是单纯的树上递归,就是挺难想到的. 显然要求最优解肯定是取最大的 ...

随机推荐

  1. 关于Date的冷门知识记录

    最近在做项目的时候,用到了Date.toLocaleString来处理当前日期.在这之前,我都是通过get*等方式来获取数据进行拼接.无意间,发现了toLocaleString方法.遂想写一篇文章来记 ...

  2. 集合Gk表示这样一堆数字,该集合内的数字有k个1

    问题描述 集合Gk表示这样一堆数字,该集合内的数字有k个1.比如,G1 = { 1, 10, 100, 1000, ...} G2 = {11, 110, 1110 }, ... , Gk { ... ...

  3. RHEL6(RedHat6)和SUSE11系统配置IPV6地址

    临时生效 RHEL6和SUSE11系统临时配置IPv6地址操作是一样的,比如添加如下ipv6地址. ip - addr add ::A/ dev eth2 ip - route add default ...

  4. vue-知乎日志

    1.项目API来源 2.项目地址 3.截图                                                       4.功能 首页 轮播图 动态消息 下拉刷新 动态 ...

  5. vue-cli项目使用axios实现登录拦截

    登录拦截 一.路由拦截 项目中某些页面需要用户登录后才可以访问,在路由配置中添加一个字段requireAuth 在router/index.js中 . const router = new Route ...

  6. pymyspl模块

    pymysql的下载和使用 该模块本质就是一个套接字客户端软件,使用前需要事先安装,能够让我们在 Python程序中操作数据库. pymysql模块的下载: 在Python安装文件中找到scripts ...

  7. Java学习笔记十八:Java面向对象的三大特性之封装

    Java面向对象的三大特性之封装 一:面向对象的三大特性: 封装 继承 多态   二:封装的概念: 将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访 ...

  8. ACM1004:Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  9. 行人属性识别 PETA数据集

    参见https://blog.csdn.net/hyk_1996/article/details/80322026(内有数据集下载地址) 其中footwearSneaker属性应该是footwearS ...

  10. 001-快速排序(C++实现)

    快速排序的基本实现 快速排序算法是一种基于交换的高效的排序算法,它采用了分治法的思想: 1.从数列中取出一个数作为基准数(枢轴,pivot). 2.将数组进行划分(partition),将比基准数大的 ...