time limit per test2 seconds memory limit per test256 megabytes
inputstandard input outputstandard 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 inputCopy 3 outputCopy 9 inputCopy 4 outputCopy 56 inputCopy
10 outputCopy 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.
答案显然与N的全排列有关。
对于连续的序列,我们可以根据第一个数字把他们分成n个部分考虑
画图寻找规律,每次多出来的部分的结构与上一个n的答案结构很类似
于是打了个表
发现每部分额外多出的连续序列的数量是n=N-1时的答案-1
//cyc
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define vector<int> VI
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mst(a) memset(a,0,sizeof a)
#define int long long
using namespace std;
typedef pair<int,int> pii;
const int mod=998244353;
const int maxn=1e6+5;
int fac[maxn];
int ans[maxn];
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
fac[1]=1;
for(int i=2;i<=maxn;i++){
fac[i]=fac[i-1]*i%mod;
}
int n;
cin>>n;
ans[1]=1;
ans[2]=2;
ans[3]=9;
for(int i=4;i<=n;i++){
ans[i]=(i*(ans[i-1]-1)+(fac[i]))%mod;
}
cout<<ans[n]<<endl;
}