正题
题目链接:https://www.luogu.com.cn/problem/P3200
题目大意
求一个长度为 2 ∗ n 2*n 2∗n的排列要求
- 奇数位和偶数位分别递增
- 相邻的偶数位大于奇数位
解题思路
可以看做是一个 2 ∗ n 2*n 2∗n的序列按顺序填进奇数和偶数位,然后因为第二个要求所以奇数位在任何时候都得比偶数位的要多,就转换为了求第 n n n个卡特兰数了。
然后因为 p p p不是质数所以要质因数分解来除数,时间复杂度 O ( n log n ) O(n\log n) O(nlogn)
c o d e code code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e6+10;
int n,p,tot,pri[N],cnt[N];
bool v[N];
int main()
{
scanf("%d%d",&n,&p);
for(int i=2;i<=2*n;i++){
if(!v[i])pri[++tot]=i;
for(int j=1;j<=tot&&i*pri[j]<=n;j++){
v[i*pri[j]]=1;
if(i%pri[j]==0)break;
}
}
for(int i=n+2;i<=2*n;i++){
int x=i;
for(int j=1;pri[j]*pri[j]<=x&&j<=tot;j++)
while(x%pri[j]==0)x/=pri[j],cnt[j]++;
if(x!=1)cnt[lower_bound(pri+1,pri+1+tot,x)-pri]++;
}
for(int i=1;i<=n;i++){
int x=i;
for(int j=1;pri[j]*pri[j]<=x&&j<=tot;j++)
while(x%pri[j]==0)x/=pri[j],cnt[j]--;
if(x!=1)cnt[lower_bound(pri+1,pri+1+tot,x)-pri]--;
}
long long ans=1;
for(int i=1;i<=tot;i++)
while(cnt[i])ans=ans*pri[i]%p,cnt[i]--;
printf("%d\n",ans);
}