传送门

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. springdata-jpa 八种查询方法

    使用:maven+Spring+jpa+Junit4 查询方式:SQL,JPQL查询,Specification多条件复杂查询 返回类型:list<POJO>,list<Stinrg ...

  2. 自定义Jquery 下拉框

    (function ($){ 'use strict'; var g_id = 0; var g_open_id = []; $.fn.select3 = function () { var _id ...

  3. fork后子进程从哪里开始执行

    子进程和父进程都从调用fork函数的下一条语句开始执行

  4. execl, execlp, execle, execv, execvp - 执行某个文件

    总览 (SYNOPSIS) #include <unistd.h> extern char **environ; int execl( const char *path, const ch ...

  5. VC-基础:常用的安全CRT函数

    常用的安全CRT函数 安全CRT(C Runtime Library = C运行时间库)函数,是微软公司对C/C++语言的扩展.它在原来函数名后添加了“_s”后缀:一般返回出错代码:并将原来的函数返回 ...

  6. 时钟Demo

    其实是一个很简单的Demo,可以编译了拿NSIS打包.最近在做富文本编辑器和补C++不记得的东西吧,项目遥遥无期. //clock.pro #----------------------------- ...

  7. Liskon替换原则

    肯定有不少人跟我刚看到这项原则的时候一样,对这个原则的名字充满疑惑.其实原因就是这项原则最早是在1988年,由麻省理工学院的一位姓里的女士(Barbara Liskov)提出来的. 定义1:如果对每一 ...

  8. sin_addr.s_addr和sin_addr.S_un.S_addr

    sin_addr.s_addr和sin_addr.S_un.S_addr 先mark一下,等下写

  9. Cookie 详解以及实现一个 cookie 操作库

    Cookie 详解以及实现一个 cookie 操作库 cookie 在前端有着大量的应用,但有时我们对它还是一知半解.下面来看看它的一些具体的用法 Set-Cookie 服务器通过设置响应头来设置客户 ...

  10. 虚拟机如何设置静态IP

    一.本机环境 Mac.VMware Fusion 10, CentOS6.8 二.设置静态IP地址 1.选择网络连接模式,选择NAT模式 注意: 1)必须要选择NAT模式,否则你的虚拟机与主机始终会在 ...