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 Solved: 2xx
题目连接
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
Sample Input
2 2 30
4 3
2 2 7
4 3
3 1 1
1 1 1
Sample Output
1
1
6
HINT
题意:
给你n个数,k个魔法棒,s为所求的数,然后让你找有多少种方法,能够使的这n个数之和为s,其中一个魔法棒可以使的一个数变成他的阶乘。
题解:
折半搜索
~\(≧▽≦)/~啦啦啦,讲完啦~
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/*
inline ll read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
*/
//**************************************************************************************
ll n,s,k;
/*
inline ll read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
*/
struct node
{
ll x;
ll y;
}dota[];
ll pl[];
ll kiss[],mid,ans;
ll cnt=;
map<ll,ll> mp[];
void dfs(ll a,ll b,ll c)
{
//cout<<a<<" "<<b<<" "<<c<<endl;
if(b>k||c>s)
return;
if(a==mid+)
{
//cout<<a<<" "<<b<<" "<<c<<endl;
dota[++cnt].x=b,dota[cnt].y=c;
return;
}
dfs(a+,b,c);
dfs(a+,b,c+kiss[a]);
if(kiss[a]<=)
dfs(a+,b+,c+pl[kiss[a]]);
}
void DFS(ll a,ll b,ll c)
{ if(b>k||c>s)
return;
if(a==n+)
{
//cout<<a<<" "<<b<<" "<<c<<endl;
mp[b][c]++;
return;
}
DFS(a+,b,c);
DFS(a+,b,c+kiss[a]);
if(kiss[a]<=)
DFS(a+,b+,c+pl[kiss[a]]);
}
int main()
{
cin>>n>>k>>s;
pl[]=;
for(int i=;i<;i++)
pl[i]=i*pl[i-];
for(int i=;i<=n;i++)
cin>>kiss[i];
mid=n/;
dfs(,,);
DFS(mid+,,);
for(int i=;i<=cnt;i++)
{
for(int j=;j<=k-dota[i].x;j++)
{
ans+=mp[j][s-dota[i].y];
}
}
cout<<ans<<endl;
}
Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索的更多相关文章
- [Codeforces Round #297 Div. 2] E. Anya and Cubes
http://codeforces.com/contest/525/problem/E 学习了传说中的折半DFS/双向DFS 先搜前一半数,记录结果,然后再搜后一半数,匹配之前结果. #include ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 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 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
随机推荐
- Linux内核触摸屏驱动--多点触摸 【转】
转自:http://blog.chinaunix.net/uid-24227137-id-3127126.html 简介 为了使用功能强大的多点触控设备,就需要一种方案去上报用户层所需的详细的手指 ...
- replication-manager 搭建
replication-manager 搭建 介绍 replication-manager 主要用于mysql主从结构的监控和主从切换. 安装 vi /etc/yum.repos.d/signal18 ...
- 二十、springboot之jpa开发@MappedSuperclass 注解说明
@MappedSuperclass使用条件: 当我们进行开发项目时,我们经常会用到实体映射到数据库表的操作,此时我们经常会发现在我们需要映射的几个实体类中,有几个共同的属性,例如编号ID,创建者,创建 ...
- 【前端vue开发】Hbuilder配置Avalon、AngularJS、Vue指令提示
偶尔也会研究一下前端内容,因为Hbuilder是基于eclipse开发的,所以用起来倍感亲切啊,而且在我尝试使用的几款前端开发工具中,Hbuilder的表现也是相当出色地,可以访问Huilder官网下 ...
- 深度解析eclipse控制台
第一个按钮:scroll lock 控制台在打印sql语句的时候会一直滚动,用这个按钮可以固定住控制台不乱跑; 第二个按钮:show console when standard out changes ...
- 洛谷P1455搭配购买
传送门啦 这是强连通分量与背包的例题 需要注意的就是价值和价格两个数组不要打反了.. 另外 这是双向图!!! #include <iostream> #include <cstdio ...
- 应用服务器中对JDK的epoll空转bug的处理
原文链接:应用服务器中对JDK的epoll空转bug的处理 前面讲到了epoll的一些机制,与select和poll等传统古老的IO多路复用机制的一些区别,这些区别实质可以总结为一句话, 就是epol ...
- MySQL缓存命中率概述及如何提高缓存命中率
MySQL缓存命中率概述 工作原理: 查询缓存的工作原理,基本上可以概括为: 缓存SELECT操作或预处理查询(注释:5.1.17开始支持)的结果集和SQL语句: 新的SELECT语句或预处理查询语句 ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- LeetCode446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...