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 html格式解析
使用TFHpple解析html https://github.com/topfunky/hpple 前期准备工作 引入静态库文件 添加库文件的 header search paths(注意,必须选中 ...
- Azure ARMTemplate模板,VM扩展命令
Azure ARM模板中,给虚拟机安装扩展脚本的命令 "resources": [ { "apiVersion": "[variables('apiV ...
- EMAC IP 核
在有线连接的世界里,以太网(Ethernet)无所不在.以太网具有各种速度模式.接口方式.以及灵活的配置方式.现在的以太网卡都是10/100/1000Mbps自适应网卡.以太网的物理层(PHY)通常使 ...
- git项目常用命令
git rm --cached 文件名 //移除不上传 git add . //添加所有文件 .gitignore //git忽略不想上传或者不需要上传的文件 REAMDE.md 文 ...
- break,continue,return的区别
break,continue,return的区别 break 当break语句用于循环语句时,会终止执行循环,并执行循环后代码(如果有的话). function main() { for(var i ...
- iview upload 上传图片 不传服务器 转 base64
开始的时候 找不到this了,后来想起来要用 ES6的箭头函数 就有this了 reader.onload = e => { // 读取到的图片base64 数据编码 将此编码字符串传给后台即可 ...
- Java集合(四)--基于JDK1.8的ArrayList源码解读
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...
- QT_4_QpushButton的简单使用_对象树
QpushButton的简单使用 1.1 按钮的创建 QPushButton *btn = new QPushButton; 1.2 btn -> setParent(this);设置父窗口 1 ...
- 安装docker和docker-compose
环境:centos7,参考官方文档:https://docs.docker.com/insta... 第一步:删除旧版本和相关依赖,运行命令: yum remove docker \ docker ...
- docker run之后状态总是Exited
add -it docker run -it -name test -d nginx:latest /bin/bash