Cattle off fireworks practice session 26B (probability DP)

Links: https://ac.nowcoder.com/acm/contest/180/B
Source: Cattle-off network

Fireworks
time limit: C / C ++ 1 second, other languages 2 seconds
space limitations: C / C ++ 262144K, other languages 524288K
Special Judge, 64bit the IO the Format:% LLD
subject description
small a has nn fireworks, each of the fireworks represent mutually the same color, for the first ii fireworks, it has p_ip
i probability of ignition, now a small light them go, he wanted to know the number of the desired color and produce produce produce exactly the probability kk colors

Input Description:
The first line of two integers n, kn, k
next line number nn, the number of ii p_ip
I represents the probability of fireworks ii ignited output Description: output has two rows of the first row represents a different generation the number of colors desired probability of generating a second row represents the colors exactly kk newline character segmentation






Example 1
Input
Copy
32
0.5 0.25 0.75
Output
replication
1.5000
0.4062
DESCRIPTION
second sample Q explain:
(1, 2): 0.5 * 0.25 * (l - 0.75) = 0.03125 (2): 0.5 * 0.25 * (1 -0.75) = 0.03125
(1, 3): 0.5 * (l - 0.25) * 0.75 = 0.28125 (3): 0.5 * (1-0.25) * 0.75 = 0.28125
(2, 3): (l - 0.5) * 0.25 * 0.75 = 0.09375 (3) :( 1-0.5) * 0.25 * 0.75 = 0.09375
sum obtained 0.406250.40625
NOTE:
for 30% 30% data: n \ leqslant 6, k \ leqslant nn⩽6 , k⩽n
100% 100% data: n-\ leqslant 10 ^. 5, K \ leqslant 2n⩽10 2 * 10 ^
. 5
, k⩽2 10 *
2

Outputs are reserved four decimal places, if your answer with std error less than 10 ^ {--4} 10
-4
is the correct

Meaning of the questions: ideas:


Because the number of fireworks contribution of each color is 1 so

The second probability outputs do dp,

Defined state dp dp [i] [j] to the i-th fireworks has a discharge probability j fireworks. .

For fireworks only two states each, lit and not lit, so

Current DP [I] [J] = DP [i-1] [J-. 1] A [I] + DP [i-1] [J] (1.0000-A [I]) i.e., i-1 fireworks only ignited when the probability of the j-1 fireworks, I'm using a [i] of the i-th current to ignite the fireworks, the fireworks that lit the j-th when the i-th fireworks,

Another is when a fireworks i, j ignites fireworks, fireworks currently does not ignite, j is lit fireworks when the i-th fireworks, add up to dp [i] [j]

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=100010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
double a[maxn];
double dp[maxn][202];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
    cin>>n;
    cin>>k;
    repd(i,1,n)
    {
        cin>>a[i];
    }
    repd(i,1,n)
    {
        repd(j,1,i)
        {
            dp[i][j]+=dp[i-1][j-1]*a[i];
            dp[i][j]+=dp[i-1][j]*(1.0000-a[i]);
        }
    }
    cout<<fixed<<setprecison()
    
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11247916.html