[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]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
随机推荐
- ASP.NET 验证控件报错:WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。
在Visual Studio 2012中添加并使用验证控件时,可能会遇到如下的错误: WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResour ...
- Java基础面试题: 份证号码组成规则是前4位是代表省份和地区 编写一个程序,通过身份证号码判断某人是否是武汉人以及其性别。
package com.swift; public class ID_Where_Male_Test { public static void main(String[] args) { /* * 中 ...
- c++调用系统关机命令 c++调用暂停命令
#include<stdlib.h> int main() { //调用系统dos命令 system("shutdown -s -t 120"); ; } system ...
- iOS 打印系统字体
NSArray * array = [UIFont familyNames]; for( NSString *familyName in array ){ printf( "Family: ...
- 沙盒(SandBox)
iOS 应用沙盒机制就是指 iOS 应用程序只能在为该程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本 ...
- STL之stack操作
c++ stl栈stack介绍 C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构. c++ stl栈stack的头文件 ...
- 洛谷 4219/BZOJ 4530 大融合
4530: [Bjoi2014]大融合 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 990 Solved: 604[Submit][Status] ...
- Vim如何显示和关闭行号
显示行号: set nu 去除行号: set nonu
- Linux-CentOS6.9启动流程排错
1 . CentOS6.9启动流程 POST 加电自检,是BIOS功能的一个主要部分.负责完成对CPU.主板.内存.硬盘子系统.显示子系统.串并行接口.键盘.CD-ROM光驱等硬件情况的检测. Boo ...
- w3resource_MySQL练习:Joins
w3resource_MySQL练习题:Joins 1. Write a query to find the addresses (location_id, street_address, city, ...