首页 > 其他 > 详细

HDU 5187

时间:2015-03-15 22:43:43      阅读:333      评论:0      收藏:0      [点我收藏+]

超简单的公式题(2^n-2)。不过,要过可不容易,因为会爆64位,所以,可以使用快速乘法。

#include <iostream>
#include <cstdio>
#include <algorithm>
#define LL unsigned __int64
using namespace std;

LL n,p;


LL mul(LL x,LL m,LL p)///????
{
    LL re=0;
    while(m){
        if(m&1){
            re=(re+x)%p;
        }
        x=(x+x)%p;
        m>>=1;
    }
    return re;
}


LL quick(LL n,LL p){
    LL ret=1ll,t=2;
    while(n){
        if(n&1) ret=mul(ret,t,p);
        n>>=1;
        t=mul(t,t,p);
    }
    return ret;
}

int main(){
    while(scanf("%I64d%I64d",&n,&p)!=EOF){
        if(n==1ll){
            if(p>1)puts("1");
            else puts("0");
            continue;
        }
        LL ans=quick(n,p);
        printf("%I64d\n",((ans-2)%p+p)%p);
    }
    return 0;
}

 

HDU 5187

原文:http://www.cnblogs.com/jie-dcai/p/4340403.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!