题目链接: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. 1
    3 2
output
  1. 6

题解:

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

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <sstream>
  11. #include <algorithm>
  12. using namespace std;
  13. #define pb push_back
  14. #define mp make_pair
  15. #define eps 0.0000001
  16. #define LNF (1<<60)
  17. #define LOCAL
  18. typedef long long LL;
  19. const int inf = 0x3f3f3f3f;
  20. const int maxn = +;
  21. const int mod = 1e9+;
  22.  
  23. LL dp[][];
  24.  
  25. void init()
  26. {
  27. memset(dp, , sizeof(dp));
  28. for(int i = ; i<; i++)//先假设集合无区别
  29. {
  30. dp[i][] = ; dp[i][i] = ;
  31. for(int j = ; j<i; j++)
  32. dp[i][j] = (dp[i-][j]*j + dp[i-][j-])%mod;
  33. }
  34.  
  35. LL t = ;
  36. for(int j = ; j<; j++)//因为集合有区别,所以要乘上阶乘
  37. {
  38. t = (t*j)%mod;
  39. for(int i = j; i<; i++)
  40. dp[i][j] = (dp[i][j]*t)%mod;
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. #ifdef LOCAL
  47. freopen("galactic.in", "r", stdin);
  48. // freopen("output.txt", "w", stdout);
  49. #endif // LOCAL
  50.  
  51. init();
  52. int T, n, k;
  53. scanf("%d",&T);
  54. while(T--)
  55. {
  56. scanf("%d%d",&n,&k);
  57. if(n<k)//当n<k时,不要直接输出dp[n][k], 因为k<=1e6,会溢出。
  58. {
  59. puts("");
  60. continue;
  61. }
  62. printf("%lld\n",dp[n][k]);
  63. }
  64. }

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. 深入理解Thread构造函数

    上一篇快速认识线程 本文参考汪文君著:Java高并发编程详解. 1.线程的命名 在构造现成的时候可以为线程起一个名字.但是我们如果不给线程起名字,那线程会有一个怎样的命名呢? 这里我们看一下Threa ...

  2. 几点iOS开发技巧

    转自I'm Allen的博客   原文:iOS Programming Architecture and Design Guidelines   原文来自破船的分享   原文作者是开发界中知晓度相当高 ...

  3. live555client连多路1080P视频流花屏问题

    硬件和软件环境是这种: DM8168 + linux. 解码器是DM8168自带的 视频来源: ipc通过live555做的的rtsp sever发送过来的 其它測试: 通过VLC在pc连4路1080 ...

  4. byte 单位换算

    1G就1GB啦,平时人们说1G只是简洁来说而已. bit(位).B(字节).K(千).M(兆).G(吉咖).T(太拉) B(Byte).KB(KiloByte).MB(MegaByte).GB(Gig ...

  5. HDOJ1160 Fat Mouse&#39;s Speed

    FatMouse's Speed pid=1160">http://acm.hdu.edu.cn/showproblem.php?pid=1160 最长递增子序列问题的一个变体.实际上 ...

  6. flex 节点删除

    <mx:Script>        <![CDATA[            protected function btn1_clickHandler(evt:MouseEvent ...

  7. android页面间传递对象

    android传递对象有两种方式: 一种是Serializable和Parcelable 对于第一种方式: import java.io.Serializable; public class Shop ...

  8. python(21)- python内置函数练习

    题目一:用map来处理字符串列表啊,把列表中所有人都变成sb,比方alex_sbname=['alex','wupeiqi','yuanhao'] name=['alex','wupeiqi','yu ...

  9. java 是 传值还是传址 Pass-by-value or Pass-by-reference

    原文在此,写的非常好,解答了我的疑问 http://www.javadude.com/articles/passbyvalue.htm 首先放上一段代码,我是在找寻这段代码的内部原理的时候,在stac ...

  10. Arduino关于旋转编码器程序的介绍(Reading Rotary Encoders)--by Markdown

    介绍 旋转或编码器是一个角度測量装置. 他用作精确測量电机的旋转角度或者用来控制控制轮子(能够无限旋转,而电位器只能旋转到特定位置).其中有一些还安装了一个能够在轴上按的button,就像音乐播放器的 ...