QuickPow
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 142(88 users) Total Accepted: 96(85 users) Rating: Special Judge: No
Description
计算A ^ B – K。 由于结果非常大,我们将结果对MOD取模。
(100
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int PowerMod(int a, int b, int c)/*快速幂博客网址http://blog.csdn.net/qq_32866009/article/details/50807651*/
{
int ans = 1;
a=a%c;
while(b>0)
{
if(b%2==1)
ans = (ans * a) % c;
b = b/2;
a = (a * a) % c;
}
return ans;
}
int main()
{
int a,b,k,mod;
while(~scanf("%d%d%d%d",&a,&b,&k,&mod))
{
int sum=PowerMod(a,b,mod);
int sum1=k%mod;
if(sum<sum1)
{
printf("%d\n",(sum+mod-sum1%mod)%mod);
}
else printf("%d\n",(sum-sum1)%mod);
}
return 0;
}