[Hello 2020] C. New Year and Permutation (组合数学)

C. New Year and Permutation

time limit per test

1 second

memory limit per test

1024 megabytes

input

standard input

output

standard output

Recall that the permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

A sequence aa is a subsegment of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l,r][l,r], where l,rl,r are two integers with 1≤l≤r≤n1≤l≤r≤n. This indicates the subsegment where l−1l−1 elements from the beginning and n−rn−r elements from the end are deleted from the sequence.

For a permutation p1,p2,…,pnp1,p2,…,pn, we define a framed segment as a subsegment [l,r][l,r] where max{pl,pl+1,…,pr}−min{pl,pl+1,…,pr}=r−lmax{pl,pl+1,…,pr}−min{pl,pl+1,…,pr}=r−l. For example, for the permutation (6,7,1,8,5,3,2,4)(6,7,1,8,5,3,2,4) some of its framed segments are: [1,2],[5,8],[6,7],[3,3],[8,8][1,2],[5,8],[6,7],[3,3],[8,8]. In particular, a subsegment [i,i][i,i] is always a framed segments for any ii between 11 and nn, inclusive.

We define the happiness of a permutation pp as the number of pairs (l,r)(l,r) such that 1≤l≤r≤n1≤l≤r≤n, and [l,r][l,r] is a framed segment. For example, the permutation [3,1,2][3,1,2] has happiness 55: all segments except [1,2][1,2] are framed segments.

Given integers nn and mm, Jongwon wants to compute the sum of happiness for all permutations of length nn, modulo the prime number mm. Note that there exist n!n! (factorial of nn) different permutations of length nn.

Input

The only line contains two integers nn and mm (1≤n≤2500001≤n≤250000, 108≤m≤109108≤m≤109, mm is prime).

Output

Print rr (0≤r<m0≤r<m), the sum of happiness for all permutations of length nn, modulo a prime number mm.

Examples

input

Copy

1 993244853

output

Copy

1

input

Copy

2 993244853

output

Copy

6

input

Copy

3 993244853

output

Copy

32

input

Copy

2019 993244853

output

Copy

923958830

input

Copy

2020 437122297

output

Copy

265955509

Note

For sample input n=3n=3, let's consider all permutations of length 33:

  • [1,2,3][1,2,3], all subsegments are framed segment. Happiness is 66.
  • [1,3,2][1,3,2], all subsegments except [1,2][1,2] are framed segment. Happiness is 55.
  • [2,1,3][2,1,3], all subsegments except [2,3][2,3] are framed segment. Happiness is 55.
  • [2,3,1][2,3,1], all subsegments except [2,3][2,3] are framed segment. Happiness is 55.
  • [3,1,2][3,1,2], all subsegments except [1,2][1,2] are framed segment. Happiness is 55.
  • [3,2,1][3,2,1], all subsegments are framed segment. Happiness is 66.

Thus, the sum of happiness is 6+5+5+5+5+6=326+5+5+5+5+6=32.

题意:

给定一个数字n和 一个质数m,

问n的所有全排列的good值sum和,每一个排列的good值使有多少个点对pair(i,j) 是framed subsegment

使 \(i<=j\) 且 \(\max\{p_l, p_{l+1}, \dots, p_r\} - \min\{p_l, p_{l+1}, \dots, p_r\} = r - l\)

思路:

如果\([l,r]\) 是framed subsegment ,那么所有\([min_{i = l}^{r} p_i, max_{i = l}^{r} p_i]\) 范围内的数都必须在区间\([l,r]\) 中。

我们定义 区间\([l,r]\) 的长度 \(len= r-l+1\) ,那么 长度为len的framed subsegment 的范围一共有n-len+1 种。

例如 n=3,len=2,有2种范围 : ①\([1,2]\) ②\([2,3]\)

而长度为len的framed subsegment 又有\(len!\) 种排列方式

例如: 范围是\([1,2]\) 有\(2!\) 种排列方式,(1,2) and (2 ,1 ) 且都符合要求。

除了 长度为len的framed subsegment 的范围 的数有 n-len 个,它们任意排列后再将framed subsegment 整体插入都对满足条件没有影响。

所以任意排列有\((n-len)!\) 方式,插板法 有\(n-len+1\) 种方式。

所以 长度为len的framed subsegment 的方式个数为:

\((n-len+1)^2*fac[len]*fac[n-len]\),fac[i] 为 i的阶乘

所以我们只需要预处理0~n的所有阶乘后在\([1,n]\)范围内枚举len 即可得到答案。

ps:记得取模。

代码:

ll m;
int n;
ll fac[maxn]; int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
n = readint();
m = readll();
fac[0] = 1ll;
repd(i, 1, n)
{
fac[i] = fac[i - 1] * i % m;
}
ll ans = 0ll;
repd(i, 1, n)
{
ans += (1ll * (n - i + 1) % m * fac[i] % m * fac[n - i] % m * (n - i + 1) % m);
ans %= m;
}
printf("%lld\n", ans );
return 0;
}

最新文章

  1. javaScript中的页面传值
  2. AP创建会计科目
  3. has_many :through VS has_and_belongs_to_many
  4. URI和URL的区别
  5. vim vi 及其相关插件的使用
  6. powerdesigner 15 如何导出sql schema
  7. BZOJ 3931: [CQOI2015]网络吞吐量( 最短路 + 最大流 )
  8. WPF的MVVM
  9. BZOJ 2741: 【FOTILE模拟赛】L [分块 可持久化Trie]
  10. 【状压dp】Bzoj2064 分裂
  11. JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)
  12. SpringCloud(9)---mysql实现配置中心
  13. style.left offsetLeft offsetwidth clientLeft clientWidth scrollLeft scrollWidth
  14. 一段自适应的CSS代码
  15. Docker 配置
  16. 纯css实现顶部进度条随滚动条滚动
  17. mongodb 安装(官方说明链接)
  18. VIO回顾:从滤波和优化的视角
  19. JVM参数简述
  20. snapkit更新约束崩溃的问题

热门文章

  1. DHCP原理及报文格式
  2. nm
  3. docker+hexo 搭建博客
  4. leetcode 0216
  5. Python学习第二十一课——Mysql 对数据库的基本操作
  6. Python安装numpy,pandas慢,超时报错,下载不了的解决方法
  7. RPC远程服务调用
  8. jqGird错误“decimalSeparator”的解决办法
  9. 记处理spring-devtools 和 通用mapper 使用问题
  10. 最全Pycharm教程(39)——Pycharm版本控制之本地Git用法