Codeforces Good Bye 2018 D (1091D) New Year and the Permutation Concatenation
题意:给n!个n的排列,按字典序从小到大连成一条序列,例如3的情况为:[1,2,3, 1,3,2, 2,1,3 ,2,3,1 ,3,1,2 ,3,2,1],问其中长度为n,且和为sum=n*(n+1)/2的序列有多少个?
思路(官方题解):我们考虑一下next_perumation函数产生字典序递增的全排列的过程:
假设某一个序列长度为n,最长的递减的后缀长度k,那么它的下一个排列是这样产生的:选取序列第n-k个数,与后k个数中比第n - k个数大的最小的数交换,然后将后k个数按从小到大排序。
例如序列1,2,5,4,3的下一个排列为1,3,2,4,5。我们观察发现:这种时候1,2,(5,4,3,1,3,)2,4,5不满足和为sum了,因为在产生下一个排列的过程中,第n-k个位置的数被替换了。
也就是说,假设一个序列存在长度为k的递减后缀,那么这个后缀不能产生一个长度为sum的序列。例如,1,2,(5,4,3,1,3,)2,4,5不行,但是1,(2,5,4,3,1,)3,2,4,5可以。
所以,我们的任务是找出每个长度为k的递减后缀有多少个?应该为C(n,n-k)*(n-k)!=A(n,n-k)=n!/k!个。因为只要选了前面n-k个数,后面长度为k的递减的序列是固定的,所以我们只需要选n-k个数全排列就行了。
我们可以得到最终的答案了:一共有n*n!-(n-1)个序列,要减去( ∑(k from 1 to n-1) n!/k! )- (n-1)个。
为什么要减去n-1个呢?我们来看最后一个排列(假设n为5)5,4,3,2,1 。5之后的序列不存在,所以要从总的序列数中减去。而这(n-1)个不存在的序列恰好会被判定为不满足题意,也应该减去。
所以总的来说,答案应该是:(所有的序列-不存在的序列)-(不满足的序列-不存在的序列)。我们可以把答案写的更优雅一点:ans=n*n!-∑(k from 1 to n-1) n!/k!。
代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<vector>
#include<stack>
#define INF 0x3f3f3f3f
#define pii pair<int,int>
#define LL long long
#define fi first
#define se second
#define ls(x) (x<<1)
#define rs(x) ((x<<1)+1)
#define lowbit(x) (x&(-x))
using namespace std;
const int maxn=1000010;
const LL mod=998244353;
LL s[maxn],f[maxn];//s[k]是n!/k!
int main(){
LL n;
scanf("%lld",&n);
f[0]=1,s[n]=1;
for(LL i=1;i<=n;i++){
f[i]=(f[i-1]*i)%mod;
}
for(LL i=n-1;i>=1;i--){
s[i]=(s[i+1]*(i+1))%mod;
}
LL ans=(n*(f[n]))%mod;
for(LL i=1;i<=n-1;i++){
ans=(ans-s[i]+mod)%mod;
}
cout<<ans<<endl;
}
Codeforces Good Bye 2018 D (1091D) New Year and the Permutation Concatenation的更多相关文章
- Codeforces 1091D New Year and the Permutation Concatenation 找规律,数学 B
Codeforces 1091D New Year and the Permutation Concatenation https://codeforces.com/contest/1091/prob ...
- Codeforces:Good Bye 2018(题解)
Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...
- Codeforces Good Bye 2018
咕bye 2018,因为我这场又咕咕咕了 无谓地感慨一句:时间过得真快啊(有毒 A.New Year and the Christmas Ornament 分类讨论后等差数列求和 又在凑字数了 #in ...
- Good Bye 2018 (A~F, H)
目录 Codeforces 1091 A.New Year and the Christmas Ornament B.New Year and the Treasure Geolocation C.N ...
- Good Bye 2018
Good Bye 2018 2018年最后一场CF,OVER! 弱弱的我只能做出3道A,B,D~~~~ 最后几分钟,感觉找到了C题的规律,结束的那一刻,提交了一发 "Wrong answer ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Good Bye 2018题解
Good Bye 2018题解 题解 CF1091A [New Year and the Christmas Ornament] 打完cf都忘记写题解了qwq 题意就是:给你一些黄,蓝,红的球,满足蓝 ...
- Codeforces 1091 Good Bye 2018
占个坑先,希望不要掉的太惨了吧,不要掉到上一次之前的rating upt:flag竟然没到,开心. A - New Year and the Christmas Ornament 好像没什么可说的. ...
- codeforces Good Bye 2015 B. New Year and Old Property
题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...
随机推荐
- 《Advanced Bash-scripting Guide》学习(二):测试脚本调用的参数是否正确
本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 #!/bin/bash E_WRONG_ARGS=85 script_param ...
- poj-1426-Find The Multiple(打表水过)
思路: 2的最近可以整除的数是10 所以,很关键的一点,只要是偶数,例如: 6:2*3,可以拆分为能够被2整除和能够被3整除的乘积,所以,10*111=1110 144:72*2,8*9*2,2*2* ...
- L107
It is advisable to take an open- minded approach to new idea. 对新思想采取不存先入之见的态度是明智的.That said, the com ...
- BZOJ - 2618 凸多边形 (半平面交)
题意:求n个凸多边形的交面积. 半平面交模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; ty ...
- NET持续集成与自动化部署
https://www.cnblogs.com/hunternet/p/9590287.html 相信每一位程序员都经历过深夜加班上线的痛苦!而作为一个加班上线如家常便饭的码农,更是深感其痛.由于我们 ...
- LeetCode 315. Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)
http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...
- BZOJ4066:简单题
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- Java基础--序列化Serializable
对Java对象序列化的目的是持久化对象或者为RMI(远程方法调用)传递参数和返回值. 下面是一个序列化对象写入文件的例子: ---------------------------- package u ...
- live555源码分析----RSTPServer创建过程分析
最近五一回家,终于有机会能安静的看一下流媒体这方面相关的知识,准备分析live555的源码,接下来会把我读源码的过程记录成博客,以供其他的同路人参考. 因为再读源码的过程中,并不是一路顺着读下来,往往 ...