QuickPow | ||||||
| ||||||
Description | ||||||
计算A ^ B – K。 由于结果非常大,我们将结果对MOD取模。 (100<A,B<10000, 1< K< MOD < 50). | ||||||
Input | ||||||
多组测试数据。 | ||||||
Output | ||||||
对于每组数据,输出一个整数表示计算的结果。每组测试数据占一行。 | ||||||
Sample Input | ||||||
39 7720 39 47 8856 1898 6 12
| ||||||
Sample Output | ||||||
32 6
| ||||||
Hint | ||||||
Mod == A? | ||||||
Source | ||||||
新生练习赛(2013.11.23) | ||||||
Author | ||||||
RedHat |
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int a,b,k,mod;
while(~scanf("%d%d%d%d",&a,&b,&k,&mod))
{
int ans=1;
a=a%mod;
while(b>0)
{
if(b%2==1)ans=ans%mod*a%mod;
b/=2;
a=(a*a)%mod;
}
ans=(ans-(k%mod)+mod)%mod;
printf("%d\n",ans);
}
}