HDU 5187

超简单的公式题(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
",((ans-2)%p+p)%p);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jie-dcai/p/4340403.html