SD provincial team training 2019Day10 the "Distribution"

Distribution(distribution)

Title Description

Order \ (P \) a \ ((0, 1) \) a real number between, \ (n-\) a \ ([1, 100] \) positive integer.
Order \ (X_1, X_2,, X_n \...) Of \ (n-\) independent random variables, and satisfies \ (Pr (X_i = 1) = p, Pr (X_i = 0) = 1 - p \ ) , consider the following random variables \ (X = \ frac {X_1
+ X_2 + · · · + X_n + u - np} {\ sqrt {np (1 - p)}} \) where \ (U \) is \ ([- 0.5, 0.5] \) real random variable uniformly distributed between, and \ (X_i \) independently.
Now for some \ (n \) gives a number \ (X \) values, want you to guess \ (n \) values.

practice

To 100 possible values ​​of n, the probability is calculated, taking a maximum output.

Bayes calculation method to use the formula: (D denotes a random out \ (X_i \) values)
\ [\ Because P (D) * P (n-= A | D) = P (n-= A) * P (D | n = a) \\ \ therefore
P (n = a | D) = \ frac {P (D | n = a) * P (n = a)} {P (D)} \] since n is randomly , so \ (P (n = a) \) is a constant value. Similarly \ (P (D) \) is also a constant value. So we count \ (n = a \) randomly out when D (input \ (X_i \) probability value in the case) can be.

Code

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head
// 正文开始
int _,T;
double dp[110][110],x,prob[110],p,coef[110];
int main() {
    freopen("distribution.in","r",stdin);
    freopen("distribution.out","w",stdout);
    scanf("%d",&_);
    scanf("%lf",&p);
    dp[0][0]=1;
    rep(i,0,100) rep(j,0,i+1) dp[i+1][j+1]+=dp[i][j]*p,dp[i+1][j]+=dp[i][j]*(1-p);
    //解释一下这个方程:
    //dp[i][j]表示i个数中随机出j个1的概率
    //那么要么就是dp[i-1][j-1]再随机出来一个1,要么就是dp[i-1][j]在随机出来一个0。
    //所以就是dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j]*(1-p)
    //这里稍微改变了循环方式,可以避免不必要的计算,优化常数
    
    rep(i,1,101) rep(j,0,i+1) dp[i][j]=log(dp[i][j]);
    rep(n,1,101) coef[n]=0.5*log(n*p*(1-p));
    for (;_;_--) {
        scanf("%d",&T);
        rep(n,1,101) prob[n]=0;
        rep(i,0,T) {
            scanf("%lf",&x);
            rep(n,1,101) {
                int rr=floor(x*sqrt(n*p*(1-p))+n*p+0.5);
                if (rr<0||rr>n) prob[n]+=-1e9/*避免浮点数误差*/; else prob[n]+=dp[n][rr]+coef[n];
            }
        }
        printf("%d\n",max_element(prob+1,prob+101)-prob);
    }
}

Guess you like

Origin www.cnblogs.com/water-lift/p/10993794.html