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 现在一群 wizard 在开会,进入会议室的时候,每个人将自己的 wand 放到了桌子上,现在开会结束了,
在 wizard 们将要离开的时候,将 wand 原来的摆放顺序打乱,那 wizard 将桌子上的 wang 拿走时: 
要求:至少有 K 个人拿到的是自己原来的 wand 。问,有多少种打乱顺序的方法
首先在 n 个中拿出 k 到 n 个,是放在原来的位置,方法有 C(n,i)种,然后将剩下的那些 wand 错排 预处理阶乘逆元
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
int n, k, t;
LL ans[maxn], a = , b = ;
void init() {
ans[] = a, ans[] = b;
for (int i = ; i <= ; i++) {
ans[i] = (((i - ) % mod) * ((a + b) % mod)) % mod;
a = b;
b = ans[i];
}
}
LL C(int k, int n) {
LL ret = ;
for (int i = n - k + ; i <= n; i++) ret *= i;
for (int i = ; i <= k; i++) ret /= i;
return ret;
}
LL c[]; long long modexp(long long a, long long b, int mod) {
long long res = ;
while(b > ) {
if (b & ) res = res * a % mod;
b = b >> ;
a = a * a % mod;
}
return res;
} void cal() {
LL ans = ;
for(int i = ; i <= 1e7; i ++) {
ans *= i;
ans %= mod;
c[i] = ans;
}
}
int main() {
cal();
c[] = ;
init();
sf(t);
while(t--) {
sff(n, k);
LL ret = ;
for (int i = k ; i <= n ; i++) {
LL fz = c[n];
LL fm = (c[i] * c[n - i]) % mod;
LL a1 = (fz * (modexp(fm, mod - , mod) % mod)) % mod;
ret = (ret + a1 * ans[n - i] % mod) % mod;
}
printf("%lld\n", ret % mod);
}
return ;
}

Wand FZU - 2282 全错位重排的更多相关文章

  1. [fzu 2282]置换不动点大于等于k的排列数

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2282 编号1~n的置换,不动点个数大于等于k的方案数. 参考百度百科错排公式,可以知道长度为n,每个数都不在自 ...

  2. HDU_2048——全错位排列递推公式

    Problem Description HDU 2006'10 ACM contest的颁奖晚会隆重开始了! 为了活跃气氛,组织者举行了一个别开生面.奖品丰厚的抽奖活动,这个活动的具体要求是这样的:首 ...

  3. HDU_2049——部分错位排列,概率论

    Problem Description 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样 ...

  4. NYOJ 451

    光棍节的快乐 描述 光棍们,今天是光棍节.聪明的NS想到了一个活动来丰富这个光棍节. 规则如下: 每个光棍在一个纸条上写一个自己心仪女生的名字,然后把这些纸条装进一个盒子里,这些光 棍依次抽取一张纸条 ...

  5. 神、上帝以及老天爷[HDU2048]

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

  6. CCF考前注意几点

    1.数组最好保持初始化习惯,且检查数组初始化在循环内还是循环外,若在循环内需要对数组进行处理,则数组初始化必须放在for循环内. 2.for循环保持好习惯,用括号括起来,以免粗心. 3.if条件判断要 ...

  7. NYOJ-451-光棍节的快乐

    光棍节的快乐 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描写叙述 光棍们,今天是光棍节.聪明的NS想到了一个活动来丰富这个光棍节. 规则例如以下: 每一个光棍在一个纸条上写 ...

  8. [BZOJ 4563]放棋子

    [BZOJ 4563]放棋子 题目 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在这个矩阵上放N枚棋子(障碍的位置不能放棋子),要求你放N个棋子 ...

  9. 12 October

    次小生成树 http://poj.org/problem?id=1679 不难得出,次小生成树可以由最小生成树更换一条边得到. 首先构造原图的最小生成树,然后枚举每一条不在最小生成树中的边 (u, v ...

随机推荐

  1. 【system.array】使用说明

    对象:system.array 说明:提供一系列针对数组类型的操作 目录: 方法 返回 说明 system.array.join( array, separator ) [String]  将数组转换 ...

  2. codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)

    You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...

  3. html5 canvas绘制环形进度条,环形渐变色仪表图

    html5 canvas绘制环形进度条,环形渐变色仪表图                                             在绘制圆环前,我们需要知道canvas arc() 方 ...

  4. Java学习个人备忘录之内部类

    内部类: 将一个类定义在另一个类的里面,对里面那个类就称为内部类. class Outer { private int num = 3; class Inner //它想访问Outer中的num, 如 ...

  5. c# dll问题

    问题描述: dll完全拷贝另一个程序,可是报缺少引用程序集之类的错误. 解决办法: 有可能是.net版本造成的错误.一般常见在3.5升到4之后,存在很多容差.

  6. c#非界面线程控制控件

    private delegate void FlushCilent(); Invoke(new FlushCilent(databaseConnect));

  7. k邻近算法理解及代码实现

    github:代码实现 本文算法均使用python3实现 1 KNN   KNN(k-nearest neighbor, k近邻法),故名思议,是根据最近的 $ k $ 个邻居来判断未知点属于哪个类别 ...

  8. 【转】how can i build fast

    http://blog.csdn.net/pcliuguangtao/article/details/5830860

  9. Android------去除标题栏

    这里暂时只给出一种方法,在java代码中去除 1.继承Activity 在onCreate方法中 getWindow().setFlags(WindowManager.LayoutParams.FLA ...

  10. KMP算法字符串查找子串

    题目: 经典的KMP算法 分析: 和KMP算法对应的是BF算法,其中BF算法时间复杂度,最坏情况下可以达到O(n*m),而KMP算法的时间复杂度是O(n + m),所以,KMP算法效率高很多. 但是K ...