题目链接:http://codeforces.com/gym/101147/problem/G

G. The Galactic Olympics
time limit per test

2.0 s

memory limit per test

64 MB

input

galactic.in

output

standard output

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个有区别的球放入m个有区别的盒子。先把盒子看成是无区别的,这样问题就转化为第二类斯特林数,共有S[n][m]种方案,然后再考虑盒子的区别,可知这是一个全排列,所以总的方案数为:m! * S[n][m] 。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define eps 0.0000001
#define LNF (1<<60)
#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+; LL dp[][]; void init()
{
memset(dp, , sizeof(dp));
for(int i = ; i<; i++)//先假设集合无区别
{
dp[i][] = ; dp[i][i] = ;
for(int j = ; j<i; j++)
dp[i][j] = (dp[i-][j]*j + dp[i-][j-])%mod;
} LL t = ;
for(int j = ; j<; j++)//因为集合有区别,所以要乘上阶乘
{
t = (t*j)%mod;
for(int i = j; i<; i++)
dp[i][j] = (dp[i][j]*t)%mod;
}
} int main()
{
#ifdef LOCAL
freopen("galactic.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL init();
int T, n, k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
if(n<k)//当n<k时,不要直接输出dp[n][k], 因为k<=1e6,会溢出。
{
puts("");
continue;
}
printf("%lld\n",dp[n][k]);
}
}

Gym - 101147G G - The Galactic Olympics —— 组合数学 - 第二类斯特林数的更多相关文章

  1. BZOJ 2159: Crash 的文明世界(组合数学+第二类斯特林数+树形dp)

    传送门 解题思路 比较有意思的一道数学题.首先\(n*k^2\)的做法比较好想,就是维护一个\(x^i\)这种东西,然后转移的时候用二项式定理拆开转移.然后有一个比较有意思的结论就是把求\(x^i\) ...

  2. 【cf961G】G. Partitions(组合意义+第二类斯特林数)

    传送门 题意: 给出\(n\)个元素,每个元素有价值\(w_i\).现在要对这\(n\)个元素进行划分,共划分为\(k\)组.每一组的价值为\(|S|\sum_{i=0}^{|S|}w_i\). 最后 ...

  3. Gym Gym 101147G 第二类斯特林数

    题目链接:http://codeforces.com/gym/101147/problem/G 题意:n个人,去参加k个游戏,k个游戏必须非空,有多少种放法? 分析: 第二类斯特林数,划分好k个集合后 ...

  4. Gym 101147G 第二类斯特林数

    大致题意: n个孩子,k场比赛,每个孩子至少参加一场比赛,且每场比赛只能由一个孩子参加.问有多少种分配方式. 分析: k>n,就无法分配了. k<=n.把n分成k堆的方案数乘以n的阶乘.N ...

  5. 【BZOJ5093】图的价值(第二类斯特林数,组合数学,NTT)

    [BZOJ5093]图的价值(第二类斯特林数,组合数学,NTT) 题面 BZOJ 题解 单独考虑每一个点的贡献: 因为不知道它连了几条边,所以枚举一下 \[\sum_{i=0}^{n-1}C_{n-1 ...

  6. 【BZOJ4555】求和(第二类斯特林数,组合数学,NTT)

    [BZOJ4555]求和(第二类斯特林数,组合数学,NTT) 题面 BZOJ 题解 推推柿子 \[\sum_{i=0}^n\sum_{j=0}^iS(i,j)·j!·2^j\] \[=\sum_{i= ...

  7. Codeforces 932 E Team Work ( 第二类斯特林数、下降阶乘幂、组合数学 )

    题目链接 题意 : 其实就是要求 分析 : 先暴力将次方通过第二类斯特林数转化成下降幂 ( 套路?) 然后再一步步化简.使得最外层和 N 有关的 ∑ 划掉 这里有个技巧就是 将组合数的表达式放到一边. ...

  8. 【51NOD 1847】奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数)

    [51NOD 1847]奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数) 题面 51NOD \[\sum_{i=1}^n\sum_{j=1}^nsgcd(i,j)^k\] 其中\( ...

  9. 【BZOJ2159】Crash的文明世界(第二类斯特林数,动态规划)

    [BZOJ2159]Crash的文明世界(第二类斯特林数,动态规划) 题面 BZOJ 洛谷 题解 看到\(k\)次方的式子就可以往二项式的展开上面考,但是显然这样子的复杂度会有一个\(O(k^2)\) ...

随机推荐

  1. poj 2104 K-th Number(主席树

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59058   Accepted: 20529 Case Time Limi ...

  2. JS没有contains方法,可以用indexof实现

    我们很多时候会不自觉的在js代码里对一个字符串进行如下操作: str.contains("substr"); 但是js里面没有这个方法去判断字符串str是不是包含substr,而j ...

  3. extjs常用技巧

    grid http://extjs.org.cn/node/590 监听 http://extjs.org.cn/node/593 总结 http://extjs.org.cn/node/641 常用 ...

  4. Java中获取ServletContext的方法

    Servlet: this.getServletContext() this.getServletConfig().getServletContext() request.getSession().g ...

  5. openfire Android学习(六)----总结

    Xmpp的一些方法整理到一个工具类中了 XmppConnection.java [java] view plaincopy [java] view plaincopy import java.io.B ...

  6. svn hooks 实现自动更新

    搞来搞去,原来是hooks 下面的脚本名称必须是post-commit才可以, 写成fly-commit一直不行.晕死~~~ https://serverfault.com/questions/144 ...

  7. SPFA 求带负权的单源最短路

    int spfa_bfs(int s) { ///s表示起点. queue <int> q; memset(d,0x3f,sizeof(d)); ///d数组中存下的就是最短路径(存在的话 ...

  8. electron 缓存目录 禁用缓存

    C:\Users\Administrator\AppData\Roaming\linksame // 禁用缓存 app.commandLine.appendSwitch("--disable ...

  9. react webapp 开发小结

    1.监听props的方法 componentWillReceiveProps(nextProps) { // } 2.监听state的方法 3.props 传递的方法 <AlarmList {. ...

  10. 服务管理-Nginx

    nginx优势 select,epoll模型 对于一次IO访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间.所以说.当一个read ...