先放代码,注释里做了详细解释,变量名已经专门设置为了易读懂的名字:
#include <bits/stdc++.h>
using namespace std;
long long base_number,index_number,mod,answer;
inline long long fast_read(){
long long x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') if(ch=='-')f=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x*f;
}
long long quickpower(long long base,long long index){
//when understanding, remember to regard "index" as a binary number
long long ans=1;
long long transbase=base;//transbase=base^(2^n)
while(index > 0){//index is a changing binary(二进制的) number;if index isn't used up:
//& is a bitwise operation symbol,we use "index & 1" to judge whether the last number of index in binarity is "1",if so:
if(index & 1) ans =ans * transbase % mod;//times ans with corresponding transbase;
transbase =transbase * transbase % mod;//times itself, make a^(2^n) to a^(2^(n+1))
index >>= 1;//bitwise operation "index shr 1",equal to "index/=2",but faster.
}
return ans;
}
int main(){
base_number=fast_read();index_number=fast_read();mod=fast_read();
printf("%ld^%ld ",base_number,index_number);
answer=quickpower(base_number,index_number) % mod;
printf("mod %ld=%ld",mod,answer);
return 0;
}
蒟蒻对各位大佬%%%
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zlt_jackie/article/details/83450585