传送门

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 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 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.

tag:

binary search

brute force
hashing
meet-in-the-middle

转一下官方题解:

http://codeforces.ru/blog/entry/17119?locale=en

525E — Anya and Cubes

To solve this problem we need to use meet-in-the-middle. At first sort given array in increasing order and divide it in two parts. In first part must be first n / 2 elements, in second part — other.

Iterate all submasks of all masks of elements from first part. That is iterate which cubes from first part we take and on which from them we paste exclamation marks. In this way we iterated all possible sums, which we can get with cubes from first part. Let for current submask we get sum sum_lf and use tlf exclamation marks. To store all such sums we use associative arrays map < long long > cnt[k + 1], where k — count of exclamation marks which we have in the beginning.

After that similary iterate all submasks of all masks of elements from second part. Let for current submask sum is sumrg and number of used exclamation marks is trg. Then from first part we need to get sum (s - sumrg) and we can use only (k - trg) exclamation marks, where s — sum which we must get by condition of the problem. Then iterate how many exclamation marks we will use in first part (let it be variable cur) and increase answer on cnt[cur][s - sumrg]. To accelerate our programm we may increase answer only if cnt[cur].count(s - sumrg) = true.

For submasks in iterate we can cut off iteration on current sum for submask (it must be less or equal to given s) and on current count of exclamation marks (it must be less or equal to given k). Also we should not paste exclamation marks on cubecs with numbers larger than 18, because 19! more than 1016 — maximal value of s.

Asymptotic behavior of this solution — O(3((n + 1) / 2) * log(maxcnt) * k), where n — count of cubes, maxcnt — maximal size of associative array, k — count of exclamation marks.

题意:选一些数,某些可以是该数的阶乘(不超过k个),问和等于S的方案数

题解:折半,三进制状压(0表示不选,1表示选ai,2表示选ai!),map

加速: To accelerate our programm we may increase answer only if cnt[cur].count(s - sumrg) = true.

不加速会T,,T在test91

10487086 2015-03-27 13:58:06 njczy2010 E - Anya and Cubes GNU C++ Time limit exceeded on test 91 2000 ms 33200 KB
10487048 2015-03-27 13:54:28 njczy2010 E - Anya and Cubes GNU C++ Accepted 858 ms 3800 KB 
 #include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <map>
#include <algorithm> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int n,k;
ll s;
ll ans;
ll a[N];
ll f[N];
map<ll,ll> cnt[N];
int L,R;
int totL,totR;
int b[N]; void ini1()
{
ll i;
f[]=;
for(i=;i<=;i++){
f[i]=f[i-]*i;
}
} int pw(int x)
{
int re=;
for(int i=;i<=x;i++){
re*=;
}
return re;
} void ini()
{
int i;
ans=;
for(i=;i<n;i++){
scanf("%I64d",&a[i]);
}
for(i=;i<=k;i++){
cnt[i].clear();
}
L=n/;
totL=pw(L);
R=n-L;
totR=pw(R);
} void solve()
{
int o,j;
int te;
ll sum;
int cou;
int ff;
for(o=;o<totL;o++){
te=o;
for(j=;j<L;j++){
b[j]=te%;
te/=;
}
sum=;cou=;
ff=;
for(j=;j<L;j++){
if(b[j]==) continue;
else if(b[j]==){
sum+=a[j];
}
else{
if(a[j]>=){
ff=;break;
}
else{
sum+=f[ a[j] ];
cou++;
}
}
if(sum>s){
ff=;break;
}
}
if(ff== || sum>s) continue;
cnt[ cou ][ sum ]++;
} for(o=;o<totR;o++){
te=o;
for(j=;j<R;j++){
b[j]=te%;
te/=;
}
sum=;cou=;
ff=;
for(j=;j<R;j++){
if(b[j]==) continue;
else if(b[j]==){
sum+=a[j+L];
}
else{
if(a[j+L]>=){
ff=;break;
}
else{
sum+=f[ a[j+L] ];
cou++;
}
}
if(sum>s){
ff=;break;
}
}
if(ff==) continue;
int leftk=k-cou;
ll lefts=s-sum;
if(leftk<) continue;
if(lefts<) continue;
for(int x=;x<=leftk;x++){
//printf(" x=%d lefts=%I64d cnt=%d\n",x,lefts,cnt[x][lefts]);
if(cnt[x].count(lefts)>)
ans+=cnt[ x ][ lefts ];
} }
} void out()
{
printf("%I64d\n",ans);
} int main()
{
ini1();
//freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d%I64d",&n,&k,&s)!=EOF)
{
ini();
solve();
out();
}
}

