Gym

题目链接: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] 。

代码如下:

 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 = 100000+10;
21 const int mod = 1e9+7;
22 
23 LL dp[1005][1005];
24 
25 void init()
26 {
27     memset(dp, 0, sizeof(dp));
28     for(int i = 1; i<1005; i++)//先假设集合无区别
29     {
30         dp[i][0] = 0; dp[i][i] = 1;
31         for(int j = 1; j<i; j++)
32             dp[i][j] = (dp[i-1][j]*j + dp[i-1][j-1])%mod;
33     }
34 
35     LL t = 1;
36     for(int j = 1; j<1005; j++)//因为集合有区别,所以要乘上阶乘
37     {
38         t = (t*j)%mod;
39         for(int i = j; i<1005; 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("0");
60             continue;
61         }
62         printf("%lld
",dp[n][k]);
63     }
64 }
View Code
原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538724.html