C. New Year and Permutation

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的阶乘,毫无疑问一定是要用阶乘写的,所以首先利用组合数学把1-n的阶乘进行对模取余计算,存到数组a中,可以把i个数看做一个整体,对于排列是可以分成n-1个+1,n-2个+1....1个加(n-1)个,对于i个时他的排列方式应该有i!种,对应的n-i个中加上把i个数看成一个整体的排列方式具有(n-i+1)!种这仅仅是对于具有长度为i的子序列,按这样排列会具有(n-i+1)个满足这样的子序列,所以对应(i,n-i+1)它的排列方式具有i!*(n-i+1)!*(n-i+1)种,所以对其从1-n开始进行累加并对mod进行取模运算即可。

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
long long a[N];
int main(){
	long long n,m;
	cin>>n>>m;
	a[1]=1;
	for(int i=2;i<=n;i++){
		a[i]=a[i-1]*i%m;
	}
	long long sum=0;
	for(int i=1;i<=n;i++){
		sum=(sum+a[i]*a[n-i+1]%m*(n-i+1)%m)%m; 
	}
	cout<<sum<<"\n";
}

用C++编写程序,实现以下问题2、题目ID Codes(POJ1146) Time Limit: 1000MS Memory Limit: 10000K 描述: It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical measure--all citizens are to have a tiny microcomputer surgically implanted in their left wrists. This computer will contains all sorts of personal information as well as a transmitter which will allow people's movements to be logged and monitored by a central computer. (A desirable side effect of this process is that it will shorten the dole queue for plastic surgeons.) An essential component of each computer will be a unique identification code, consisting of up to 50 characters drawn from the 26 lower case letters. The set of characters for any given code is chosen somewhat haphazardly. The complicated way in which the code is imprinted into the chip makes it much easier for the manufacturer to produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters. Thus, once a set of letters has been chosen all possible codes derivable from it are used before changing the set. For example, suppose it is decided that a code will contain exactly 3 occurrences of a', 2 of b' and 1 of c', then three of the allowable 60 codes under these conditions are: abaabc abaacb ababac These three codes are listed from top to bottom in alphabetic order. Among all codes generated with this set of characters, these codes appear consecutively in this order. Write a program to assist in the issuing of these identification codes. Your program will accept a sequence of no more than 50 lower case letters (which may contain repeated characters) and print the successor code if one exists or the message No Successor' if the given code is the last in the sequence for that set of characters. 输入: Input will consist of a series of lines each containing a string representing a code. The entire file will be terminated by a line consisting of a single #. 输出: Output will consist of one line for each code read containing the successor code or the words 'No Successor'. 样例输入 abaacb cbbaa # 样例输出 ababac No Successor
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值