Hello 2020 C. New Year and Permutation

链接

https://codeforces.com/contest/1284/problem/C

题意

对于一个有 n 个不重复数的序列的排列中有多少个区间使 maxpl,pl+1,,prminpl,pl+1,,pr=rl

思路

假设 [l,r] 满足上述等式,设 len=rl+1,对于此区间,其所有排列均满足上述等式,即有 len! 种排列

将此区间看成一个数放入原序列,则原序列中有 nlen+1 个数,有 (nlen+1)! 种排列

而对于区间长度 len 满足 maxpl,pl+1,,prminpl,pl+1,,pr=rl 的共有 nlen+1 种情况

所以对于区间长度 len 共有 (nlen+1)×len!×(nlen+1)!O(n) 即可算出结果

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=250005;
int n,m;
ll fac[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
fac[1]=1;
for(int i=2;i<=n;i++) fac[i]=fac[i-1]*i%m;
ll ans=0;
for(int i=1;i<=n;i++) ans=(ans+fac[i]*fac[n-i+1]%m*(n-i+1)%m)%m;
cout<<ans<<endl;
return 0;
}