HDU 4336 Card Collector(容斥)

Card Collector


模板题。


代码:

#include <bits/stdc++.h>
using namespace std;

const int max_n=10100;
 
int n;
double p[max_n],ans;

void dfs(int x, int tot, double sum)//容斥原理dfs(1,0,0.0) 
{
    if (x==n+1)
    {
        if (sum==0.0) return;
        if (tot&1)
            ans+=1/sum;
        else
            ans-=1/sum;
        return;
    }
    dfs(x+1,tot,sum);
    dfs(x+1,tot+1,sum+p[x]);
}   

int main()
{
    while(cin>>n)
    {
        for (int i=1; i<=n; i++)
            cin>>p[i];
        ans=0;
        dfs(1,0,0.0);
        printf("%.4f\n",ans);    
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37846371/article/details/78002140