codeforces 1284C. New Year and Permutation (combinatorics)

Link: https: //codeforces.com/problemset/problem/1284/C

The meaning of problems: the definition of a framed segment, in the interval [l, r] in, max value -min value = r - l. Seeking sequence consisting of 1-n, the number of all the framed segment% m

Ideas: combinatorics push a conclusion. For example, assume a sequence consisting of 1 to n, seek length is framed segment k, then a sequence of maximum value - minimum value = k, for example, n = 5, k = 3, the framed segment must be 123 or 2 34 or 345, can be observed with a length of framed segment k is necessarily continuous, they can be considered a single whole, so that the overall length of the sequence becomes n - k + 1, the internal length is k, a combination of internal type It is K! a, the overall combination of species is (n-k + 1)! Length framed segment of the type of k is (123,234,345) n - k + 1 species, so the length k framed segment final answer is (n-k + 1) * (n-k + 1)! * k !, pretreatment can look factorial.

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<queue> 
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn = 2e5+5e4+5;
10 ll fac[maxn];
11 int main(){
12     ll n,m;
13     cin>>n>>m;
14     fac[1] = 1;
15     for(int i = 2;i<=n;i++){
16         fac[i] = (fac[i-1]*i)%m;
17     }
18     ll ans = 0;
19     for(int i = 1;i<=n;i++){
20         ans +=((n+1-i)*(fac[i])%m)*(fac[n+1-i])%m;
21         ans = ans%m;
22     }
23     // 1 2 3
24     cout<<ans%m;
25     return 0;
26 }

Guess you like

Origin www.cnblogs.com/AaronChang/p/12185455.html