Skip to content

Instantly share code, notes, and snippets.

@314pies
Last active November 16, 2018 05:18
Show Gist options
  • Save 314pies/ade7145095ff712cf456a82cc404ef4b to your computer and use it in GitHub Desktop.
Save 314pies/ade7145095ff712cf456a82cc404ef4b to your computer and use it in GitHub Desktop.
Binomial distribution
/*
Input: x,n,p (Each inputs should separated by space or line)
Output: b(x,n,p)
*/
#include<iostream>
#include<cmath>
using namespace std;
long long int factorial(int n){
long long int _result=1;
for(int _n = n;n>=1;n--){
_result = _result*n;
}
return _result;
}
long long int combination(int n,int x){
return factorial(n)/(factorial(x)*factorial(n-x));
}
int main(){
int x,n;
double p;
while(cin>>x>>n>>p){
cout<<combination(n,x)*pow(p,x)*pow(1-p,n-x)<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment