D. New Year and the Permutation Concatenation(打表找规律)

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/85546043

http://codeforces.com/contest/1091/problem/D

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 n

be an integer. Consider all permutations on integers 1 to n in lexicographic order, and concatenate them into one big sequence p. For example, if n=3, then 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!

.

Let 1≤i≤j≤n⋅n!

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

.

You are given n

. Find the number of subarrays of p of length n having sum n(n+1)2. Since this number may be large, output it modulo 998244353

(a prime number).

Input

The only line contains one integer n

 (1≤n≤106

), as described in the problem statement.

Output

Output a single integer — the number of subarrays of length n

having sum n(n+1)2, modulo 998244353

.

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 16

subarrays of length 3

. In order of appearance, they are:

[1,2,3]

, [2,3,1], [3,1,3], [1,3,2], [3,2,2], [2,2,1], [2,1,3], [1,3,2], [3,2,3], [2,3,1], [3,1,3], [1,3,1], [3,1,2], [1,2,3], [2,3,2], [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

解题思路:

比赛的时候一头雾水。赛后打了个表也没发现什么规律。还是观察力少了些。

打表可发现:

3——————9

4——————56

5——————395

6——————3084

(因为连起来的排列组合的sum一定是n*(n-1)/2,所以)

3——————9=3!+3=3!+1*3

4——————56=4!+32=4!+4*8

5——————395=5!+275=5!+5*55

6——————3084=6!+2364=6!+6*394

emmmmmmmmmm....这样就推出来了。也是很有趣。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod=998244353;
const int maxn=1000000+100;
LL P[maxn];
int main()
{
    LL n;
    while(scanf("%lld",&n)!=EOF){
        LL fac;
        P[1]=1;
        P[2]=2;
        P[3]=9;
        fac=6;
        for(int i=4;i<=n;i++){
            fac=fac*i%mod;
            P[i]=(((P[i-1]-1)%mod*i)%mod+fac)%mod;
        }
        printf("%lld\n",P[n]);
    }
}

打表代码:

#include<bits/stdc++.h>
using namespace std;
int b[1000000];
int bb[1000000];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            b[i]=i;
            bb[i]=i;
        }
        int index=n;
        while(next_permutation(b+1,b+n+1)){
            for(int i=1;i<=n;i++){
                index++;
                bb[index]=b[i];
            }
        }
        /*cout<<"index:"<<index<<endl;
        for(int i=1;i<=index;i++){
            cout<<bb[i]<<"  * ";
        }*/

        int countt=0;
        for(int i=1;i<=index-n+1;i++){
            int sum=0;
            for(int j=i;j<i+n;j++){
                sum+=bb[j];
            }
            ///cout<<"sum:"<<sum<<endl;
            if(sum==n*(n+1)/2){
                countt++;
            }
        }
        cout<<"countt:"<<countt<<endl;
         cout<<endl;
    }
}


 

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/85546043