# [练习][HDU5187]快速幂加强（快速幂+快速乘） zhx's contest

HDU5187

As one of the most powerful brushes, zhx is required to give his juniors n$n$ problems.
zhx thinks the ith$i^{th}$ problem’s difficulty is i$i$. He wants to arrange these problems in a beautiful way.
zhx defines a sequence {ai$a_i$} beautiful if there is an i$i$ that matches two rules below:
1:a1..ai$a_1..a_i$ are monotone decreasing or monotone increasing.
2:ai..an$a_i..a_n$ are monotone decreasing or monotone increasing.
He wants you to tell him that how many permutations of problems are there if the sequence of the problems’ difficulty is beautiful.
zhx knows that the answer may be very huge, and you only need to tell him the answer module p$p$.

2 233
3 5

2
1

【样例说明】

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
#include<set>
using namespace std;

long long getlong()
{
long long sum=0,f=1;
char ch;
for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
if(ch=='-')
{
f=-1;
ch=getchar();
}
for(;isdigit(ch);ch=getchar())
sum=(sum<<3)+(sum<<1)+ch-48;
return sum*f;
}

long long n,p;

long long ksc(long long x,long long y)
{
long long res=0;
for(;y>=1;y=y>>1,x=(x+x)%p)
if(y&1)
res=(res+x)%p;

return res;
}

void ksm()
{
long long ans=1,m=2;
if(n==1)//特判n=1
ans=3;
else
for(;n>=1;n=n>>1,m=ksc(m,m))
if(n&1)
ans=ksc(ans,m);

ans=(ans+p-2)%p;//在有取模的减法中要加一个模数，因为可能原数被模之后比被减数还小，造成答案变成负数

cout<<ans<<'\n';
}

int main()
{
freopen("zhx.in","r",stdin);
freopen("zhx.out","w",stdout);

while(scanf("%I64d",&n)!=EOF)
{
p=getlong();
ksm();
}

return 0;
}