Codeforces525E Anya and Cubes(双向搜索)
题目
Source
http://codeforces.com/contest/525/problem/E
Description
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 Input
2 2 30
4 3
2 2 7
4 3
3 1 1
1 1 1
Sample Output
1
1
6
分析
题目大概说有n个数,从中选出若干个数,可以再从选择的数中选择不超过k个数让它们变为自身的阶乘,问有几种方案使得选出来的若干个数的和等于S。
将n个数分成两边,左边dfs不选、选、选且变为阶乘搜出所有方案,用map统计各个和的情况;右边一样,利用map更新答案。
不过,由于最后还要枚举更新答案,超时了;把左边搜索的数量增加一点,变为min(n/2+2,n)就过了。
看了下官方题解,题解说先用count()判断map有没有再用这样会快,果然快了1倍。
代码
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std; long long S;
int n,m,a[26]; long long fact[20]={1}; long long ans; map<long long,int> cnt[26];
int tmp;
void dfs1(int x,int y,long long s){
if(s>S || y>m) return;
if(x==tmp){
++cnt[y][s];
return;
}
dfs1(x+1,y,s);
dfs1(x+1,y,s+a[x]);
if(a[x]<19) dfs1(x+1,y+1,s+fact[a[x]]);
} void dfs2(int x,int y,long long s){
if(s>S || y>m) return;
if(x==n){
for(int i=0; i+y<=m; ++i){
if(cnt[i].count(S-s)) ans+=cnt[i][S-s];
}
return;
}
dfs2(x+1,y,s);
dfs2(x+1,y,s+a[x]);
if(a[x]<19) dfs2(x+1,y+1,s+fact[a[x]]);
} int main(){
for(int i=1; i<20; ++i){
fact[i]=fact[i-1]*i;
}
scanf("%d%d%lld",&n,&m,&S);
for(int i=0; i<n; ++i){
scanf("%d",a+i);
}
tmp=min(n,n/2+2);
dfs1(0,0,0);
dfs2(tmp,0,0);
printf("%lld\n",ans);
return 0;
}
Codeforces525E Anya and Cubes(双向搜索)的更多相关文章
- 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 ...
- Anya and Cubes 搜索+map映射
Anya loves to fold and stick. Today she decided to do just that. Anya has n cubes lying in a line an ...
- Codeforces 525E Anya and Cubes
http://codeforces.com/contest/525/problem/E 题意: 有n个方块,上面写着一些自然数,还有k个感叹号可用.k<=n 你可以选任意个方块,然后选一些贴上感 ...
- Anya and Cubes CodeForces - 525E (双端搜索)
大意: 给定$n$元素序列$a$, 可以任选不超过$k$个$a_i$变换为$a_i!$, 求变换后任选若干元素和为S的方案数. 分成两块暴搜, 复杂度$O(3^{\frac{n}{2}})$ #inc ...
- codeforces E - Anya and Cubes 分块处理 暴力搜索
说的是给了n个立方体,立方体从1标号到n,每个立方体上有一个数字, 你有 k 个机会 使得其中 k个数位他们自己的阶乘,(自然使用可以少于k次机会,每个立方体最多被使用1次) ,那么求出你从这n个立方 ...
- CF525E Anya and Cubes(meet in the middle)
题面 给你\(n\)个数,\(n\le 26\)初始序列为\(a_i,0\le a_i\le 10^9\) 你有\(k\)个\(!\),每个\(!\)可以使序列中的一个数变成\(a_i!\) 例如\( ...
- [Codeforces Round #297 Div. 2] E. Anya and Cubes
http://codeforces.com/contest/525/problem/E 学习了传说中的折半DFS/双向DFS 先搜前一半数,记录结果,然后再搜后一半数,匹配之前结果. #include ...
- Codeforces 525E Anya and Cubes 中途相遇法
题目链接:点击打开链接 题意: 给定n个数.k个感叹号,常数S 以下给出这n个数. 目标: 随意给当中一些数变成阶乘.至多变k个. 再随意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制 ...
- 【Henu ACM Round#18 E】Anya and Cubes
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个数字有3种选择. 1.选中它. 2.选中它且加阶乘符号 3.不选中它(即计算和的时候不考虑它) 如果我们直接暴力写的话复杂度是\ ...
随机推荐
- CSS 概念 & 作用
http://www.cnblogs.com/moveofgod/archive/2012/09/18/2691101.html 式样定义 如何显示 HTML内容 通常存储在式样表中 作用 : 解 ...
- PHP文件大小格式化函数合集
比如碰到一个很大的文件有49957289167B,大家一看这么一长串的数字后面单位是字节B,还是不知道这个文件的大小是一个什么概念,我们把它转换成GB为单位,就是46.53GB.用下面这些函数就可以完 ...
- A=AUB
#include<stdio.h>#include<stdlib.h> #define LIST_MAX 10#define LIST_ADD 2 typedef struct ...
- (转载)Sumblime Text 2 常用插件以及安装方法
[内容提要]使用Package Control组件在线安装更方便 安装Sublime Text 2插件的方法: 1.直接安装 安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Pa ...
- js中的逻辑与(&&)和逻辑或(||)
之前有一个同事去面试,面试过程中碰到这样一个问题: 在js中写出如下的答案 : var a = 2; var b = 3; var andflag = a && b ; var orf ...
- maven project中,在main方法上右键Run as Java Application时,提示错误:找不到或无法加载主类XXX.XXXX.XXX
新建了一个maven project项目,经过一大堆的修改操作之后,突然发现在main方法上右键运行时,竟然提示:错误:找不到或无法加载主类xxx.xxx.xxx可能原因1.eclipse出问题了,在 ...
- STL学习之运算符(<<)重载问题和仿函数的实现
/* 运算符<<的重载一直报错, 友原函数中可以访问到类的私有成员*/#include<iostream>using namespace std; class MyIn ...
- angularjs中父,子,兄之间controller值得传递
使用angularjs,发现controller间的值传递,比较麻烦的,以后几篇文章会陆续说几种方法. 一,angularjs $broadcast $emit $on的处理思想 在一个control ...
- 292. Nim Game
292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on ...
- vue2.0学习(一)
1.解决双花括号在初始化时的闪烁,两种方式,一种是<div v-text="name"></div>,将用v-text指令来显示,类似于angular的ng ...