【[HNOI2002]彩票】

暴力?TLE!

50!/10!*40!=10272278170,看起来就知道可能性非常多,刚开始想用动态规划,但是这是实数于是泡汤,最后看数据不是很大,于是采用搜索加上剪枝

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m,ans;
double cnt,x,y;
const double eps=1e-10;
double sum[60];
void dfs(int now,int rest,double tot)//当前数,剩下次数,目前总和
{
    double maxn=tot+sum[now+(n-rest)-1]-sum[now-1];
    double minn=tot+sum[m]-sum[m-(n-rest)];
    if(minn-cnt>eps||maxn-cnt<-eps) return;
    //如果加上最大也不能达到该值,那么剪枝;
    //如果加上最小值也超出该值,那么剪枝
    if(rest==n)//如果剩下次数做完
    {
        ++ans;
        return;
    }
    if(now==m+1) return;
    dfs(now+1,rest,tot);//不选
    dfs(now+1,rest+1,tot+1.0/now);//选
}
int main()
{
    cin>>n>>m>>x>>y;
    cnt=x/y;
    int bt;
    for(int i=1;i<=m;++i)
        if(1.0/i>cnt)
            bt=i;
//如果就选这一个都要超出范围,那么就绝对不选
//(虽然dfs里面也可以自己退出但是能省时间就省时间吧)
    for(int i=1;i<=m;i++)
        sum[i]=sum[i-1]+1.0/i;
    dfs(1,0,0.0);
    cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/Shikita/article/details/81674644