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

Codeforces Round #297 (Div. 2)E. Anya and Cubes

Time Limit: 2 Sec  Memory Limit: 512 MB
Submit: xxx  Solved: 2xx

题目连接

http://codeforces.com/contest/525/problem/E

Description

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 k stickers 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 k exclamation 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 n, k and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n, 1 ≤ 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 Input

Input
2 2 30
4 3
 
Input
2 2 7
4 3
 
Input
3 1 1
1 1 1

Sample Output

Output
1
Output
1
Output
6

HINT

题意:

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

题解:

折半搜索

对于每一个数,都有三种策略,拿,不拿,使用魔法棒
那么我们就直接折半搜索然后扔进一个map里面,然后就查询就好啦~
13^3*12^3=3 796 416;
复杂度算的刚刚好呀= =

~\(≧▽≦)/~啦啦啦,讲完啦~

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/*
inline ll read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
*/
//**************************************************************************************
ll n,s,k;
/*
inline ll read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
*/
struct node
{
ll x;
ll y;
}dota[];
ll pl[];
ll kiss[],mid,ans;
ll cnt=;
map<ll,ll> mp[];
void dfs(ll a,ll b,ll c)
{
//cout<<a<<" "<<b<<" "<<c<<endl;
if(b>k||c>s)
return;
if(a==mid+)
{
//cout<<a<<" "<<b<<" "<<c<<endl;
dota[++cnt].x=b,dota[cnt].y=c;
return;
}
dfs(a+,b,c);
dfs(a+,b,c+kiss[a]);
if(kiss[a]<=)
dfs(a+,b+,c+pl[kiss[a]]);
}
void DFS(ll a,ll b,ll c)
{ if(b>k||c>s)
return;
if(a==n+)
{
//cout<<a<<" "<<b<<" "<<c<<endl;
mp[b][c]++;
return;
}
DFS(a+,b,c);
DFS(a+,b,c+kiss[a]);
if(kiss[a]<=)
DFS(a+,b+,c+pl[kiss[a]]);
}
int main()
{
cin>>n>>k>>s;
pl[]=;
for(int i=;i<;i++)
pl[i]=i*pl[i-];
for(int i=;i<=n;i++)
cin>>kiss[i];
mid=n/;
dfs(,,);
DFS(mid+,,);
for(int i=;i<=cnt;i++)
{
for(int j=;j<=k-dota[i].x;j++)
{
ans+=mp[j][s-dota[i].y];
}
}
cout<<ans<<endl;
}
上一篇:linux中yum命令的解析


下一篇:模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie