1091D. New Year and the Permutation Concatenation

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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值