Discription

Altanie is a very large and strange country in Mars. People of Mars ages a lot. Some of them live for thousands of centuries!

Your old friend Foki "The president of the Martian United States of Altanie" is the oldest man on Mars. He's very old no one knows how old he is! Foki loves children, so, he had (0 < K ≤ 106) children!

The government in Altanie decided to send a team of athletes to Venus. To participate in (0 < N ≤ 103) different game in the Galactic Olympics. So Foki told them to send his children instead!

Foki is in a big problem. How can he decide whom of his children is going to participate in which game, at the same time his children must participate in all the games and every one of his children get to participate in at least one game?

Note that in a certain arrangement, each one of Foki's children can participate in multiple games in the Olympics, but each game must be arranged to exactly one player.

Your job is to help Foki and answer his question: in how many way can he arrange his children to the games in Venus Olympics while satisfying the previous two conditions.

Input

The first line of the input contains T the number of the test cases. Each test is represented with two integers on a single line. ( 0 < N ≤ 103 ) the number of the games in the Olympics, ( 0 < K ≤ 106 ) the number of Foki's children.

Output

For each test case print one line contains the answer to Foki's question. Since the answer is very large. Print the answer modulo 109 + 7

Example

Input
1
3 2
Output
6

    首先每个人至少要参加一个项目,并且一个项目最多只能属于一个人,所以我们可以很容易的建出模型: 有n个不同的物品,我们要把它们分别放入k个有序的集合,并且要求每个集合至少要有一个物品,求方案总数。
我们知道,当集合无序的时候,方案数就是 S(n,k) ,即第二类斯特林数的第n行第k列的值; 集合有序的时候,再乘上 k! 就行了。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1000000007;
const int maxn=1005;
int T,n,m,S[maxn][maxn],jc[maxn];
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline void init(){
S[0][0]=jc[0]=1;
for(int i=1;i<=1000;i++){
jc[i]=jc[i-1]*(ll)i%ha;
for(int j=1;j<=i;j++) S[i][j]=add(S[i-1][j-1],S[i-1][j]*(ll)j%ha);
}
}
int main(){
freopen("galactic.in","r",stdin);
init(),scanf("%d",&T);
while(T--) scanf("%d%d",&n,&m),printf("%d\n",n<m?0:(int)(S[n][m]*(ll)jc[m]%ha));
return 0;
}

  

 

Codeforces Gym - 101147G The Galactic Olympics的更多相关文章

  1. 【第二类Stirling数】Gym - 101147G - The Galactic Olympics

    如果K>n,就无解: 如果K==n,就答案是P(n,n): 如果K<n,答案就是s(n,K)*P(K,K): P为排列数,s为第二类斯特林数. 第二类斯特林数就是将n个球,划分为K个非空集 ...

  2. Gym - 101147G G - The Galactic Olympics —— 组合数学 - 第二类斯特林数

    题目链接:http://codeforces.com/gym/101147/problem/G G. The Galactic Olympics time limit per test 2.0 s m ...

  3. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  4. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  5. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  6. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  7. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  8. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  9. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

随机推荐

  1. django知识分支_1

    django知识分支 1.Cookie工作流程: 浏览器向服务器发出请求,服务器接收到浏览器的请求进行处理,服务器设置一个cookie发送给浏览器,浏览器将cookie保存,当需要再次登录的时候,浏览 ...

  2. 牛客练习赛29 B

    炎热的早上,gal男神们被迫再操场上列队,gal男神们本来想排列成x∗x的正方形,可是因为操场太小了(也可能是gal男神太大了),校长安排gal男神们站成多个4∗4的正方形(gal男神们可以正好分成n ...

  3. Linux学习-什么是登录档

    CentOS 7 登录档简易说明 登录档的重要性 为什么说登录文件很重要, 解决系统方面的错误: 用 Linux 这么久了,你应该偶而会发现系统可能会出现一些错误,包括硬件捉不到或者是某些系 统服务无 ...

  4. Mantis安装与配置

    什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written in t ...

  5. GridView的RowCommand事件中获取每行控件的值

    //获取当前行                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent; //获取 ...

  6. Selenium WebDriver-通过断言页面是否存在某些关键字来确定页面按照预期加载

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...

  7. Windows杂技

    WINDOWS下 ,在某目录下按住shift加鼠标右键,可以直接有当前目录的dos窗口 win10不能用 解决办法是在当前文件夹的地址栏输入cmd然后回车 发现Powershell可以当作dos执行相 ...

  8. VirtualBox Host-only Adapter,Failed to create the host-only adapter 转

    不用重装VirtualBox,安装虚拟网卡 今天使用VirtualBox的host-only模式,因为之前把网络连接卸载,这次出现的各种错误. Failed to create the host-on ...

  9. 【Luogu】P3355骑士共存问题(最小割)

    题目链接 像题面那样把棋盘染成红黄点.发现骑士迈一步能到达的点的颜色一定是跟他所在的格子的颜色不同的.于是(woc哪来的于是?这个性质有这么明显吗?)从源点向所有红点连边,从所有黄点向汇点连边,红点向 ...

  10. 动态方式破解apk进阶篇(IDA调试so源码)

    动态方式破解apk进阶篇(IDA调试so源码) 来源 https://blog.csdn.net/qq_21051503/article/details/74907449 下面就说关于在IDA中And ...