a^b fast power

a^b fast power

Note that the last modulus of ans

#include<iostream>
using namespace std;

#define ll long long

ll a,b,p ;

int main()

{
    
    
	cin>>a>>b>>p;
	
	ll ans=1;

		
	while(b)
	{
    
    
		if(b&1)
		{
    
    
			ans=(ans*a)%p;
			
		}
		a=a*a %p;
		
		b=b>>1;
	}
	
	cout<<ans%p;  //注意当b==0的时候,没有经过while循环,需要再度取模 
	
	
	return 0;
 } 

Guess you like

Origin blog.csdn.net/weixin_45448563/article/details/113633257