指数计算 C++

指数计算

7 月 1 日是建党日,从 1921 年到 2020 年,Communist Party of China已经带领中国人民走过了 99 年。
请计算:7 ^ 2020 mod 1921,其中 A mod B 表示 A 除以 B 的余数。

#include <iostream>
using namespace std;

//递归求解
int power(int b, int p, int s){
    
    
	if(p != 0)
		return (b*power(b,p-1,s)%s);
	else
		return 1;
}

int main(){
    
    
	int b,p,s,res;	//b为基数,P为指数,s为取余 
	cin >> b >> p >>s;
	
	res = power(b,p,s);
	
	cout << res; 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44524918/article/details/109116684