FZU2282-Wand

Wand

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个人分配到属于自己的编号的方案数。
解题思路:组合数学+全错位

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAXN=1e4+5;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
LL f[MAXN],C[MAXN][102],A[MAXN];

void init()
{
  f[0]=1;f[1]=0;f[2]=1;
  for(int i=3;i<=10001;i++)
  {
    f[i]=((i-1)*(f[i-1]+f[i-2])%MOD)%MOD;
  }
  C[1][0]=1;C[1][1]=1;
  for(int i=2;i<=10000;i++)
  {
    C[i][0]=1;
    for(int j=1;j<=i&&j<=100;j++)
    {
      C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
    }
  }
  A[0]=1;A[1]=1;
  for(int i=2;i<=10001;i++)
  A[i]=i*A[i-1]%MOD;
}

int main()
{
  int T;
  scanf("%d",&T);
  init();
  while(T--)
  {
    int n,k;
    scanf("%d%d",&n,&k);
    LL ans=0;
    for(int i=0;i<k;i++)
    {
      ans=(ans+(C[n][i]*f[n-i])%MOD)%MOD;
    }
    ans=(A[n]-ans+MOD)%MOD;
    printf("%lld\n",ans);
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值