题解:CF1091D New Year and the Permutation Concatenation
WoodReal12 · · 题解
前言
感觉我这题思路挺清晰的(也有可能只是我感觉),写个题解记录下。
思路
::::info[hint1]
题目中对序列的要求告诉你什么?
::::
::::success[answer1]
发现题目说要求的序列长度为
于是问题转化为这个长度为
::::
::::info[hint2]
考虑这些子串的组成是什么,分别计算。
::::
::::success[answer2]
是由两部分组成的,一部分是原来的排列,共
::::
::::info[hint3]
考虑什么时候第二种子串会出现。
::::
::::success[answer3]
因为是由第
::::
::::info[hint4]
考虑按照字典序排列这一性质。
::::
::::success[answer4]
设第
::::
::::info[hint5]
考虑现在有多少组,就可以解决这个问题了。
::::
::::success[answer5]
即第
因此答案为
:::info[为什么
当
当
| ::: |
|---|
代码
// Problem: CF1091D New Year and the Permutation Concatenation
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1091D
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// Date: 2026-09-28 15:11:11 (UTC+8)
//
// by WoodReal12
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
// #include <atcoder/all>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define pc putchar
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
using namespace std;
// using namespace atcoder;
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T> inline bool read(T &x){
x=0;int f=1;char c=gc();
for(;c!=EOF&&!isdigit(c);c=gc())if(c=='-')f=-1;
if(c==EOF)return 0;
for(;c!=EOF&&c>='0'&&c<='9';c=gc())x=(x<<1)+(x<<3)+(c^48);
x*=f;return 1;
}
template <typename T,typename ...Targs>
inline bool read(T &x,Targs& ...args){return read(x)&&read(args...);}
template <typename T> void print(T x){
if(x<0)pc('-'),x=-x;
if(x>=10)print(x/10);
pc(x%10+'0');
}
const int N=1e6+5,mod=998244353;
int qmi(int a,int b){
int res=1;
while(b){
if(b&1){
res=(res*a)%mod;
}
a=(a*a)%mod;
b>>=1;
}
return res;
}
int fac[N],inv[N];
int C(int a,int b){
if(a<b)
return 0;
if(b==0||a==b)
return 1;
return fac[a]*inv[b]%mod*inv[a-b]%mod;
}
void init(){
fac[0]=1;
for(int i=1;i<N;i++)
fac[i]=(fac[i-1]*i)%mod;
inv[N-1]=qmi(fac[N-1],mod-2);
for(int i=N-2;i>=0;i--)
inv[i]=(inv[i+1]*(i+1))%mod;
}
int n;
signed main(){
init();
read(n);
int ans=fac[n];
for(int l=2;l<n;l++){
ans=(ans+((fac[l]-1)*C(n,l)%mod*fac[n-l])%mod)%mod;
}
cout<<ans<<endl;
return 0;
}