题意
给你一个n,一个p,
若1-n的单峰序列有ans个,
即先增后减或先减后增的序列均满足要求,
求ans%p
思路来源
https://blog.csdn.net/qq_40379678/article/details/79149727
题解
有些博主的题解真是清晰易懂,十分感谢
自己还是太辣鸡了,一遇组合数学就懵逼
考虑1,2,3,…,n的单增序列,
显然,中间那个单峰只能是n,
那么把左边(n-1)个数取1个放在右边,就是
…
取(n-1)个放在右边,就是
先单增后单减序列ans=
反过来,先单减后单增序列ans=
这样最后sum就是
特判n=1,sum是1,p=1时sum%p==0,其余sum%p==1
注意n、p均1e18,相乘爆ll,所以要用快速乘,是为记
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
ll mul(ll x,ll n,ll mod)
{
ll res=0;
while(n)
{
if(n&1)res=(res+x);
if(res>=mod)res%=mod;
x=x+x;
if(x>=mod)x%=mod;
n/=2;
}
return res;
}
ll modpow(ll x,ll n,ll mod)
{
ll res=1;
while(n)
{
if(n&1)res=mul(res,x,mod);
if(res>=mod)res%=mod;
x=mul(x,x,mod);
if(x>=mod)x%=mod;
n/=2;
}
return res;
}
ll n,p;
ll solve(ll n,ll p)
{
if(n==1)
{
if(p==1)return 0;
else return 1;
}
else
{
ll res=1;
res=modpow(2,n,p)-2;
return (res+p)%p;
}
}
int main()
{
while(~scanf("%lld%lld",&n,&p))
{
printf("%lld\n",solve(n,p));
}
return 0;
}