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. Word2vec资料

    Word2vec 很好的资料 Word2Vec-知其然知其所以然   https://www.zybuluo.com/Dounm/note/591752 Word2Vec数学原理讲解 http://w ...

  2. LINQ结合正则表达式查询文件系统

    string startFolder = @"D:\Program Files (x86)\Microsoft Visual Studio 12.0\"; IEnumerable& ...

  3. 怎样将Oracle数据库设置为归档模式及非归档模式

    怎样将Oracle数据库设置为归档模式及非归档模式 1.Oracle日志分类 分三大类: Alert log files--警报日志,Trace files--跟踪日志(用户和进程)和 redo lo ...

  4. 【转】MFC 程序入口和执行流程

    一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...

  5. NOIP2016——一个逗号引发的血案

    今年江西省报名人数一下子增起来了 隔壁中学来了80+人(虽然都是来给我们垫底的...临时被老师抓来上战场 总之我们赛区参赛人数总算多起来了(起码没再减50%...连续4年减50%真不是随便说说的... ...

  6. (70)zabbix telnet监控类型

    概述 zabbix监控的方式很多,例如前面讲到的agent.snmp以及后续后续要讲到ssh和今天要讲到的telnet.流程很简单,创建item-->配置ip.用户.密码.端口.脚本->z ...

  7. 【Python学习之七】面向对象高级编程——__slots__的使用

    1.Python中的属性和方法的绑定 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法. (1)首先,定义一个class:  class Stu ...

  8. MYSQL不能显示中文字,显示错误“ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89'”

    或者建表时带上编码utf8 CREATE TABLE `students`( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR( ...

  9. Linux redis服务搭建记录

    Redis的安装 1.安装redis需要C语言的编译环境 //gcc在线安装 yum install gcc-c++ 如果提示 /var/run/yum.pid 已被锁定,解决办法,删除yum.pid ...

  10. 【linux】服务说明

     引用自<鸟哥的linux私房菜>  http://cn.linux.vbird.org/linux_server/0210network-secure_3.php 服务名称 服务内容 a ...