[Hello 2020] C. New Year and Permutation (组合数学)
[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;
}
[Hello 2020] C. New Year and Permutation (组合数学)的更多相关文章
- Wannafly Winter Camp 2020 Day 6D 递增递增 - dp,组合数学
给定两个常为 \(n\) 的序列 \(l_i,r_i\),问夹在它们之间 ( \(\forall i, l_i \leq a_i \leq r_i\) ) 的不降序列的元素总和. Solution 先 ...
- HDU 6044 Limited Permutation 读入挂+组合数学
Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...
- codeforces 1284C. New Year and Permutation(组合数学)
链接:https://codeforces.com/problemset/problem/1284/C 题意:定义一个framed segment,在区间[l,r]中,max值-min值 = r - ...
- hdu 6044 : Limited Permutation (2017 多校第一场 1012) 【输入挂 组合数学】
题目链接 参考博客: http://blog.csdn.net/jinglinxiao/article/details/76165353 http://blog.csdn.net/qq_3175920 ...
- Wannafly Camp 2020 Day 1C 染色图 - 组合数学,整除分块
定义一张无向图 G=⟨V,E⟩ 是 k 可染色的当且仅当存在函数 f:V↦{1,2,⋯,k} 满足对于 G 中的任何一条边 (u,v),都有 f(u)≠f(v). 定义函数 g(n,k) 的值为所有包 ...
- Codeforces Global Round 7 C. Permutation Partitions(组合数学)
题意: 给你 n 长全排列的一种情况,将其分为 k 份,取每份中的最大值相加,输出和的最大值和有多少种分法等于最大值. 思路: 取前 k 大值,储存下标,每两个 k 大值间有 vi+1 - vi 种分 ...
- Colorful Bricks CodeForces - 1081C ( 组合数学 或 DP )
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in t ...
- Algorithm: Permutation & Combination
组合计数 组合数学主要是研究一组离散对象满足一定条件的安排的存在性.构造及计数问题.计数理论是狭义组合数学中最基本的一个研究方向,主要研究的是满足一定条件的排列组合及计数问题.组合计数包含计数原理.计 ...
- Mysterious Crime CodeForces - 1043D (思维+组合数学)
Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody ...
随机推荐
- idea开发web项目${pageContext.request.contextPath}无法转义
web-app版本问题,我的web.xml中头文件的配置是: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web ...
- 常用es5和es6语法区别,以及三个点的用法
链接:https://www.jianshu.com/p/b4d48e9846e7 //三个点 链接:https://blog.csdn.net/qiladuo1207/article/details ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 位运算
bitwise_and() 函数对数组中整数的二进制形式执行位与运算. import numpy as np print ('13 和 17 的二进制形式:') a,b = 13,17 print ( ...
- js指定范围指定个数的不重复随机数
今天偶然看到的 比如要生成 1-100范围之内的10个不重复随机数,代码就可以这么写 var arr = []; for (var i = 1; i <=100; i++) { arr.push ...
- Python读取MNIST数据集
MNIST数据集获取 MNIST数据集是入门机器学习/模式识别的最经典数据集之一.最早于1998年Yan Lecun在论文: Gradient-based learning applied to do ...
- codeforces Round #611
这种凌晨场真的折寿 就过了四题,8wa结尾心态炸裂,求别被hack,再hack就要爬了 A2 B8 C38(1) E1:58(7) D题感觉可以写,但是没有时间看了.幸好E最后发现了自己的 ...
- phpsduty安装SSL证书,apache不能启动,解决方案
最近给客户开发微信小程序,因为本人也不太懂服务器的安装,(大神勿喷),顾个人一直使用的集成环境,原来一直是客户提供主机什么的,都是我们和客户说一下需要什么环境啊,配置啊,之类的,这次首次自己动手. 废 ...
- Python3.5学习之旅——day4
本节内容 1.装饰器 2.迭代器与生成器 3.内置方法 4.软件目录结构规范 一.装饰器 装饰器是一个用来装饰其他函数的工具,即为其他函数添加附加功能,其本质就是函数. 装饰器需要遵循的以下两个原则: ...
- 奖学金(0)<P2007_1>
奖学金 (scholar.pas/c/cpp) [问题描述] 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金.期末,每个学生都有3门课的成绩:语文.数学.英语.先按总分 ...
- Vacuum Pump Manufacturer Introduction: Airless Pump Bottle
Fillable vacuum pump bottle with matt silver aluminum base and cap and shiny silver aluminum collar. ...