7.16 T1 gift

Subject to the effect: n two articles, each time there is a probability of pi to buy, you can repeat buy, you can buy nothing, but considered a purchase, ask the number expected to buy everything. Data for 10%, N = 1; for 30% of data, N ≤ 5; to 100% of the data, N ≤ 20, 0 <Wi ≤ 10 ^ 9, 0 <Pi ≤ 1 and ΣPi ≤ 1

Just saw this question, I went, probability and expectation, sank half (too dishes ..) put the last to do it.

n is small, so consider like pressure, f [i] n represents the current state of the kinds of goods as i expected purchases, 0 to buy, 1 did not buy (I'm just the opposite of QAQ with others), based on past expectations title practice, consider transferring backwards, so f [i] is the state i, to buy the full expectation purchases. f [0] indicates the whole buy, buy the full expectation is 0, f [1 << n + 1) -1] indicates not buy, i.e., the final answer.

Transfer equation is f [i] = Σ (f [j] +1) * p [k] + (1-Σp [k]) * (f [i] +1) j i ratio is more than 0 a plurality buy one kind, k is the kind of items

Meaning is transferred from the f [j] expectations and what this purchase did not buy or buy a heavy expectations, Σp [k] represents the probability buy new items, 1-Σp [k] is to buy old or do not buy, this is the state does not change, be transferred from f [i].

On both sides f [i], Gaussian elimination? False, item will get what shift f [i] = Σf [j] * p [k] / Σp [k].

Test when pushing the same formula does not consider to buy things, expect the question really weak burst, and n == 1 point that also did not expect to export 1 / p [1] who want to know Shane, and a topic and ideas exam the sample is not very clear and have been trying to find fault, do more than one hour, and then put it put off, trying to get the highest score.

#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
int n,b[(1<<21)+15];
ll w[25],ans;
double p[25],pp,pi,f[(1<<21)+15];
int lowbit(int x)
{
    return x&(-x);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lf%lld",&p[i],&w[i]);
        if(p[i]>0) ans+=w[i];
    }
    printf("%lld\n",ans);
    for(int i=0;i<=n;i++){
        b[1<<i]=i;
    }
    for(int i=0;i<=(1<<n+1)-1;i++){
        double h=0;
        for(int j=i;j;j-=lowbit(j)){
            int tmp=lowbit(j);
            f[i]+=f[i-tmp]*p[b[tmp]];
            h+=p[b[tmp]];
        }
        if(h) f[i]=(f[i]+1)/h;
    }
    printf("%.3lf\n",f[(1<<n+1)-1]);
    return 0;
}
Do not decadent

 

Guess you like

Origin www.cnblogs.com/jrf123/p/11198805.html