codeforces 1091D New Year and the Permutation Concatenation 组合数

D. New Year and the Permutation Concatenation

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Let nn be an integer. Consider all permutations on integers 11 to nn in lexicographic order, and concatenate them into one big sequence pp . For example, if n=3n=3 , then p=[1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1]p=[1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1] . The length of this sequence will be n⋅n!n⋅n! .

Let 1≤i≤j≤n⋅n!1≤i≤j≤n⋅n! be a pair of indices. We call the sequence (pi,pi+1,…,pj−1,pj)(pi,pi+1,…,pj−1,pj) a subarray of pp . Its length is defined as the number of its elements, i.e., j−i+1j−i+1 . Its sum is the sum of all its elements, i.e., ∑jk=ipk∑k=ijpk .

You are given nn . Find the number of subarrays of pp of length nn having sum n(n+1)2n(n+1)2 . Since this number may be large, output it modulo 998244353998244353 (a prime number).

Input

The only line contains one integer nn  (1≤n≤1061≤n≤106 ), as described in the problem statement.

Output

Output a single integer — the number of subarrays of length nn having sum n(n+1)2n(n+1)2 , modulo 998244353998244353 .

Examples

Input

Copy

3

Output

Copy

9

Input

Copy

4

Output

Copy

56

Input

Copy

10

Output

Copy

30052700

Note

In the first sample, there are 1616 subarrays of length 33 . In order of appearance, they are:

[1,2,3][1,2,3] , [2,3,1][2,3,1] , [3,1,3][3,1,3] , [1,3,2][1,3,2] , [3,2,2][3,2,2] , [2,2,1][2,2,1] , [2,1,3][2,1,3] , [1,3,2][1,3,2] , [3,2,3][3,2,3] , [2,3,1][2,3,1] , [3,1,3][3,1,3] , [1,3,1][1,3,1] , [3,1,2][3,1,2] , [1,2,3][1,2,3] , [2,3,2][2,3,2] , [3,2,1][3,2,1] .

Their sums are 6 ,6, 7 ,6 ,7,5,6,6, 8 , 6 , 7 , 5 , 6 , 6 , 7 , 6 . As n(n+1)/2=6 , the answer is 9 .

题意:给你一个数n,让你求在n的全排列依次排列的序列中找到多少个区间满足区间长度为n,该区间内数的和为n*(n+1)/2。

思路:很容易想到,全排列中的每一个排列的和就是n*(n+1)/2,全排列有n!个,然后就是两个相邻排列之间可以组合的了,我们用一个固定长度为n的滑动窗口来类比所选区间,当窗口向右移动时,左边每出去一个数右边就会进去一个数,想要区间和为    n*(n+1)/2那么区间内所数必须是(1~n)的全排列,因为是全排列顺序排列,所以只可能是出去和进去的数是相同的(有多个的话这多个数顺序也必须相同的,比如:....,45 321,54123,45 123 和123 45都满足和为15 但是是顺序的全排列这是不可能出现的),终上所述只要两个相邻的全排列相同前缀的就可以满足条件,我们有假设长度为i的相同前缀,有多少长度就可以分多少个区间出来,还是由于是全排列,所以这i个也是全排列,有A(n,i)选择,剩下后面的n-i个数还是全排列,最后要确保不重不漏,确保只有长度严格为i的前缀相同,那么在相邻两个排列中第i+1个必须不同,所以第i+1位一定是剩下n-i个中的一个,需要的是两个相邻的所以有n-i-1中可行的两个相邻排列。最后三者相乘取sigma,得到结果,说的比较生硬,集体见代码。比赛的时候我真的差点就推出来了。

#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<iterator>
#include<set>
#include<map>
#include<bitset>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=1e6+5;
const int inf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
const int mod=998244353;
const double e=exp(1.0);
const double pi=acos(-1);
const double eps=1e-6;
int gcd(int a,int b)
{
	if(b==0)return a;
	return gcd(b,a%b);
}
ll quick_pow(ll a,int n)
{
	ll ans=1;
	while(n)
	{
		if(n&1)ans=(ans*a)%mod;
		n/=2;
		a=(a*a)%mod;
	}
	return ans;
}
ll fac[maxn],inv[maxn];
ll C(ll n,ll m)
{
	return ((fac[n]*inv[m])%mod*inv[n-m])%mod;
}
int main()
{
	fac[0]=1ll;
	fac[1]=1ll;
	inv[1]=1ll;
	for(int i=2;i<maxn-2;i++)
	{
		fac[i]=fac[i-1]*i%mod;
		inv[i]=quick_pow(fac[i],mod-2);
	}
	int n;
	while(scanf("%d",&n)!=EOF)
	{
	ll ans=fac[n];
	for(int i=1;i<=n-2;i++)//相邻的两个排列最后两个数一定是不同的 
	ans=(ans+i*C(n,i)%mod*fac[i]%mod*(n-i-1)%mod)%mod;
	cout<<ans<<endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章