【bzoj1224】彩票 dfs

本文介绍了一道算法题目中的剪枝优化方法,通过上下界剪枝减少搜索范围,提高求解效率。文章详细展示了代码实现过程,并分享了优化经验。

AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1224

【题解】

裸的爆搜相信大家都会,这里我们主要来谈一谈剪枝。

此题使用上下界剪枝:对于当前状态,如果后面的数都选最大的仍不能到达目标状态,则当前状态为不合法状态,直接return,此为上界剪枝,下界剪枝同理。

最后膜一发popoqqq大爷,参考他的代码,我省略掉了一个for循环,成功缩短时间2s,成功卡过。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<queue>
using namespace std;
#define eps 1e-10
#define FILE "read"
int n,m,x,y,ans;
long double cnt,c[55],beh[55],bef[55];
inline int read(){
	int x=0,f=1;  char ch=getchar();
	while(!isdigit(ch))  {if(ch=='-')  f=-1;  char ch=getchar();}
	while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
	return x*f;
}
void dfs(int depth,int last,long double sum){
	if(sum+beh[m-n+depth+1]-cnt>eps) return;//上界剪枝
	if(sum+(n-depth)*((long double)1/(last+1))-cnt+eps<0) return;//下界剪枝
	if(depth==n)  {ans++;  return;}
	dfs(depth,last+1,sum);
	dfs(depth+1,last+1,sum+(long double)1/(last+1));
}
int main(){
	freopen(FILE".in","r",stdin);
	freopen(FILE".out","w",stdout);
	n=read();  m=read();  x=read();  y=read();
	for(int i=1;i<=m;++i)  c[i]=(long double)1/i;
	for(int i=m;i>=1;--i)  beh[i]=c[i]+beh[i+1];
	cnt=(long double)x/y;
	dfs(0,0,0.0);
	printf("%d\n",ans);
	return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值