模乘运算和模幂运算

inline unsigned __int64 MulMod(unsigned __int64 a,unsigned __int64 b,unsigned __int64 n)//模乘运算即计算两个数的乘积然后取模
{
	return (a % n)*(b % n )% n;
}

unsigned __int64 PowMod(unsigned __int64 base,unsigned __int64 pow,unsigned __int64 n)//模幂运算即首先计算某数的若干次幂,然后对其结果进行运算
{
	unsigned __int64  a=base, b=pow, c=1;
	while (b)
	{
		while( !(b & 1) )
		{
			b>>=1;
			a= MulMod(a, a, n);
		}
		b--;
		c=MulMod(a, c, n);
	}
	return c;
}

你可能感兴趣的:(C++,c,C#)