二分查找:
int a[110],N;
int BinarySearch(int *a,int x)
{
int Left = a[1];
int Right = a[N];
while(Left <= Right)
{
int mid = (Left+Right)>>1;
if(a[mid] == x)
return mid;
else if(a[mid] > x)
Right = mid - 1;
else
Left = mid + 1;
}
return -1;
}
三分查找求极点:
double left,right,mid,midmid;
left = 0;
right = PI/2;
while(right-left >= 1e-7)
{
mid = (left+right)/2;
midmid = (mid+right)/2;
if(calc(mid) > calc(midmid))
right = midmid;
else
left = mid;
}
二分求等比数列
LL Power(LL p,LL n)
{
LL ret = 1;
while(n > 0)
{
if(n&1)
ret = ret * p % 9901;
p = p * p % 9901;
n >>= 1;
}
return ret;
}
LL Sum(LL p,LL n) //递归二分求(1 + p^1 + p^2 + … + p^n)%mod
{
if(n == 0)
return 1;
if(n&1)
return (Sum(p,n/2)*(1+Power(p,n/2+1)))%9901;
else
return (Sum(p,n/2-1)*(1+Power(p,n/2+1)) + Power(p,n/2))%9901;
}
时间: 2024-10-04 21:12:50