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. MySQL索引的使用及注意事项

    索引是存储引擎用于快速找到记录的一种数据结构.索引优化应该是对查询性能优化最有效的手段了.索引能够轻易将查询性能提高几个数量级,"最优"的索引有时比一个"好的" ...

  2. 【读书笔记 - Effective Java】05. 避免创建不必要的对象

    1. 如果对象是不可变的(immutable),它就始终可以被重用. (1) 特别是String类型的对象. String str1 = new String("str"); // ...

  3. angular 打包

    ERROR in ng:///F:/IDEWorkspace/dsmc/dsmc-front-new/trunk/src/app/routes/city-manage/component-coding ...

  4. 《MySQL必知必会》--使用cmd登陆数据库

    数据库:保存有组织的数据的容器(通常是一个文件或一组文件). 表:某种特定类型数据的结构化清单. 模式:关于数据库和表的布局及特性的信息. 列:表中的一个字段.所有表都是由一个或多个列组成的. 数据类 ...

  5. EOJ Monthly 2019.3 A

    A. 钝角三角形 单点时限: 3.0 sec 内存限制: 512 MB QQ 小方以前不会判断钝角三角形,现在他会了,所以他急切的想教会你. 如果三角形的三边长分别为 a, b, c (a≤b≤c), ...

  6. Numpy 01

    Infi-chu: http://www.cnblogs.com/Infi-chu/ import numpy as np # 创建的数组 stus_score = np.array([[80, 88 ...

  7. P1060 开心的金明

    P1060 开心的金明 题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要 ...

  8. Dota2一直 正在登录服务器的解决办法

    然后:1: c:\Windows\System32\drivers\etc\ 2:双击hosts文件,用记事本方式打开3:复制以下并粘贴至以记事本方式打开的hosts最后面111.221.33.253 ...

  9. HIS 与医保系统的接入方案及实现

    HIS 与医保系统的接入方案及实现刘剑锋 李刚荣第三军医大学西南医院信息科(重庆 400038)摘要: 目的 建设HIS,迎接医疗改革的挑战.方法 分析HIS与地方医疗保险系统的不同特点,提出解决问题 ...

  10. 翻译:利用GDAL生成cogeoff文件

    翻译自: Introducing the AWS Lambda Tiler https://hi.stamen.com/stamen-aws-lambda-tiler-blog-post-76fc11 ...