二分查找:
#include<stdio.h>

int a[10]={0,1,2,3,4,5,6,9,10,100};

int search(int x)
{
  int left,right,mid;
  left=0;
  right=9;
  while(left<=right)
  {
    mid=(right+left)>>1;
    if(a[mid]>x)
      right=mid-1;
    else if(a[mid]<x)
      left=mid+1;
    else
      return mid;
  }
  return -2;
}

int main()
{
  int x,ans;
  while(scanf("%d",&x)!=EOF)
  {
    ans=search(x);
    printf("%d\n",ans+1);
  }
  return 0;
}


二分解方程:
#include<stdio.h>
#include<math.h>
#define eps 1e-6

double min(double x,double y)
{
  if(x>y)
    return y;
  else
    return x;
}

double f(double k,double x,double y,double c)
{
  double xx,yy;
  xx=1/sqrt(x*x-k*k);
  yy=1/sqrt(y*y-k*k);
  return xx+yy;
}

void slove(double x,double y,double c)
{
  double mid,left,right;
  left=0;
  right=min(fabs(x),fabs(y));
  while(1)
  {
    mid=(left+right)/2;
    if(fabs(f(mid,x,y,c)-(1/c))<=eps)
      break;
    if(f(mid,x,y,c)-(1/c)>0)
      right=mid;
    if(f(mid,x,y,c)-(1/c)<0)
      left=mid;
  }
  printf("%.3lf\n",mid);
}

int main()
{
  double x,y,c;
  while(scanf("%lf%lf%lf",&x,&y,&c)!=EOF)
  {
    slove(x,y,c);
  }
  return 0;
}

二分取模:
#include<stdio.h>

int m;

int pow(int x,int n)
{
  if(n==0)
    return 1;
  if(n&1)
    return ((pow(x,n>>1)%m)*(pow(x,n>>1)%m)*(x%m))%m;
  else
    return ((pow(x,n>>1)%m)*(pow(x,n>>1)%m))%m;
}

int main()
{
  int x,n,ans;
  while(scanf("%d%d%d",&x,&n,&m)!=EOF)
  {
    ans=pow(x,n)%m;
    printf("%d\n",ans);
  }
  return 0;
}

二分取模改进算法:
#include<stdio.h>

int m;

__int64 pow(__int64 a, __int64 n)
{
        __int64 t;
        if(n==0)
    return 1%m;
        if(n==1)    
    return a%m;
        t=pow(a,n/2);
        t=t*t%m;
        if((n&1)==1)    
    t=t*a%m;
        return t;
}    

int main()
{
  int x,n,ans;
  while(scanf("%d%d%d",&x,&n,&m)!=EOF)
  {
    ans=pow(x,n)%m;
    printf("%d\n",ans);
  }
  return 0;
}
二分算法取余

int pow(int x,int n)
{
  if(n==0)
    return 1;
  if(n&1)
    return ((pow(x,n>>1)%m*pow(x,n>>1)%m)*(x%m))%m;
  else
    return ((pow(x,n>>1)%m)*(pow(x,n>>1)%m))%m;
}
废话不多说,直接进入正题。
二分作用在一个全序关系链上,其时间复杂度为O(logn),基本接近常数时间。
1,查找一个(非降)有序链a[1…n]上是否存在某元素val.
int ll=1,rr=n,mid;
while(ll<=rr)
{
        mid=(ll+rr)>>1;
        if(a[mid]==value) break;
        else if(a[mid]>value) rr=mid-1;
        else ll=mid+1;
}
事后检查ll<=rr? 或检查a[mid]==value?即可判断是否存在.

2,查找一个(非降)有序链a[1…n]上是否存在某元素a[i],使得a[i]<=val<a[i+1],即找到最后一个不大于val的元素.
首先if(a[1]>val) 则不存在
否则:
int ll=1,rr=n,mid;
while(ll<rr)
{
        mid=(ll+rr+1)>>1; //+1取天棚
        if(a[mid]<=val) ll=mid; //只要ll<rr.ll就不会==mid.此步改变了ll值,限制了死循环
        else rr=mid-1;
}
a[ll]即为要找的元素。

3,查找一个(非降)有序链a[1…n]上是否存在某元素a[i],使得a[i]<val<=a[i+1],即找到最后一个小于val的元素.
首先if(a[1]>=val) 则不存在
否则:
int ll=1,rr=n,mid;
while(ll<rr)
{
        mid=(ll+rr+1)>>1;    //+1取天棚
        if(a[mid]<val) ll=mid; //只要ll<rr.ll就不会==mid.此步改变了ll值,限制了死循环
        else rr=mid-1;
}
a[ll]即为要找的元素。

4,查找一个(非降)有序链a[1…n]上是否存在某元素a[i],使得a[i-1]<val<=a[i],即找到第一个不小于val的元素.
首先if(a[n]<val) 则不存在
否则:
int ll=1,rr=n,mid;
while(ll<rr)
{
        mid=(ll+rr)>>1;
        if(a[mid]==value) break;
        else if(a[mid]>=value) rr=mid;
        else ll=mid+1;
}
a[rr]即为要找的元素。

