FZU 2282 Wand

  • 2018-09-08
  • 13
  • 0

Description:

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个数1~n求i\in [k,n]中i个数字在自己位置上的所有情况数量。

错排公式为n个数都不在自己位置上的所有可能情况数量,递推求得错排数Staggered[i]

\sum_{i-k}^{n}C^{i}_{n}*Staggered[n-i]即为结果。

其中C^{i}_{n}代表i个数在自己位置上的数量,Staggered[n-i]代表其余数都不在自己位置上的数量。

其中求C^{i}_{n}要用到阶乘逆元,阶乘逆元递推中先通过飞马小定理利用快速幂求出一个最大值的阶乘逆元之后由大向小递推。

Staggered[0]要初始化为1,因为C^{i}_{n}*Staggered[0]为所有数都在自己位置上的数量,显然为1。

AC代码:

评论

还没有任何评论,你来说两句吧