cf1284C New Year and Permutation

分别统计每一种长度的区间对答案的贡献即可。

枚举长度$L$,$L$从1到$n$。

对于固定的$L$,对答案的贡献为:

$(n-L+1) \times L ! \times (n-L+1) ! $

$(n-L+1)$表示长度为$L$的连续的数有多少种可能。

$L!$表示这$L$个数的全排列。

$(n-L+1) ! $ 表示把L个数看成一个整体,和剩下的$n-L$个数合在一起形成$(n-L+1)$个数的全排列。

https://codeforces.com/contest/1284/submission/68185410

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 typedef long long ll;
 4 const int N = 260000;
 5 ll n,m;
 6 ll f[N];
 7 
 8 int main() {
 9     ios::sync_with_stdio(0);
10     cin>>n>>m;
11     f[0] = 1;
12     for(int i=1;i<=n;i++) {
13         f[i] = f[i-1] * i % m;
14     }
15     ll ans = n * f[n] % m;
16     for(int L=2;L<=n;L++) {
17         ll tmp = f[L] * (n-L+1) % m;
18         int j = n - L + 1;
19         tmp = tmp * f[j] % m;
20         ans = (ans+tmp) % m;
21     }
22     cout<<ans<<endl;
23     return 0;
24 }
25 
26  
View Code

猜你喜欢

转载自www.cnblogs.com/scnucjh/p/12153516.html
今日推荐