Codeforces Round #297 (Div. 2) [ 折半 + 三进制状压 + map ]
2 seconds
256 megabytes
standard input
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.
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 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.
2 2 30
4 3
1
2 2 7
4 3
1
3 1 1
1 1 1
6
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 ![]()
转一下官方题解:
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 ]的更多相关文章
- Codeforces Round #222 (Div. 1) C. Captains Mode 状压
C. Captains Mode 题目连接: http://codeforces.com/contest/377/problem/C Description Kostya is a progamer ...
- Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa
原题链接:http://codeforces.com/contest/580/problem/D 题意: 给你一些一个有向图,求不超过m步的情况下,能获得的最大权值和是多少,点不能重复走. 题解: 令 ...
- Codeforces Round #302 (Div. 1) C - Remembering Strings 状压dp
C - Remembering Strings 思路:最关键的一点是字符的个数比串的个数多. 然后就能状压啦. #include<bits/stdc++.h> #define LL lon ...
- Codeforces Round #585 (Div. 2) E. Marbles (状压DP)
题目:https://codeforc.es/contest/1215/problem/E 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...
- Codeforces Round #585 (Div. 2) E. Marbles (状压DP),BZOJ大理石(同一道题)题解
题意 林老师是一位大理石收藏家,他在家里收藏了n块各种颜色的大理石,第i块大理石的颜色为ai.但是林老师觉得这些石头在家里随意摆放太过凌乱,他希望把所有颜色相同的石头放在一起.换句话说,林老师需要对现 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 安卓&IOS 手机添加O365 邮箱账户
手机添加O365 邮件账户 一.Android手机添加O365邮件账户 1. 找到手机上“电子邮件” 2. 打开设置 3. 点击添加账户 4. 选择“Exchange” 5. 输入O365的邮箱账户和 ...
- PMP项目管理学习笔记(12)——范围管理之创建工作分解结构(WBS)
创建工作分解结构过程是范围管理知识领域中最重要的过程,因为要在此过程明确所要做的全部工作 输入:收集需求和定义范围过程的输出会成为创建工作分解结构过程的输入(需求文档.组织资产过程.项目范围说明书) ...
- (一) Docker in Docker
一. 背景介绍 工作中,要实现在docker中运行docker,实现镜像的拉取,创建,修改,上传等操作. 尝试过在docker中,安装docker.行不通,服务起不来. 而且直接在 docker 容 ...
- mysql数据库比较,各数据库不同之处
和mysql数据库比较,各数据库不同之处: Oracle数据库:字段类型不同 postgresql数据库:show tables不同; SQL语句需要前面加上 模式名 Mongodb数据库:文档存储, ...
- python基础一 day5 集合
集合是无序的 增:add()添加进去是无序,不一定是最后面,update()像extend() 删: 没有改,有查,里面的元素是不可变类型 查用for in 交集: 并集: 反交集 叉集: 子集与超集 ...
- 加快create-react-app的方法
npm config get registry 查看npm源,默认源是 https://registry.npmjs.org/ npm config set registry https://regi ...
- 包含绑定变量的sql进行调优需注意一点
拿1个sql举个例子,我只贴出了where后面部分 实际环境中有init_date 和direct_no的组合索引IDX_DATE_NO 上诉标红处,:b3=0 和:b3<>0这两种情况o ...
- idea java 注释模板配置
在网上找了好久,好多的文章都有一个共同的病点就是“@param注释当有多个参数时候,全部放在了一行里面”,非常不友好. 以下是我整理好的,完全按照eclipse的注释风格. !!!先看最后实现的效果图 ...
- 02.28 day03
print(1 or 3 > 2 and 4 < 5 or 6 and 2 < 7)## while True:# print(11)# print(22)# # break# # ...
- MySQL中的事务日志
一.事务日志的作用 事务日志在保证事务的特性的同时,提高事务的执行效率 二.事务日志的工作原理 使用事务日志时,存储引擎修改了表的数据时只需要修改其内存拷贝. 然后再将修改行为记录到持久在硬盘上的事务 ...