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.不选中它(即计算和的时候不考虑它) 如果我们直接暴力写的话复杂度是\ ...
随机推荐
- 10月30日下午 PHP精确查询(模糊查询、模糊+关键字共同查询)
1.一个条件的模糊查询 <body> <br /> <form action="main.php" method="post"&g ...
- IIS错误处理集合
1.编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ya ...
- Nessus的安装/激活/更新
0x1,安装 百度:Nessus,随意下载一个就好了. 0x2,激活 开启代理,获取register code,如图: 获取到register code,填写,进行激活,意外报错: NOTICE: A ...
- 进入OS前的两步之System tick
OK,继续向操作系统迈进.由简入繁,先实现两个小功能.第一个是system tick,第二个是任务切换(PendSV).一个是操作系统的心跳,一个是操作系统的并发处理的具体实现. System tic ...
- Error: Collection was modified; enumeration operation may not execute.
http://blog.csdn.net/ffeiffei/article/details/6131254
- JavaScript闭包之“词法作用域”
大家应该写过下面类似的代码吧,其实这里我想要表达的是有时候一个方法定义的地方和使用的地方会相隔十万八千里,那方法执行时,它能访问哪些变量,不能访问哪些变量,这个怎么判断呢?这个就是我们这次需要分析的问 ...
- 好代码系列(一):LazyObject
site-packages/django/utils/functional.py def new_method_proxy(func): def inner(self, *args): if self ...
- jq小插件--方便设置css属性
$.fn.extend({ setCss:function(name,value){ $(this).css(name,value) } }) 调用方式,如: $('.login-btn').setC ...
- C#高级编程笔记 Day 4, 2016年9月 12日(接口)
1.定义和实现接口:接口名称通常上以字母 I 开头 例子:定义IBankAccount接口 namespace Test.YinXi{ public interface IBankAccount{ v ...
- mac brew install redis 报错
mac brew install redis 报错 /usr/local/opt/php55/bin/phpize /usr/local/opt/php55/bin/phpize: line 61: ...