5,查找一个(非降)有序链a[1…n]上是否存在某元素a[i],使得a[i-1]<=val<a[i],即找到第一个大于val的元素.
首先if(a[n]<=val) 则不存在
否则:
int ll=1,rr=n,mid;
while(ll<rr)
{
        mid=(ll+rr)>>1;
        if(a[mid]==value) break;
        else if(a[mid]>value) rr=mid;
        else ll=mid+1;
}
a[rr]即为要找的元素。

有三点需要说明:
        一,将以上的'<=','<','>','>='看作一种抽象的关系,而不仅仅是平凡意义上的大于小于运算,即可将以上结论推广。即若将'<'号看作大于运算,则a[i]<=val<a[i+1]表示最后一个大于或等于val的元素等等。
        二,边界可以改变,若范围为a[lft,rht](lft<rht), 则判断条件中的1改为lft,n改为rht,仅此而已。
        三,第一种情况最为简单,事实上其它四种情况也可以套用它的模式,即:
             int ll=1,rr=n,mid;
             while(ll<=rr)
             {
                     mid=(ll+rr)>>1;
                     if(check(mid)) break;// 注意边界处理,如check(mid):a[mid-1]<=value<a[mid]
                                                                //这时mid需要大于左边界,否则会溢出!
                        else if(..) rr=mid-1;
                     else ll=mid+1;
             }之后检查check(mid)是否成立即可。
                
总之,当左右都会步进(显式+1或-1)时,一般不会出错,当只有右边步进时,用mid=(ll+rr)>>1;只有左边步进时,用mid=(ll+rr+1)>>1;意在确保每次循环都会缩小区间范围。    
POJ 3273, Monthly Expense Time Limit: 2000MS    Memory Limit: 65536K
Total Submissions: 4105    Accepted: 1620


Description

Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.

FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.

FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.

    

Input
Line 1: Two space-separated integers: N and M    
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day

    

Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.

    

Sample Input
7 5
100
400
300
100
500
101
400

    

Sample Output
500

    

Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.

    

Source
USACO 2007 March Silver

答案:


#include <iostream>
#include <algorithm>
using namespace std;

int n,m;
int a[100000];
bool ok(int mid)
{
        int i,j,k;
        if(mid<a[0]) return false;
        int cur=a[0],cnt=1;
        for(i=1;i<n;i++)
        {
                if(cur+a[i]<=mid) cur+=a[i];
                else
                {
                        if(mid<a[i]) return false;
                        cnt++;
                        cur=a[i];
                }        
        }        
        return cnt<=m;
}        

int main()
{
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int i,j,k=1,ca,sum;
        while(scanf("%d%d",&n,&m)!=-1)
        {
                sum=0;
                for(i=0;i<n;i++) {scanf("%d",a+i);sum+=a[i];}
                int ll=0,rr=sum;
                int mid;
                while(ll<rr)
                {
                        mid=(ll+rr)>>1;
                        if(ok(mid)) rr=mid;
                        else ll=mid+1;
                }        
                printf("%d\n",rr);
        }
        //system("pause");
        return 0;
}    
1614:取模

Time Limit:1000MS    Memory Limit:65536K
Total Submit:24 Accepted:7    

[Submit]     [Status]     [Discuss]    

Font Size: Aa Aa Aa    
Description
给你三个数X(1<=X<=10^100)、Y(1<=Y<=10^8)、Z(1<=Z<=10^4),你能计算出X^Y%Z的值吗?
Input
输入三个如上所描述的数X、Y、Z。多组输入。

Output
输出X^Y%Z的值。
Sample Input
2 3 5    
12345 2345 345    
123456789123456789 19234321 2341

Sample Output
3
240
1825

Hint
X^Y表示X的Y次方

Source
deamone--SWUST 10.3.21


Source */

#include <iostream>
answer:

#include <string>
using namespace std;

int main()
{
  string x;
  unsigned long int y, z, xLen, xValue, i, result;
  while(cin>>x>>y>>z)
  {
    xValue = 0;
    xLen = x.length();
    if (xLen < 8)
    {
      for (i = 0; i < xLen; i++)
      {
        xValue = xValue * 10 + (int)(x.at(i) - '0');
      }
      xValue %= z;
    }
    else
    {
      i = 0;
      while(i < xLen)
      {
        while(xValue <9999999 && i < xLen)
        {
          xValue =    xValue * 10 + (int)(x.at(i) - '0');
          i++;
        }
        xValue %= z;
      }
    }
    result = 1;
    while(y > 0)
    {
      if (y&1)
      {
        result = (result * xValue) % z;
      }
      y >>= 1;
      xValue = (xValue * xValue) % z;
    }
    cout<<result<<endl;
  }
  return 0;
}


----------我是分割线------------

普通的二分取模:

#include<stdio.h>

int m;

__int64 pow(__int64 a, __int64 n)
{
        __int64 t;
        if(n==0)
    return 1%m;
        if(n==1)    
    return a%m;
        t=pow(a,n/2);
        t=t*t%m;
        if((n&1)==1)    
    t=t*a%m;
        return t;
}    

int main()
{
  int x,n,ans;
  while(scanf("%d%d%d",&x,&n,&m)!=EOF)
  {
    ans=pow(x,n)%m;
    printf("%d\n",ans);
  }
  return 0;
}