poj1995快速幂取余

快速幂运算:

View Code
int pow(int a,int n)
{
    int rs=1;
    while(n)
    {
    if(n&1)
      rs=rs*a;
    a=a*a;
    n=n>>1;
  }
  return rs;

}

快速幂取余:

//求a^b%n,O(logb)
__int64 get_mi_mod(__int64 a,__int64 b,int n)
{
	if(0 == a)
		return 0;
	if(0 == b)
		return 1;
	__int64 rs=1;
	while(b)
	{
		if(b&1)
			rs=(rs*a)%n;
		a=(a*a)%n;
		b>>=1;
	}
	return rs;
}

 

posted on 2011-11-10 15:01  buptLizer  阅读(1105)  评论(1编辑  收藏  举报

导航