[Codeforces Round #297 Div. 2] E. Anya and Cubes
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的更多相关文章
- 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 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 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 #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]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
随机推荐
- 你对CommonJS规范了解多少?
写在前面 为什么会出现CommonJS规范? 因为JavaScript本身并没有模块的概念,不支持封闭的作用域和依赖管理,传统的文件引入方式又会污染变量,甚至文件引入的先后顺序都会影响整个项目的运行. ...
- 【转】C++中map容器的说明和使用技巧
C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明 1 头文件 #include <map> ...
- python入门:1-99所有数的和的等式
#!/usr/bin/env python # -*- coding:utf-8 -*- #1-99所有数的和的等式 #start(开始,译音:思达二测)sum(合计,译音:桑木)temp(临时雇员, ...
- 【android】android对位图文件的支持
Android 支持以下三种格式的位图文件:.png(首选)..jpg(可接受)..gif(不建议).
- 【netbeans】【ubuntu】ubuntu netbeans 抗锯齿化修复
每一个在ubuntu下用netbeans的,都会对它的字体怎么会显示的那么难看表示很不理解.我就是因此几乎没有用netbeans的. 不过今天终于解决问题了,虽然没有eclipse显示的那么漂亮, ...
- 【mac】【php】mac php开机重启
homebrew.mxcl.php71.plist <?xml version="1.0" encoding="UTF-8"?> <!DO ...
- OpenCV中图像的读取,显示与保存
图像的读取,显示与保存 相关函数:cv2.imread().cv2.imshow().cv2.imwrite() 1.读入图像: 用cv2.imread()函数来读取图像,cv2.imread(路 ...
- Head First Python (二)
if...else... 1 movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam" ...
- STM32——PWM基本知识及配置过程
将通用定时器分为四个部分: 1,选择时钟 2,时基电路 3,输入捕获 4,输出比较 本节定时器PWM输出主要涉及到定时器框图右下方部分,即输出比较部分 和上一讲相同,时基时钟来源于内部默认时钟 对此有 ...
- hdu-2544 最短路(最短路)
Time limit1000 ms Memory limit32768 kB 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到 ...