codeforces 297 E. Anya and Cubes
参考题解:http://blog.csdn.net/u014800748/article/details/44680613
题意:
给你n个cube,从里边最多选k个数,求选定的数中,求有多少数,或这个数的阶乘,的和等于S的个数数。
思路:
本题利用双向查找解决。双向查找一般用于求若干个数之和相加等于一个固定值的题目。一般方法是将n个数分为两部分:1~n/2和n/2+1到n,然后枚举出两部分的所有可能的结果,最后利用二分查找看第一部分的结果是否存在于第二部分中。本题也是让找一些数之和等于S,这个数还可以变成对应的阶乘数,由于阶乘的个数受到k的限制。因此可以利用dfs来枚举所有的情况。由于最后要求出方案的个数,因此一组sum,k(和值和已经使用的k的个数)和它出现的次数构成一个映射,因此用map来保存状态,即定义map<P,int>a,其中P就是pair<LL,int>类型,pair(sum,k)表示当选k个数,和为sum 的个数。其他的过程就和双向搜索的主过程一样了。详细细节见代码注释。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits> 11 #include<string.h> 12 #include<numeric> 13 #include<cmath> 14 #include<stdlib.h> 15 #include<vector> 16 #include<stack> 17 #include<set> 18 #define FOR(x, b, e) for(int x=b;x<=(e);x++) 19 #define REP(x, n) for(int x=0;x<(n);x++) 20 #define mp(a,b) make_pair(a,b) 21 #define INF 1e7 22 #define MAXN 100010 23 #define maxn 1000010 24 #define Mod 1000007 25 #define N 25 26 using namespace std; 27 typedef long long LL; 28 typedef pair<LL, int> P; 29 map<P, int> a, b; 30 LL f[N], S; 31 int val[N]; 32 int _n, _k; 33 34 void dfs1(int pos, LL sum, int k) //前半部分 35 { 36 if (sum > S || k > _k) return; 37 if (pos > _n / 2) { 38 a[P(sum, k)]++; 39 return; 40 } 41 dfs1(pos + 1, sum + val[pos], k); 42 dfs1(pos + 1, sum, k); 43 if (val[pos] <= 20) //20以上的阶乘大于10^16 44 dfs1(pos + 1, sum + f[val[pos]], k + 1); 45 } 46 47 void dfs2(int pos, LL sum, int k) //后半部分 48 { 49 if (sum > S || k > _k) return; 50 if (pos > _n) 51 { 52 b[P(sum, k)]++; 53 return; 54 } 55 dfs2(pos + 1, sum + val[pos], k); 56 dfs2(pos + 1, sum, k); 57 if (val[pos] <= 20) 58 dfs2(pos + 1, sum + f[val[pos]], k + 1); 59 } 60 61 void init() 62 { 63 f[0] = f[1] = 1; 64 FOR(i, 2, 20) 65 f[i] = i*f[i - 1]; 66 } 67 68 int main() 69 { 70 init(); 71 while (cin >> _n >> _k >> S) { 72 a.clear(); 73 b.clear(); 74 for (int i = 1; i <= _n; ++i) 75 cin >> val[i]; 76 dfs1(1, 0, 0); 77 dfs2(_n / 2 + 1, 0, 0); 78 LL ans = 0; 79 map<P, int>::iterator it = a.begin(); 80 for (; it != a.end(); it++) { 81 int j = (*it).first.second; 82 for (int i = 0; i + j <= _k; ++i) { 83 if (b.count(mp(S - (it->first.first), i))) 84 ans += (LL)it->second*b[mp(S - (it->first.first), i)]; 85 } 86 } 87 cout << ans << endl; 88 } 89 return 0; 90 }
代码君
转载于:https://www.cnblogs.com/usedrosee/p/4375096.html
codeforces 297 E. 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 MB Submit: xxx ...
- 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次染色机会.选择一个数字并对 ...
- 【CodeForces - 764B 】Timofey and cubes (模拟)
题干: Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents ...
- Solution:CF525E(Anya and Cubes)
题目链接 Link:CF525E 解题思路 折半搜索. 存下前半部分所有可能的和,在搜后半部分时尝试配对. 但是,由于 ! ! ! 的个数限制,我们要分出 k + 1 k+1 k+1 个存前半部分结果 ...
- 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 MB Submit: xxx ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'* ...
- 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie
题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <al ...
- CodeForces - 764B Timofey and cubes
CodeForces - 764B Timofey and cubes 这个题其实压根不用读题,看样例猜反而更简便. Young Timofey has a birthday today! He go ...
- Codeforces Round #702 C. Sum of Cubes
解法:暴力 题目大意:给定一个x,求是否存在两个数使得 a*a*a + b*b*b = x. 思路:如果用双层for枚举,数据量一定会导致超时.所以可以通过枚举a,同时通过已经知道的x,来求出b,这样 ...
最新文章
- php reactphp wss_php无限级分类 - ▄︻┻┳000000000000000000000000000000000000 - OSCHINA - 中文开源技术交流社区...
- servlet设置session追踪模式
- python 函数调用 不允许关键字参数_你所不知道的Python|函数参数的演进之路
- oracle定时服务器,服务器oracle数据库定时备份
- python二级考试答案分值_2018全国计算机二级考试内容 科目分值设置
- pkpm板按弹性计算还是塑性_PKPM中的S\R验算显红原因分析
- 只要一点点力气就可以撬起重物?
- jquery控制只监听数字_如何在jQuery中监听并保持单击?
- java when for where_JAVA_EE_MyBatis之动态SQL
- python为什么没有数据类型_python3 数据类型
- JS Location
- JVM-类加载、GC回收机制
- HashMap底层实现原理详解
- 可控硅的工作原理和主要作用
- 快速将bmp批量转换jpg的方法
- 嘉兴 机器人仓库 菜鸟_菜鸟物流嘉兴未来园区的工业机器人系统运维员的一天...
- 三阶魔方入门基础教程
- 部落卫队问题 (回溯)
- 波菲那契数列公式_斐波那契数列为什么那么重要,所有关于数学的书几乎都会提到?...
- 百面机器学习2---模型评估
热门文章
- [3] ADB 设备连接管理
- ThinkPHP6项目基操(2.Nginx配置虚拟域名及简化访问路径)
- input输入框禁止自动补全和下拉提示
- mysql如何实现逻辑自增_每日一面 - mysql 的自增 id 的实现逻辑是什么样子的?
- Linux笔记-iptables开放指定端口,开放ICMP协议,其他端口禁止访问
- Linux笔记-shell遍历数组并判断是否等于某个值
- Android逆向笔记-单机游戏通过配置文件修改技能等
- C++笔记-设置cout输出数据的宽度和填充
- 数据库工作笔记/设计思路-通过增加索引表为某库中其他表添加索引(描述信息)
- C++工作笔记-VS中“调用堆栈”窗口的使用,实现越界的快速定位