[fzu 2282]置换不动点大于等于k的排列数
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2282
编号1~n的置换,不动点个数大于等于k的方案数。
参考百度百科错排公式,可以知道长度为n,每个数都不在自己位置的方案数。然后枚举长度即可。
考虑对立面(即小于k个在自己位置的)可以优化空间。
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn=;
const int md=;
int D[maxn];
int C[maxn][];
int F[maxn]; int main()
{
D[]=;
D[]=;
D[]=;
for (int i=;i<=;i++)
{
D[i]=1ll*(i-)*(0ll+D[i-]+D[i-])%md;
}
C[][]=;
for (int i=;i<=;i++)
{
C[i][]=;
for (int j=;j<=min(,i);j++)
{
C[i][j]=(C[i-][j-]+C[i-][j])%md;
}
}
F[]=;
for (int i=;i<=;i++)
{
F[i]=1ll*F[i-]*i%md;
}
int t;
scanf("%d",&t);
while (t--)
{
int n,k;
scanf("%d%d",&n,&k);
int ans=F[n];
for (int i=;i<k;i++)
{
ans=(ans-1ll*C[n][i]*D[n-i]%md+md)%md;
}
printf("%d\n",ans);
}
return ;
}
[fzu 2282]置换不动点大于等于k的排列数的更多相关文章
- LeetCode 笔记21 生成第k个排列
题目是这样的: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...
- LinkCode 第k个排列
http://www.lintcode.com/zh-cn/problem/permutation-sequence/# 原题 给定 n 和 k,求123..n组成的排列中的第 k 个排列. 注意事项 ...
- [Swift]LeetCode60. 第k个排列 | Permutation Sequence
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- LeetCode 60 第K个排列
题目: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "13 ...
- LeetCode(60): 第k个排列
Medium! 题目描述: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" ...
- 蓝桥杯 问题 1110: 2^k进制数 (排列组合+高精度巧妙处理)
题目链接 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2 ...
- LeetCode 中级 - 第k个排列(60)
可以用数学的方法来解, 因为数字都是从1开始的连续自然数, 排列出现的次序可以推 算出来, 对于n=4, k=15 找到k=15排列的过程: 1 + 对2,3,4的全排列 (3!个) 2 + 对1,3 ...
- 代码题(45)— 下一个排列、第k个排列
1.31. 下一个排列 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只 ...
随机推荐
- Python学习之property
Python中使用Property函数可以将类中的函数当作属性来调用. 案例 __metaclass__=type class Rectangle: def __init__(self): self. ...
- C语言:类型、运算符、表达式
看了一天书,有点累了.就写写随笔记录一下今天的复习成果吧. C语言的基本数据类型 数值型:整型数,浮点数,布尔数,复数和虚数. 非数值型:字符. 整数最基本的是int,由此引出许多变式诸如有符号整数s ...
- linux文件操作篇 (二) 打开和关闭文件
2.1 打开文件和关闭文件 #include <sys/types.h>#include <sys/stat.h>#include <fcntl.h> 头文件 i ...
- spring boot踩坑记
Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWri ...
- Service ANR问题
错误堆栈: ActivityManager: ANR in com.oppo.reader PID: 8071 Reason: executing service com.oppo.reade//co ...
- VHDL入门学习-程序组成
1. VHDL程序的组成 一个完整的VHDL程序是以下五部分组成的: 2. 库(LIBRARY):比较好理解,调用系统已有的库,WORK库就是用户当前编辑文件所在的文件夹, IEEE库:由IEEE(美 ...
- linux里面的fork函数创建一个新进程
由fork创建的新进程成为子进程,fork函数被调用一次,但是返回两次,
- 台湾ML笔记--1.2 formalize the learning probelm
Basic notations input: x∈χ (customer application) output: y∈y (good/bad after approving cred ...
- 步骤2:JMeter 分布式测试(性能测试大并发、远程启动解决方案)
转载(记录) http://www.cnblogs.com/fengpingfan/p/5583954.html http://www.cnblogs.com/puresoul/p/4844539.h ...
- nmon Analyser分析仪
nmon Analyser官网: https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Power+System ...