http://codeforces.com/contest/525/problem/E

学习了传说中的折半DFS/双向DFS

先搜前一半数,记录结果,然后再搜后一半数,匹配之前结果。

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define LL long long
using namespace std;
const int mxn=;
struct data{
LL x,sum;
}b[mxn];//
int tot=;
map<LL,LL>mp[];//用[i]次阶乘,和为[j]的方案数
LL n,k,s;
LL a[mxn];
LL p[];
void DFS(LL cnt,LL t,LL now,int ed){
if(now>s || t>k)return;
if(cnt>ed){
if(ed==n/){
tot++;
b[tot].x=t;
b[tot].sum=now;
}
else{
mp[t][now]++;
}
return;
}
DFS(cnt+,t,now,ed);//不选
DFS(cnt+,t,now+a[cnt],ed);//选
if(a[cnt]<)DFS(cnt+,t+,now+p[a[cnt]],ed);//选阶乘*/
return;
}
void init(){
p[]=;
for(int i=;i<;i++){
p[i]=p[i-]*i;
}
}
int main(){
init();
scanf("%I64d%I64d%I64d",&n,&k,&s);
int i,j;
for(i=;i<=n;i++)scanf("%d",&a[i]);
int mid=n/;
DFS(,,,mid);
DFS(mid+,,,n);
LL ans=;
for(i=;i<=tot;i++){
for(j=;j<=k-b[i].x;j++){//可以不使用阶乘,所以从0开始
ans+=mp[j][s-b[i].sum];
}
}
printf("%I64d\n",ans);
return ;
}

[Codeforces Round #297 Div. 2] E. 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 MBSubmit: xxx  ...

  2. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  3. 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 ...

  4. 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  ...

  5. 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 ...

  6. 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  ...

  7. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  8. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  9. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

随机推荐

  1. Django项目SECRET_KEY等敏感信息保存

    在我们做完django项目后,向生产环境部署时,为了避免一些敏感信息被有心人利用,我们应该将其保护起来,比如说在settings配置中的一些密码等内容存在操作系统内,通过调用来使用,比如下面这种做法: ...

  2. MySQL迁移升级解决方案

    任务背景 由于现有业务架构已不能满足当前业务需求,在保证数据完整的前提下,现需要将原有数据库迁移到另外一台单独的服务器上,在保证原有服务正常的情况下,将原有LAMP环境中mysql数据库版本5.6.3 ...

  3. Docker工具

    虚拟化 什么是虚拟化 在计算机中,虚拟化(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源, 如服务器.网络.内存及存储等,予以抽象.转换后呈现出来, 打破实体结构间的 ...

  4. sqlmanagementstudio2008下载地址

    http://big.wy119.com/sqlmanagementstudio2008_x86_chs.zip

  5. NSNotificationCenter的用法

    作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息 [[NSNotificationCenter defaultCenter]  ...

  6. debian软raid

    http://www.linuxidc.com/Linux/2013-06/86487.htm  

  7. cygwin的使用

    参考资料: 对话 UNIX: 在 Windows 上使用 Cygwin  Cygwin使用指南

  8. Android开发——减小APK大小

    0. 前言 APK的大小对APP的加载速度,使用内存大小和消耗功率多少有一定影响.如何减小APK的大小对于Android开发者是一个永恒的话题. 查阅了很多相关资料,并将其做了删减以及总结.本文原创, ...

  9. while循环输出的表格

    方法一: <?php echo '<table border="1" width="800" align="center"> ...

  10. mysql PacketTooBigException 的处理方式

    SHOW VARIABLES LIKE '%max_allowed_packet%'; set global max_allowed_packet = 2*1024*1024*1000