BZOJ4517——[Sdoi2016]排列计数
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define for1(i, x, y) for(int i = (x); i <= (y); i ++)
#define for2(i, x, y) for(int i = (x); i >= (y); i --)
#define LL long long
#define inf 2147483647
#define MOD 1000000007
#define eps 1e-7
inline LL read(){
char ch = getchar(); LL x = 0, f = 1;
while(ch < '0' || ch > '9'){
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline LL exgcd(LL &x, LL &y, LL a, LL b){
if(b == 0){
x = 1; y = 0;
return a;
}
LL ret = exgcd(x, y, b, a % b);
LL t = x; x = y; y = t - a / b * x;
return ret;
}
inline LL inv(LL a, LL p){
LL x, y; LL d = exgcd(x, y, a, p);
if(d == 1) return (x + p) % p;
}
LL fac[1000010], ine[1000010], D[1000010];
int main(){
LL T = read();
//C(n,m)*D(n-m)
fac[0] = 1; for(LL i = 1; i <= 1000000; i ++) fac[i] = fac[i - 1] * (LL)i % MOD;
LL o = fac[1000000]; ine[1000000] = inv(fac[1000000], MOD);
ine[0] = 1; for(LL i = 999999; i >= 1; i --) ine[i] = ine[i + 1] * (i + 1) % MOD;
D[0] = 1;D[1] = 0; D[2] = 1;
for(LL i = 3; i <= 1000000; i ++) D[i] = (i - 1) * (D[i - 2] + D[i - 1]) % MOD;
while(T --){
LL n = read(), m = read();
LL ans = fac[n] * ine[n - m] % MOD * ine[m] % MOD;
ans = ans * D[n - m] % MOD;
printf("%lld\n", ans);
}
return 0;
}
BZOJ4517——[Sdoi2016]排列计数的更多相关文章
- BZOJ4517 Sdoi2016 排列计数 【DP+组合计数】*
BZOJ4517 Sdoi2016 排列计数 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 ...
- [BZOJ4517][SDOI2016]排列计数(错位排列)
4517: [Sdoi2016]排列计数 Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 1616 Solved: 985[Submit][Statu ...
- bzoj4517[Sdoi2016]排列计数(组合数,错排)
4517: [Sdoi2016]排列计数 Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 1792 Solved: 1111[Submit][Stat ...
- [BZOJ4517] [Sdoi2016] 排列计数 (数学)
Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...
- 2018.10.25 bzoj4517: [Sdoi2016]排列计数(组合数学)
传送门 组合数学简单题. Ans=(nm)∗1Ans=\binom {n} {m}*1Ans=(mn)∗1~(n−m)(n-m)(n−m)的错排数. 前面的直接线性筛逆元求. 后面的错排数递推式本蒟 ...
- BZOJ4517: [Sdoi2016]排列计数
Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...
- bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得
这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...
- bzoj千题计划282:bzoj4517: [Sdoi2016]排列计数
http://www.lydsy.com/JudgeOnline/problem.php?id=4517 组合数+错排公式 #include<cstdio> #include<ios ...
- BZOJ4517:[SDOI2016]排列计数(组合数学,错排公式)
Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...
随机推荐
- KEGG and Gene Ontology Mapping in Bioinformatic Method
使用KOBAS进行KEGG pathway和Gene Ontology分析 Article from Blog of Alfred-Feng http://blog.sina.com.cn/u/170 ...
- oneM2M启动Release 3标准化,华为引领物联网技术布局
http://developer.huawei.com/cn/ict/news/cn/2016/06/onem2m [韩国,首尔,2016年6月] 国际权威的物联网组织oneM2M召开第23次技术全会 ...
- EmgnCv进行轮廓寻找和计算物体凸包
http://blog.csdn.net/qq_22033759/article/details/48029493 一.轮廓寻找 用的是FindContours函数,在CvInvoke中 不过需要用到 ...
- wpf arcglobe +c# 三维缩放到图层
/// <summary> /// 地图缩放到图层 /// </summary> /// <param name="s ...
- php 数据访问(以mysql数据库为例)
//建一个连接,造一个连接对象$db = new MySQLi("localhost","root","123","mydb&qu ...
- jquery ajax rest invoke
notice: <script type="text/javascript"> $(document).ready(function() { $("#b03& ...
- dig的用法
Dig是linux中的域名解析工具,功能比nslookup强很多,使用也很方便,不用象nslookup总是set不停. Dig是domain information groper的缩写,知道了来源想必 ...
- Android学习笔记——button_activity
工程的功能是实现在一个acticity上点击按钮,切换到另外一个activity 以下代码为MainActivity.java中的代码 package com.example.button_activ ...
- tomcat的文件路径 servelet的配置 以及maven中的WEB-INF的路径
Tomcat JavaWeb应用的组成结构 开发JavaWeb应用时,不同类型的文件有严格的存放规则,否则不仅可能会使web应用无法访问,还会导致web服务器启动报错 WebRoot →Web应用所在 ...
- Python之路【第十二篇续】jQuery案例详解
jQuery 1.jQuery和JS和HTML的关系 首先了HTML是实际展示在用户面前的用户可以直接体验到的,JS是操作HTML的他能改变HTML实际展示给用户的效果! 首先了解JS是一门语言,他是 ...