传送门

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. 洛谷P3254 圆桌问题(最大流)

    题意 $m$个不同单位代表参加会议,第$i$个单位有$r_i$个人 $n$张餐桌,第$i$张可容纳$c_i$个代表就餐 同一个单位的代表需要在不同的餐桌就餐 问是否可行,要求输出方案 Sol 比较zz ...

  2. 一份最贴近真实面试的Java基础面试题

    这是一份Java基础知识的面试题.在网上的关于Java的面试题数不胜数,但认真看过感觉大多数都没有实用性,有很多是面试官根本就不会问到的,那些已经脱离了实际开发的技术问题.而这份资料来源自一份个人觉得 ...

  3. (转)Synopsys工具简介

    DC Ultra--Design Compiler的最高版本 在Synopsys软件中完整的综合方案的核心是DC UltraTM,对所有设计而言它也是最好级别的综合平台.DC Ultra添加了全面的数 ...

  4. RFS自动化测试(一)

    RFS 即 Robot Framework + Selenium RFS 的安装 1. python https://www.python.org/ RF框架是基于python的,所以要先安装有pyt ...

  5. PMP项目管理学习笔记(9)——范围管理

    关于范围管理的几个名词定义 产品范围:表示你和你的团队正在构建的产品或服务的特性和功能:产品范围与最终产品有关,包括产品的特性,组件和组成部分.人们谈论确定产品的范围时,大多都是在谈论确定产品的特性, ...

  6. Es6里面的解析结构

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  7. javaee 第八周作业

    hashcode()和equals()的作用.区别.联系 先来试想一个场景,如果你想查找一个集合中是否包含某个对象,那么程序应该怎么写呢?通常的做法是逐一取出每个元素与要查找的对象一一比较,当发现两者 ...

  8. Android(java)学习笔记174:服务(service)之混合方式开启服务

    1. 前面我们已经讲过可以使用两种方式开启服务 startService----stopService:        oncreate() ---> onstartCommand() ---& ...

  9. axios token header response request http拦截器 axios实现登录、拦截、登出

    axios token header response request http拦截器 axios实现登录.拦截.登出 一个项目学会前端实现登录拦截 https://github.com/superm ...

  10. 命令终端执行python

    windows进入cmd 1.进入cmd窗口,找到存放py文件的地址(如E:\learn_mock) 2.退出python,输入exit() linux下一样