Codeforces Round #297 (Div. 2) [ 折半 + 三进制状压 + map ]的更多相关文章

  1. Codeforces Round #222 (Div. 1) C. Captains Mode 状压

    C. Captains Mode 题目连接: http://codeforces.com/contest/377/problem/C Description Kostya is a progamer ...

  2. Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa

    原题链接:http://codeforces.com/contest/580/problem/D 题意: 给你一些一个有向图,求不超过m步的情况下,能获得的最大权值和是多少,点不能重复走. 题解: 令 ...

  3. Codeforces Round #302 (Div. 1) C - Remembering Strings 状压dp

    C - Remembering Strings 思路:最关键的一点是字符的个数比串的个数多. 然后就能状压啦. #include<bits/stdc++.h> #define LL lon ...

  4. Codeforces Round #585 (Div. 2) E. Marbles (状压DP)

    题目:https://codeforc.es/contest/1215/problem/E 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...

  5. Codeforces Round #585 (Div. 2) E. Marbles (状压DP),BZOJ大理石(同一道题)题解

    题意 林老师是一位大理石收藏家,他在家里收藏了n块各种颜色的大理石,第i块大理石的颜色为ai.但是林老师觉得这些石头在家里随意摆放太过凌乱,他希望把所有颜色相同的石头放在一起.换句话说,林老师需要对现 ...

  6. 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 MBSubmit: xxx  ...

  7. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  8. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  9. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

随机推荐

  1. subprocess模块详解2

    1.call() 和run功能类似,都是接受一个列表里的参数. >>> import subprocess >>> a = subprocess.call([&qu ...

  2. CF962E Byteland, Berland and Disputed Cities

    思路: http://codeforces.com/blog/entry/58869. 实现: #include <bits/stdc++.h> using namespace std; ...

  3. Dom 获取、Dom动态创建节点

    一.Dom获取 1.全称:Document     Object     Model 文档对象模型 2.我们常用的节点类型 元素(标签)节点.文本节点.属性节点(也就是标签里的属性). 3.docum ...

  4. 使用laravel的Command实现搜索引擎索引和模板的建立

    创建command,初始化es 创建成功后,可通过php artisan 查看到 php artisan make:command ESInit 安装guzzle composer require g ...

  5. 腾讯AI开放平台的接口调用指南

    最近无意发现腾讯AI开放平台上提供了大量好玩的人工智能云服务,而且是完全免费的.只需要用QQ号登录即可.这么好的东西,作为一个程序员,当然要试试了! 从上图可以看出腾讯AI开放平台提供的人工智能服务主 ...

  6. (转)为Spring集成的Hibernate配置二级缓存

    http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...

  7. 网新恩普(W 笔试)

    选择题 1.一桶有黄色,绿色,红色三种,闭上眼睛抓取同种颜色的两个.抓取多少个就可以确定你肯定有两个同一颜色的球? 答案: 4次 1.最坏打算抓3次都是不同颜色的黄.绿.红,此时,三种颜色的球各抓了一 ...

  8. k8s部署测试实例

    查看运行中pod,并运行一个容器 [root@mast-1 k8s]# kubectl get pods No resources found. [root@mast-1 k8s]# kubectl ...

  9. function语句注意事项

    function语句 在Javascript中定义一个函数,有两种写法: function foo() { } 和 var foo = function () { } 两种写法完全等价.但是在解析的时 ...

  10. MFC中EDIT控件实现换行

    \n是C下的回撤换行.在MFC下得用\r\n.