真的没有什么会写的东西了QAQ
原题:
As one of the most powerful brushes, zhx is required to give his juniors n problems.
zhx thinks the ith problem's difficulty is i. He wants to arrange these problems in a beautiful way.
zhx defines a sequence {ai} beautiful if there is an i that matches two rules below:
1: a1..ai are monotone decreasing or monotone increasing.
2: ai..an 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.
一句话题意:
求有多少种n排列单峰或单调答案膜p
1<=n,p<=1e18
恩
n个数里选最最大放在第i个位置,选i-1个数放前面并递增,剩下的放后面并递减,这样有c(i-1,n)种
(注意不能简单地挑i个数放前面剩下的放后面,一定要先有一个最大/小的数在峰上
所有的i加起来就有Σ_{i=0}^{n-1}c(i,n)=2^(n-1)种
先递减再递增同理,所以方案数*2=2^n种
全部递增和全部递减多算了一遍,减2,最后答案是2^n-2
因为膜数很大所以快速幂的同时快速乘
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
int rd(){int z=,mk=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')mk=-; ch=getchar();}
while(ch>=''&&ch<=''){z=(z<<)+(z<<)+ch-''; ch=getchar();}
return z*mk;
}
ll n,m;
ll mtp(ll x,ll y){
ll bs=x,z=;
while(y){ if(y&) z=(z+bs)%m; bs=(bs+bs)%m; y>>=;}
return z;
}
ll qcp(ll x,ll y){
ll bs=x,z=;
while(y){ if(y&) z=mtp(bs,z); bs=mtp(bs,bs); y>>=;}
return z;
}
int main(){freopen("ddd.in","r",stdin);
while(scanf("%I64d%I64d",&n,&m)!=EOF){
if(n==){ printf("%I64d\n",n%m); continue;}
printf("%I64d\n",(qcp(,n)-+m)%m);
}
return ;
}