N wizards are attending a meeting. Everyone has his own magic wand. N magic wands was put in a line, numbered from 1 to n(Wand_i owned by wizard_i). After the meeting, n wizards will take a wand one by one in the order of 1 to n. A boring wizard decided to reorder the wands. He is wondering how many ways to reorder the wands so that at least k wizards can get his own wand.

For example, n=3. Initially, the wands are w1 w2 w3. After reordering, the wands become w2 w1 w3. So, wizard 1 will take w2, wizard 2 will take w1, wizard 3 will take w3, only wizard 3 get his own wand.

Input

First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.

For each test case: Two number n and k.

1<=n <=10000.1<=k<=100. k<=n.

Output

For each test case, output the answer mod 1000000007(10^9 + 7).

Sample Input

2
1 1
3 1

Sample Output

1
4
题解:n个人排位,求至少k个人能做到自己位置的方法树。那么我们就要求出组合数Cnm,错排递推数组dp[i],
然后还有数据的问题,要反向考虑,从k到n转换为从0到k(左闭右开),那么还要计算出排列递推数组fac[i](即阶乘)位总方案数,一减即得答案。
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=;
const int mod=1e9+;
ll C[maxn][],dp[maxn],fac[maxn];
void Init()
{
fac[]=,fac[]=;
for(int i=;i<=;i++)//计算总方案数,即阶乘数
fac[i]=i*fac[i-]%mod;
dp[]=,dp[]=,dp[]=;
for(int i=;i<=;i++)//错排公式
{
dp[i]=((i-)*(dp[i-]+dp[i-])%mod)%mod;;
}
C[][]=;
C[][]=;
for(int i=;i<=;i++)//组合公式
{
C[i][]=;
for(int j=;j<=i&&j<=;j++)
{
C[i][j]=(C[i-][j-]+C[i-][j])%mod;
}
}
}
int main()
{
std::ios::sync_with_stdio();
Init();
int t,n,k;
cin>>t;
while(t--)
{
cin>>n>>k;
ll ans=;
for(int i=;i<k;i++)//由于正向考虑会T,并且k<100,那么我们反向考虑情况,用总情况减去即可
{ //反向情况即为:从n个数中选取i个,剩下n-i个错排,i范围从0到k
ans=(ans+(C[n][i]*dp[n-i])%mod)%mod;
}
cout<<(fac[n]-ans+mod)%mod<<endl;//注意输出也要取模
}
return ;
}

K - Wand(组合数+错排公式)的更多相关文章

  1. Codeforces 888D: Almost Identity Permutations(错排公式,组合数)

    A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...

  2. HDU——2068RPG的错排(错排公式)

    RPG的错排 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. HDU 2068 RPG错排 [错排公式]

    1.题意:1到N的序列的排列中,元素位置与元素值相对应的情况(值为i的元素在某个排列中正好排在第i个位置)大于等于序列规模一半的情况,有多少个? 2.输入输出:每组数据一个数,N,规定输入以0结尾: ...

  4. HDU 2048:神、上帝以及老天爷(错排公式,递推)

    神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. hdu 4535(排列组合之错排公式)

    吉哥系列故事——礼尚往来 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  6. HDU——1465不容易系列之一(错排公式)

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  7. HDU 1465(错排公式)

    不容易系列之一 题意: 一个人要寄n个信封,结果装错了.信纸的编号为1到n,信封的编号为1到n,信纸的编号不能和信封的编号一样,全都不能一样. 思路:错排公式. D(n)表示n件信封装错的所有的情况. ...

  8. HDU 2068 RPG的错排(错排公式 + 具体解释)

    RPG的错排 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. 【BZOJ】4517 [Sdoi2016]排列计数(数学+错排公式)

    题目 传送门:QWQ 分析 $ O(nlogn) $预处理出阶乘和阶乘的逆元,然后求组合数就成了$O(1)$了. 最后再套上错排公式:$ \huge d[i]=(i-1) \times (d[i-1] ...

随机推荐

  1. c++ 正则表达式查找

    C++ 正则表达式的使用 需求: 字符串含有除[0-9a-z]之外的字符,均返回失败! #include<regex> smatch result; string reg_str = &q ...

  2. DQL多表查询

    DQL多表查询 一.多表查询实现多个表之间查询数据 1.交叉连接笛卡尔积:A表中的每一行匹配B表中的每一行基本结构:select [数据库名1.]表名1,属性名1,......, [数据库名.]表名. ...

  3. JS/JQUERY函数库

    1. 判断字符串是否为空 function isEmptyString(str) { return str == undefined || str == "" || str == ...

  4. POJ 3083:Children of the Candy Corn

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11015   Acce ...

  5. php对象:__autoload()函数及单入口文件,__set(), __get(), get_class_methods(),get_class_vars()

    __autoload():当类中找不到相关类的时候,会自动执行__autoload()函数,可以自动加载相关文件 __set() : 当对类的私有变量进行调用赋值时,自动调用该方法.  __get() ...

  6. html+css新特性

    audio 音频 viedeo  视频 <audio src = ""02.pogg" controls = "controls"> lo ...

  7. 简单的Vue计算属性

    倒转字符串 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. es和数据库关系对比

    es类比传统关系型数据库:   Relational DB -> Databases -> Tables -> Rows -> Columns Elasticsearch -& ...

  9. [Python]distributed 1.21.8 requires msgpack, which is not installed.

    个人博客原文地址:http://www.bearoom.xyz/2019/08/24/python-devolop-en-msgpack-problem/ 在安装tensorflow的时候,出现了这个 ...

  10. 33. docker swarm 集群服务通信 之 RoutingMesh - Ingress 网络

    1.作用 当在 任何 一个 swarm 节点去访问 端口服务的时候 会通过 本节点 的 IPVS ( ip virtual service ) 到 真正的 swarm 节点上 当访问 docker h ...