题目

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;
}

转载于:https://www.cnblogs.com/WABoss/p/5897703.html

Codeforces525E Anya and Cubes(双向搜索)相关推荐

  1. 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 MB Submit: xxx  ...

  2. CF-525E(E. Anya and Cubes) Meet-in-the-Middle

    CF-525E(E. Anya and Cubes) Meet-in-the-Middle 题目链接 题意 n(n≤25)n(n \le 25)n(n≤25)个数字 kkk次染色机会.选择一个数字并对 ...

  3. codeforces 297 E. Anya and Cubes

    参考题解:http://blog.csdn.net/u014800748/article/details/44680613 题意: 给你n个cube,从里边最多选k个数,求选定的数中,求有多少数,或这 ...

  4. Solution:CF525E(Anya and Cubes)

    题目链接 Link:CF525E 解题思路 折半搜索. 存下前半部分所有可能的和,在搜后半部分时尝试配对. 但是,由于 ! ! ! 的个数限制,我们要分出 k + 1 k+1 k+1 个存前半部分结果 ...

  5. POJ1022 Packing Unit 4D Cubes

    题目来源:http://poj.org/problem?id=1022 题目大意: 有一些4维的单位体积的立方体盒子,每个立方体有8个面.要用一个大的4为盒子将它们包起来,求最小的大盒子体积. 输入: ...

  6. Uva 10177 - (2/3/4)-D Sqr/Rects/Cubes/Boxes?

    Problem J (2/3/4)-D Sqr/Rects/Cubes/Boxes? Input: standard input Output: standard output Time Limit: ...

  7. 10601 - Cubes(Ploya)

    UVA 10601 - Cubes 题目链接 题意:给定正方体12条棱的颜色,要求用这些棱能组成多少不同的正方体 思路:利用ploya定理去求解,分类讨论,正方体一共24种旋转.相应的旋转方式有4种: ...

  8. 6kyu Build a pile of Cubes

    6kyu Build a pile of Cubes 题目背景: Task: Your task is to construct a building which will be a pile of ...

  9. poj 3131 双向搜索+hash判重

    题意: 初始状态固定(朝上的全是W,空格位置输入给出),输入初始状态的空格位置,和最终状态朝上的位置,输出要多少步才能移动到,超过30步输出-1. 简析: 每一个格子有6种状态,分别是 0WRB, 1 ...

  10. UVA10601 Cubes - 波利亚定理

    Cubes 题意 给出121212根长度相等的木棒,颜色最多有666种,问能构成的本质不同的正方体数量. 题解 根据波利亚定理公式: 设X是元素集合,G是X的置换群,{u1,u2,...,uk}\{u ...

最新文章

  1. html分析python字典_从python字典到html-lis
  2. 《VMware Virtual SAN权威指南》一第1章 VSAN概述
  3. OpenViDial:一个大规模多模态对话数据集
  4. 脑芯编:窥脑究竟,织网造芯(二)
  5. 码云怎么创建公开的仓库_使用码云或GitHub搭建简单的个人网站(补充hexo搭建博客)...
  6. win 7 或 mac 远程桌面到 ubuntu (ssh)
  7. linux将屏幕输出内容转储到文件,Linux实时将所有输出重定向到文件
  8. 睡前1分钟 坚持瘦下来(信不信由你)
  9. 关于全国大学生集成电路创新创业大赛
  10. HP UINX磁带管理
  11. 实对称矩阵的几个性质
  12. ksu7对讲机调频软件_万能对讲机写频软件
  13. 转:sklearn 用户手册之1.12. 多类别与多标签算法
  14. Razor 视图引擎的一些属性和方法
  15. 铁路网络售票是利用计算机,铁路客运计算机售票具体操作图文.pdf
  16. 昨夜西风凋碧树,独上高楼,望尽天涯路
  17. java计算机毕业设计河东街摊位管理系统MyBatis+系统+LW文档+源码+调试部署
  18. mysql查询进阶——员工表与部门表连接查询
  19. Oracle P6软件如何编制一级计划
  20. 首设农作物种业专区农民丰收节国际贸易促进会舌尖上进博会

热门文章

  1. 实验7(2019.6.18)
  2. 网络环境未能通过安全验证,请稍后再试
  3. jQuery -- 光阴似箭(三):jQuery 操作 HTML 元素和属性
  4. spring boot区分生产环境和开发环境
  5. static关键字作用总结
  6. Spring-MVC案例:Spitter的笔记
  7. IIS7.5下的web.config 404应该如何配置
  8. Light OJ 1011
  9. mysql主从复制浅析(一)
  10. SharePoint 2010 添加“我的链接”菜单