Binomial Coefficient

This commit is contained in:
Hizenberg
2024-04-10 15:36:18 +05:30
parent 6ddf41feb5
commit bc8374182f
21 changed files with 810 additions and 769 deletions

View File

@@ -1,15 +1,15 @@
//Advanced Binary Seach.
int max_iter;
double low , high , ans;
double err ;
for(int i = 1 ; i <= max_iter ; i++ ){
double mid = low + (high - low)/2;
if( check(mid) ){
ans = mid;
low = mid + err;
}
else{
high = mid - err;
}
//Advanced Binary Seach.
int max_iter;
double low , high , ans;
double err ;
for(int i = 1 ; i <= max_iter ; i++ ){
double mid = low + (high - low)/2;
if( check(mid) ){
ans = mid;
low = mid + err;
}
else{
high = mid - err;
}
}

View File

@@ -1,6 +1,6 @@
//Binary Search Jump.
int ans = n;
for(int b = n/2 ; b >= 1 ; b/=2 ){
while(check(ans - b))ans -= b;
}
//Binary Search Jump.
int ans = n;
for(int b = n/2 ; b >= 1 ; b/=2 ){
while(check(ans - b))ans -= b;
}
cout << ans << endl;

View File

@@ -1,14 +1,14 @@
//Binary Search Normal
int low = 0 , high = n , ans;
while( low <= high ){
int mid = low + ( high - low)/2;
if( check(mid) ){
ans = mid;
low = mid + 1;
}
else{
high = mid - 1;
}
//Binary Search Normal
int low = 0 , high = n , ans;
while( low <= high ){
int mid = low + ( high - low)/2;
if( check(mid) ){
ans = mid;
low = mid + 1;
}
else{
high = mid - 1;
}
}

View File

@@ -1,14 +1,14 @@
double ternary_search(double l, double r) {
double eps = 1e-9; //set the error limit here
while (r - l > eps) {
double m1 = l + (r - l) / 3;
double m2 = r - (r - l) / 3;
double f1 = f(m1); //evaluates the function at m1
double f2 = f(m2); //evaluates the function at m2
if (f1 < f2)
l = m1;
else
r = m2;
}
return f(l); //return the maximum of f(x) in [l, r]
double ternary_search(double l, double r) {
double eps = 1e-9; //set the error limit here
while (r - l > eps) {
double m1 = l + (r - l) / 3;
double m2 = r - (r - l) / 3;
double f1 = f(m1); //evaluates the function at m1
double f2 = f(m2); //evaluates the function at m2
if (f1 < f2)
l = m1;
else
r = m2;
}
return f(l); //return the maximum of f(x) in [l, r]
}