E. Anya and Cubes (CF #297 (Div. 2) 折半搜索)

E. Anya and Cubes
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

Anya loves to fold and stick. Today she decided to do just that.

Anya has n cubes lying in a line and numbered from 1 to n from left to right, with natural numbers written on them. She also has kstickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes.

Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120.

You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most kexclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can you do it?

Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.

Input

The first line of the input contains three space-separated integers nk and S (1 ≤ n ≤ 250 ≤ k ≤ n1 ≤ S ≤ 1016) — the number of cubes and the number of stickers that Anya has, and the sum that she needs to get.

The second line contains n positive integers ai (1 ≤ ai ≤ 109) — the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.

Multiple cubes can contain the same numbers.

Output

Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.

Sample test(s)
input
2 2 30
4 3
output
1
input
2 2 7
4 3
output
1
input
3 1 1
1 1 1
output
6
Note

In the first sample the only way is to choose both cubes and stick an exclamation mark on each of them.

In the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them.

In the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six.



题意:给你n个数,k个魔法棒,s为所求的数,然后让你找有多少种方法,能够使的这n个数之和为s,其中一个魔法棒可以使的一个数变成他的阶乘。

思路:采用折半搜索,本题利用双向查找解决。双向查找一般用于求若干个数之和相加等于一个固定值的题目。一般方法是将n个数分为两部分:1~n/2和n/2+1到n,然后枚举出两部分的所有可能的结果,最后利用二分查找看第一部分的结果是否存在于第二部分中。本题也是让找一些数之和等于S,这个数还可以变成对应的阶乘数,由于阶乘的个数受到k的限制。因此可以利用dfs来枚举所有的情况。由于最后要求出方案的个数,因此一组sum,k(和值和已经使用的k的个数)和它出现的次数构成一个映射,因此用map来保存,即定义map<P,int>a,其中P就是pair<LL,int>类型。其他的过程就和双向搜索的主过程一样了。详细细节见代码注释。

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 25
typedef long long LL;
typedef pair<LL, int> P;
map<P, int>a, b;
int val[N];
LL f[N], S;
int _n, _k;
void dfs1(int pos, LL sum, int k)
{
	if (sum > S)return;
	if (k > _k)return;
	if (pos > _n / 2)
	{
		a[P(sum, k)]++;
		return;
	}
	dfs1(pos + 1, sum + val[pos], k);
	dfs1(pos + 1, sum, k);
	if (val[pos] <= 20)
		dfs1(pos + 1, sum + f[val[pos]], k + 1);
}
void dfs2(int pos, LL sum, int k)
{
	if (sum > S)return;
	if (k > _k)return;
	if (pos > _n)
	{
		b[P(sum, k)]++;
		return;
	}
	dfs2(pos + 1, sum + val[pos], k);
	dfs2(pos + 1, sum, k);
	if (val[pos] <= 20)
		dfs2(pos + 1, sum + f[val[pos]], k + 1);
}
int main()
{
	//freopen("t.txt", "r", stdin);
	f[0] = f[1] = 1;
	for (int i = 2; i <= 20; i++)f[i] = f[i - 1] * i;//计算阶乘
	while (cin >> _n >> _k >> S)
	{
		a.clear(), b.clear();
		for (int i = 1; i <= _n; i++)
			scanf("%d", val + i);
		dfs1(1, 0, 0);//先计算1到_n/2之间的所有情况,结果存放在a中
		dfs2(_n / 2 + 1, 0, 0);//再计算_n/2+1到_n的所有情况,结果存放在b中
		LL ans = 0;
		map<P, int>::iterator it = a.begin();
		for (it; it != a.end(); it++)
		{
			int j = ((*it).first).second;//第一部分中使用了j个感叹号
			for (int i = 0; i + j <= _k; i++)
			{
				if (b.count(make_pair(S - (it->first).first, i)))
					ans += (LL)it->second*b[make_pair(S - (it->first).first, i)];
			}
		}
		cout << ans << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值