[hdu6432]Problem G. Cyclic
题目大意:给你$n$,一种合法的排列为,排列中没有$s[i\%n+1]-s[i]==1$,求合法方案数
题解:容斥,令$f_{i,j}$表示有$i$个元素,至少包含$j$个$s[i\%n+1]-s[i]==1$的方案数,发现$f_{n,1}=\binom n 1(n-2)!$个
推广$f_{n,k}=\binom n k(n-k-1)!$(令$(-1)!==1$)
$\therefore ans = (-1)^n + \sum_{k = 0}^{n - 1} (-1)^k \binom{n}{k} (n - k - 1)!$
卡点:无
C++ Code:
#include <cstdio>
#define maxn 100010
const long long mod = 998244353;
int Tim, n;
long long FAC[maxn + 1], inv[maxn], *fac = &FAC[1], ans;
long long C(long long a, long long b) {
if (a < b) return 0;
return fac[a] * inv[b] % mod * inv[a - b] % mod;
}
int main() {
scanf("%d", &Tim);
fac[-1] = fac[0] = fac[1] = inv[0] = inv[1] = 1;
for (int i = 2; i <= 100000; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = inv[mod % i] * (mod - mod / i) % mod;
}
for (int i = 2; i <= 100000; i++) inv[i] = inv[i] * inv[i - 1] % mod;
while (Tim --> 0) {
scanf("%d", &n);
ans = 0;
for (int i = 0; i <= n; i++) {
ans = (ans + ((i & 1) ? -1ll : 1ll) * C(n, i) * fac[n - i - 1] % mod) % mod;
}
if (ans < 0) ans += mod;
printf("%lld\n", ans);
}
return 0;
}
[hdu6432]Problem G. Cyclic的更多相关文章
- 实验9:Problem G: 克隆人来了!
想要输出""的话: cout<<"A person whose name is \""<<name<<" ...
- 实验12:Problem G: 强悍的矩阵运算来了
这个题目主要是乘法运算符的重载,卡了我好久,矩阵的乘法用3个嵌套的for循环进行,要分清楚矩阵的乘法结果是第一个矩阵的行,第二个矩阵的列所组成的矩阵. 重载+,*运算符时,可以在参数列表中传两个矩阵引 ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem G: Check The Check(模拟国际象棋)
Problem G: Check The Check Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 10 Solved: 3[Submit][Statu ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem G
Problem G Good Teacher I want to be a good teacher, so at least I need to remember all the student n ...
- 【贪心+中位数】【新生赛3 1007题】 Problem G (K)
Problem G Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- Problem G: If We Were a Child Again
Problem G: If We Were a Child AgainTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 14[Submi ...
- Problem G: Keywords Search
Problem G: Keywords SearchTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 10 Solved: 6[Submit][Status] ...
- BZOJ4977 八月月赛 Problem G 跳伞求生 set 贪心
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4977 - 八月月赛 Problem G 题意 小明组建了一支由n名玩家组成的战队,编号依次为1到n ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem G. k-palindrome dp
Problem G. k-palindrome 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7022 ...
随机推荐
- JavaScript 十行原生代码实现复制内容到剪贴板
十行原生代码,不引入任何 JS 库,目前大部分浏览器与移动平台都可兼容. function copyToClipboard(value, callback) { var textarea = docu ...
- 【例题收藏】◇例题·IV◇ Wooden Sticks
◇例题·IV◇ Wooden Sticks 借鉴了一下 Candy? 大佬的思路 +传送门+ (=^-ω-^=) 来源:+POJ 1065+ ◆ 题目大意 有n个木棍以及一台处理木棍的机器.第i个木棍 ...
- 利用sysbench进行MySQL OLTP基准测试
Preface In order to know clearly about the real performance threshold of database server,we ...
- kali安装ssh服务
一. kali安装ssh服务 1.修改源 root@DGG:~# vi /etc/apt/sources.list deb http://http.kali.org/kali kali-rolling ...
- 在程序开发中,++i 与 i++的区别
在不参与运算的情况下,i++和++i都是在变量的基础加1 ◆在参与运算的情况下 Var i=123; Var j=i++; 先将i的值123赋值给j,之后再自增 j的值为123 i 的值为124 ...
- iOS-重构微博cell模型
一.Frame模型: -------------------WeiboFrame.h-------------------------------------------------- ------- ...
- linux socketpair
相对于无名管道来说,socketpair也是使用在亲缘进程之间,不过它提供了能够全双工通信的通道 man socketpair: #include <sys/types.h> /* See ...
- [Luogu1341]无序字母对(欧拉回路)
按题意给定字符串建无向图,找欧拉回路 按照定义,当没有奇数度点或者只有2个奇数度点时才有欧拉回路 Code #include <cstdio> #include <algorithm ...
- 笔记-redis安装
笔记-redis安装配置 1. redis安装配置 1.1. windows环境安装 win8已有redis 查看版本:redis-server –version 想更新到5.0.0, ...
- 使wlr写cnblog的博客-2 设置cnblog帐号
ref:http://www.cnblogs.com/liuxianan/archive/2013/04/13/3018732.html 使用: 打开Windows Live Writer,第一次 ...