Skip to content

Instantly share code, notes, and snippets.

@balamark
Created November 12, 2017 23:45
Show Gist options
  • Save balamark/a8d705ac38719a9d4b3876f8b2bd7cef to your computer and use it in GitHub Desktop.
Save balamark/a8d705ac38719a9d4b3876f8b2bd7cef to your computer and use it in GitHub Desktop.
[超棒的二項展開式實現] binomial formula implementation
/* given p as the probability of this team winning,
* what's the probability for this team have a prime number as it's final score
*/
double solve(int p){
vector<int> prob(0, 20);
prob[0]=1;
// 短短七行讓你省去寫C(n, k) 又可以拿到第k項展開後的結果
// generate binomial coefficient (x+y)^n : C(n, k) * (x^n-k) * (x^k)
// pascal triangle
// 1
// 0.8 0.2
// 0.8^2 2*(0.8*0.2) 0.2^2
// 1 3 3 1
// 1 4 6 4 1
//...
for(int t = 0;t < 18; ++ t){
for(int i = 18; i >= 0; -- i){
prob[i] *= (100 - p) / 100.0;
if(i > 0){
probi] += prob[i-1] * p / 100.0;
}
}
}
double sum = 0.0;
for(int i=1;i<=18;++i){
if(isPrime(i)){
sum += p[i];
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment