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;
}