快速乘法+快速幂

http://acm.hdu.edu.cn/showproblem.php?pid=5950

#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
LL fast_multi(LL a,LL b,LL mod)
{
    LL res=0,base=a;
    while(b)
    {
        if(b&1) res=(res+base)%mod;
        b>>=1;
        base=(base+base)%mod;
    }
    return res;
}
LL pow_mod(LL a,LL b,LL mod)
{
    LL res=1,base=a;
    while(b)
    {
        if(b&1) res=fast_multi(res,base,mod);
        base=fast_multi(base,base,mod);
        b>>=1;
    }
    return res;
}
int main()
{
   LL b,mod;
   while(scanf("%I64d%I64d",&b,&mod)!=EOF)
   {
       if(b==1)
       {
           if(mod==1) printf("0\n");
           else printf("1\n");

       }
      else  printf("%I64d\n",(pow_mod(2,b,mod)-2+mod)%mod);
   }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容