链接:https://vjudge.net/contest/331993#problem/D
给出3个正整数A B C,求A^B Mod C。
例如,3 5 8,3^5 Mod 8 = 3。
Input3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)Output输出计算结果Sample Input
3 5 8
Sample Output
3
#include<cstdio> #include<iostream> using namespace std; int pow(int a,int b,int c) { int ans=1,base=a;// ans:幂的结果;base:底数a while(b) { if(b & 1) //判断b是不是奇数 { ans=(long long)ans*base%c; } base=(long long)base*base%c; b = b >> 1; //除2 } return ans; } int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); printf("%d ",pow(a,b,c)); }
其实,完全可以当作模板先背过,-。- 虽然我知道它很。。。
a*b%p=a%p*b%p
(a+b)%p=a%p+b%p