取余运算

Problem Description

输入b,p,k的值,求b^p mod k的值。

Input

输入有多组数据,每组数据为一行三个数b,p,k,其中b,p,k*k为长整型数。

Output

对于每组数据输出b^p mod k的值。

Sample Input

2 10 9 

Sample Output

2^10 mod 9=7
//关键字:快速幂取模
//标程:
#include<stdio.h>
#include<string.h>
__int64 f(__int64 a,__int64 b,__int64 c)
{
   __int64 sum=1;
   while(b)
   {
      if(b%2==1) sum=(sum*a)%c;
      a=(a*a)%c;
      b/=2; 
   }
    return sum;
}
int main()
{
    //freopen("a.txt","r",stdin);
    __int64 a,b,c;
    while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
        printf("%I64d^%I64d mod %I64d=%I64d\n",a,b,c,f(a,b,c));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值