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.

扫描二维码关注公众号,回复: 4996405 查看本文章

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 66, 66, 77, 66, 77, 55, 66, 66, 88, 66, 77, 55, 66, 66, 77, 66. As n(n+1)2=6n(n+1)2=6, the answer is 99.

题意很简单,因为要求和为n(n+1)/2,所以就是在序列中找一个长度为n且有1到n所有数的串,求数量。

首先很容易想到n的全排列就可以满足条件,剩下的是由相邻的两个排列子串拼凑而成。

那么在两个相邻排列子串拼凑的过程中,如果要满足题目条件,那么两个子串应该有相同的前缀。

例如 1 2 3 4 5 1 2 3 5 4  

相同前缀为 1 2 3   那么这两个子串可以拼凑3个出来{2 3 4 5 1}{3 4 5 1 2}{4 5 1 2 3}

并且全排列是按字典序排序,那么相同前缀的一定相邻。

再控制相同前缀之后,就可以计算有这样前缀的子串的个数

sum=\left ( n-i \right )!  i是前缀的长度,sum再-1即是这个前缀拼凑可以得到新串数目。

其中长度为i的前缀的个数sum=C_n^i*i!=\frac{n!}{\left ( n-i \right )!}

枚举i即可得到答案ans=n!+\sum _{i=1}^{n-2}\frac{n!}{\left ( n-i \right )!}*\left ( (n-i)!-1 \right )

#include "bits/stdc++.h"
using namespace std;
const long long inf = 2e9;
const int mod = 998244353 ;
long long  fac[1000004];
long long qk(long long a,long long n)
{
    long long ans=1;
    while (n)
    {
        if(n&1)ans=ans*a%mod;
        n>>=1;
        a=a*a%mod;
    }
    return ans;
}
int main()
{
    int n;
    while(cin>>n){
        fac[1]=1;
        for (int i = 2; i <= n; ++i) {
            fac[i]=fac[i-1]*i%mod;
        }
        long long ans=fac[n];
        for (int i = 1; i <= n-2; ++i) {
            ans=(ans  +  fac[n]  *  qk(fac[n-i],mod-2)  %  mod  *  (fac[n-i]-1)  %mod  )%mod;
        }
        printf("%lld\n",ans);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/86307373