Good Bye 2018 D. New Year and the Permutation Concatenation(组合数学or打表找规律)(*)

题目链接
在这里插入图片描述
思路:这道题我是打表找规律做出来的,我估计很多人都是这样做的,但还是来学习一下正解。
首先很容易想到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}

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

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

在这里插入图片描述 i是前缀的长度,sum再-1即是这个前缀拼凑可以得到新串数目。

其中长度为i的前缀的个数在这里插入图片描述

枚举i即可得到答案在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+1; 
const ll mod=998244353;
ll n,f[maxn],ans[maxn];
int main()
{
	f[0]=1;
	for(int i=1;i<maxn;++i) f[i]=f[i-1]*i%mod;
	ans[1]=1;ans[2]=2;
	scanf("%lld",&n);
	for(int i=3;i<=n;++i)
	ans[i]=((ans[i-1]-1)*i%mod+f[i]%mod)%mod;
	printf("%lld\n",ans[n]);
}
发布了391 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42479630/article/details/105458987