Algorithms raise fast powers



  Algorithms raise fast powers  
Time limit: 1.0s Memory limit: 256.0MB
    
Problem Description
  Given A, B, P, find (A^B) mod P.
input format
  Enter a total of one line.
  The first line has three numbers, N, M, P.
output format
  A total of one line is output, indicating what is required.
sample input
2 5 3
Sample output
2
Data size and conventions
  A total of 10 sets of data
  For 100% data, A and B are non-negative integers in the range of long long, and P is a non-negative integer in int.
 

.

.Quick Power Code: (If you don't understand Quick Power, it is recommended to read this article in Baidu Wenku) Click to open the link

#include<iostream>
using namespace std;
int PD(long long  a,long long b,int c){
		long long  res=1;
		a%=c;
	while(b>0){
		if(b%2==1){//If it is an odd number
			res = res * a% c;
		}
			b/=2;
			a=a*a%c;
	}
	return res;
}
int main(){
		long long a,b;
		int c;
			cin>>a>>b>>c;
	cout<<PD(a,b,c);
	
	
	
	
		return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325695471&siteId=291194637