PTA 7-87 计算N^N (20分)(快速幂

7-87 计算N^N (20分)
给出一个整数N,输出N^N(N的N次方)的十进制表示的值,如果这个值超过了214748364,就求结果对214748364的余数。

输入格式:
一个整数T,代表输入数据组数。接下来T行每行一个整数N(1 <= N <= 10^9)。

输出格式:
输出N^N对214748364的余数

输入样例:
2
2
3
输出样例:
4
27
作者
jzfan
单位
阜阳师范大学
代码长度限制
16 KB
时间限制
1000 ms
内存限制
64 MB

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int fast(ll n){
    
    
	ll p=214748364;
	ll ans=1,base=n,a=n,b=n;
	while(b>0){
    
    
		if(b&1){
    
    //位运算次方
			ans=ans*base%p;
		}
		base=base*base%p;
		b>>=1;
	}
	return ans%p;
}
int main(){
    
    
	int t;
	cin>>t;
	while(t--){
    
    
		ll n;
		cin>>n;
		cout<<fast(n)<<endl;
	}

	return 0;
	
}

猜你喜欢

转载自blog.csdn.net/Minelois/article/details/113281174