hdu 5187 zhx's contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3127    Accepted Submission(s): 1008

 

 

经过打表发现

0,0,4,12,28,60,124,252,508,1020,2044,4092,8188,

如果不算单增单减序列,只算先单增后单减 先单减后单增

有这样的规律 a[n]=a[n-1]*2+4;

再加上只单增只单减

答案再+2

ans=(2n%p-2+p)%p;

数据过大 

只用快速幂会炸long long

所以需要快速乘

屠龙宝刀点击就送

#include <cstdio>
typedef long long LL;
LL n,p;
LL ksc(LL a,LL b,LL Mod)
{
    LL ret=0,base=a;
    for(;b;b>>=1,base=(base*2)%Mod)
     if(b&1)
      ret=(ret+base)%Mod;
    return ret;
}
LL ksm(LL a,LL b,LL Mod)
{
    LL r=1,base=a;
    for(;b;b>>=1,base=ksc(base,base,Mod)%Mod)
     if(b&1)
      r=ksc(r,base,Mod)%Mod;
    return r;
}
int main()
{
    for(;scanf("%I64d%I64d",&n,&p)!=EOF;)
        printf("%I64d\n",(ksm(2,n,p)%p-2%p+p)%p);
    return 0;
}

 

posted @ 2017-09-13 15:09  杀猪状元  阅读(131)  评论(0编辑  收藏  举报