Codeforces Round #297 (Div. 2) 525E Anya and Cubes(dfs)
来源:互联网 发布:淘宝线下代购 编辑:程序博客网 时间:2024/05/22 06:33
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 kexclamation 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 304 3
1
2 2 74 3
1
3 1 11 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.
题目链接:点击打开链接
给出n个棍子, k个魔法棒, 目标数s. 魔法棒可以使得棍子长度变为棍子长度的阶乘, 问木棒和为s的方案数.
预处理求出阶乘, 根据s范围可知木棒最长是18, 而后进行dfs, dfs时, 分为三种情况, 取这根木棒, 不取这根木棒, 以及取这根木棒的阶乘.
进行两次dfs, 折半存储结果.
AC代码:
#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"#include "queue"#include "stack"#include "cmath"#include "utility"#include "map"#include "set"#include "vector"#include "list"#include "string"#include "cstdlib"using namespace std;typedef long long ll;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;const int MAXN = 30;int n, k;ll s, fac[MAXN], a[30], ans;map<ll, ll> mp[MAXN];void Dfs1(int i, int cnt, ll sum){if(sum > s || cnt > k) return;if(i == n / 2) {mp[cnt][sum]++;return;}Dfs1(i + 1, cnt, sum);Dfs1(i + 1, cnt, sum + a[i]);if(a[i] <= 18) Dfs1(i + 1, cnt + 1, sum + fac[a[i]]);}void Dfs2(int i, int cnt, ll sum){if(sum > s || cnt > k) return;if(i == n) {for(int j = 0; j + cnt <= k; ++j)if(mp[j].count(s - sum)) ans += mp[j][s - sum];return;}Dfs2(i + 1, cnt, sum);Dfs2(i + 1, cnt, sum + a[i]);if(a[i] <= 18) Dfs2(i + 1, cnt + 1, sum + fac[a[i]]);}int main(int argc, char const *argv[]){fac[0] = 1;for(int i = 1; i <= 18; ++i)fac[i] = fac[i - 1] * i;scanf("%d%d%lld", &n, &k, &s);for(int i = 0; i < n; ++i)scanf("%lld", &a[i]);Dfs1(0, 0, 0);Dfs2(n / 2, 0, 0);printf("%lld\n", ans);return 0;}
- Codeforces Round #297 (Div. 2) 525E Anya and Cubes(dfs)
- Codeforces Round #297 (Div. 2)---E. Anya and Cubes
- Codeforces Round #297 (Div. 2) E - Anya and Cubes
- Codeforces Round #297 (Div. 2) E. Anya and Cubes
- Codeforces Round #297 (Div. 2) E Anya and Cubes
- Codeforces Round #297 (Div. 2) E Anya and Cubes
- Codeforces Round #297 (Div. 2)E. Anya and Cubes
- Codeforces Round #297 (Div. 2) E. Anya and Cubes (双向DFS)
- 【Codeforces Round #297 (Div. 2)】Codeforces 525E Anya and Cubes
- 中途相与法(Codeforces Round #297 (Div. 2)E - Anya and Cubes )
- Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
- Codeforces Round #297 (Div. 2)E. Anya and Cubes(折半搜索)
- #297 (div.2) E. Anya and Cubes
- Anya and Cubes - CodeForces 525 E dp
- codeforces 525E. Anya and Cubes
- CODEFORCES 525E Anya and Cubes
- E. Anya and Cubes (CF #297 (Div. 2) 折半搜索)
- spring+mybatis返回json日期处理(日期全局处理相差8个小时解决方法)
- 用C++实现一个Quaternion类
- 传感器
- Jloi 2015 一试
- codeforces 301D. Yaroslav and Divisors(遍历和排序的艺术)
- Codeforces Round #297 (Div. 2) 525E Anya and Cubes(dfs)
- 一步步将vim改造成C/C++开发环境(IDE)
- 浅谈android之popupWindow用法
- 关于Android中的Fragment个人小结
- 网络技术基本认识
- 从图片重建3D模型
- meteor简介
- (九十八)递归(函数与函数自己)
- 委托和多线程的理解