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 个数是 ...
随机推荐
- javascript 小技巧
1:Boolean()==!! console.log(Boolean(888));//true console.log(!!(888));//true console.log(Boolean(&qu ...
- Mastering C# structs
http://www.developerfusion.com/article/84519/mastering-structs-in-c/
- ecshop后台,listtable.js使用
1.先载入listtable.js 2.html代码 a. <a href="javascript:listTable.sort('goods_number'); "> ...
- ecshop网站搬家缓存无法更新
问题描述: 1.后台产品列表能改,数据也能看到,前端就是不显示 2.缓存无法删除 3.网上其他方法都试过,还是不行 症状:是因为缓存无法删除,无法更新,只需要能重新更新缓存文件即可.网站搬家丢失tem ...
- yourphp超出20记录自动删除
$m = M('service_loginlog'); $res =$m->where('card_id='.$_SESSION['card_id'])->order('time desc ...
- 设计模式学习——观察者模式(Observer Pattern)
0. 前言 观察者模式在许多地方都能够用到,特别是作为MVC模式的一部分,在MVC中,模型(M):存放数据,视图(V):显示数据.当模型中的数据发生改变时,视图会得到通知,这是典型的观察者模式. 1. ...
- mysql优化--触发器和auto_increment
1.触发器: 触发器的好处:做数据回收站或者做数据关联删除 触发器的坏处:给数据库增加压力,增删改变慢,不利与mysql移到其他数据库会出问题. 触发器建立:只能增删改,查不能建立. 例子1:创建一个 ...
- PHP笔记
<?php//统计访问量 if(!@$fp=fopen("num.txt","r")){ echo "num.txt文件创建成功!<br& ...
- 数据库创建用户SQL
', default_database=TableName use TableName create user A for login A with default_schema=dbo exec s ...
- 简谈switch case
工作中从buff里截取了一个字符串,然后和配置文件中的字符串名字对比 ,如果一样,处理,不一样,elseif 再判断,再处理! switch(){case : case :...... }先说语法,再 ...