Description

求有多少种长度为 n 的序列 A,满足以下条件:
1 ~ n 这 n 个数在序列中各出现了一次
若第 i 个数 A[i] 的值为 i,则称 i 是稳定的。序列恰好有 m 个数是稳定的
满足条件的序列可能很多,序列数对 10^9+7 取模。

Input

第一行一个数 T,表示有 T 组数据。
接下来 T 行,每行两个整数 n、m。
T=500000,n≤1000000,m≤1000000
 

Output

输出 T 行,每行一个数,表示求出的序列数

 

Sample Input

5
1 0
1 1
5 2
100 50
10000 5000

Sample Output

0
1
20
578028887
60695423
/*
很容易就推出公式:ans=C(n,m)*dp[n-m]
dp[i]表示i的全错排方案数,dp[i]=(i-1)*(dp[i-1]+dp[i-2])
预处理出阶乘,阶乘的逆元和dp数组。
*/
#include<cstdio>
#include<iostream>
#define N 1000010
#define lon long long
#define mod 1000000007
#ifdef unix
#define LL "%lld"
#else
#define LL "%I64d"
#endif
using namespace std;
lon dp[N],inv[N],jc1[N],jc2[N],n,m;
void init(){
dp[]=;dp[]=;dp[]=;
for(int i=;i<N;i++)
dp[i]=(i-)*(dp[i-]+dp[i-])%mod;
inv[]=;
for(int i=;i<N;i++)
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
jc1[]=;
for(int i=;i<N;i++)
jc1[i]=jc1[i-]*i%mod;
jc2[]=;
for(int i=;i<N;i++)
jc2[i]=jc2[i-]*inv[i]%mod;
}
int main(){
init();
int T;scanf("%d",&T);
while(T--){
scanf(LL LL,&n,&m);
lon ans=jc1[n]*jc2[m]%mod*jc2[n-m]%mod*dp[n-m]%mod;
printf(LL,ans);printf("\n");
}
return ;
}

排列计数(bzoj 4517)的更多相关文章

  1. BZOJ 4517: [Sdoi2016]排列计数

    4517: [Sdoi2016]排列计数 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 911  Solved: 566[Submit][Status ...

  2. 数学(错排):BZOJ 4517: [Sdoi2016]排列计数

    4517: [Sdoi2016]排列计数 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 693  Solved: 434[Submit][Status ...

  3. BZOJ 4517: [Sdoi2016]排列计数 [容斥原理]

    4517: [Sdoi2016]排列计数 题意:多组询问,n的全排列中恰好m个不是错排的有多少个 容斥原理强行推♂倒她 $恰好m个不是错排 $ \[ =\ \ge m个不是错排 - \ge m+1个不 ...

  4. BZOJ 4517: [Sdoi2016]排列计数 错排公式

    4517: [Sdoi2016]排列计数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4517 Description 求有多少种长度为 ...

  5. BZOJ 4517: [Sdoi2016]排列计数 错排+逆元

    4517: [Sdoi2016]排列计数 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i, ...

  6. Bzoj 4517: [Sdoi2016]排列计数(排列组合)

    4517: [Sdoi2016]排列计数 Time Limit: 60 Sec Memory Limit: 128 MB Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ...

  7. bzoj-4517 4517: [Sdoi2016]排列计数(组合数学)

    题目链接: 4517: [Sdoi2016]排列计数 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 846  Solved: 530[Submit][ ...

  8. BZOJ 4517--[Sdoi2016]排列计数(乘法逆元)

    4517: [Sdoi2016]排列计数 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 1727  Solved: 1067 Description ...

  9. bzoj 2111: [ZJOI2010]Perm 排列计数 (dp+卢卡斯定理)

    bzoj 2111: [ZJOI2010]Perm 排列计数 1 ≤ N ≤ 10^6, P≤ 10^9 题意:求1~N的排列有多少种小根堆 1: #include<cstdio> 2: ...

  10. BZOJ 2111: [ZJOI2010]Perm 排列计数 [Lucas定理]

    2111: [ZJOI2010]Perm 排列计数 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1936  Solved: 477[Submit][ ...

随机推荐

  1. 《队长说得队》【Alpha】Scrum meeting 5

    项目 内容 这个作业属于哪个课程 >>2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 >>实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 ...

  2. Neural Style论文笔记+源码解析

    引言 前面在Ubuntu16.04+GTX1080配置TensorFlow并实现图像风格转换中介绍了TensorFlow的配置过程,以及运用TensorFlow实现图像风格转换,主要是使用了文章A N ...

  3. SpringMVC 项目中引用其他 Module 中的方法

    1. 将要引用的Module 引入项目中 2. 在主Module中添加依赖, 3. 被引用的类必须放在 Module 中/src/下的某个package中,否则引用不到(重要)

  4. 在VUE中,关于CKEditor使用

    官方文档 语言配置 代码如下 ClassicEditor .create( document.querySelector( '#editor' ), { language: 'de' // 配置语言 ...

  5. python3.7 文件操作

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 文件操作 # r 只读,默认打开方式,当文件不存在时会报错 # ...

  6. LeetCode(172)Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  7. 安装openstack同步数据库时出错解决方法

    错误提示:(2003, "Can't connect to MySQL server on 'controller' ([Errno -2] Name or service not know ...

  8. python 提交form-data之坑

    #coding=utf-8 import requests from requests_toolbelt import MultipartEncoder #requests库上传 files = {& ...

  9. cf963b Destruction of a Tree

    越靠近叶子越优先删掉 #include <iostream> #include <vector> #include <cstdio> using namespace ...

  10. luogu3388 【模板】割点(割顶)

    模板题 #include <iostream> #include <cstdio> using namespace std; struct Edge{ int too, nxt ...