题目链接: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. 将压缩包文件(rar/zip)伪装成图片(jpg/gif/png/ico)

    1.在Windows上使用copy命令,缺点是只能是jpg文件,gif不支持,命令如下: copy in1.jpg+in2.rar out.jpg 2.网上说使用UEdit方式可以制作gif,但是测试 ...

  2. 使用UltraISO为U盘或内存卡制作系统安装工具

    此软件可以为U盘制作Windows安装版的启动工具,也可以为Linux制作启动工具,尤其是Ubuntu这些.提示:推荐购买一些读取速度快一些的U盘,运行起来可以节省很多时间. 如果是内存卡要实现此功能 ...

  3. django忘记超级用户密码的解决方法

    用Django shell: 1 python manage.py shell 然后获取你的用户名,并且重设密码: 1 2 3 4 from django.contrib.auth.models im ...

  4. Mina、Netty、Twisted一起学(七):公布/订阅(Publish/Subscribe)

    消息传递有非常多种方式.请求/响应(Request/Reply)是最经常使用的.在前面的博文的样例中.非常多都是採用请求/响应的方式.当server接收到消息后,会马上write回写一条消息到clie ...

  5. Spring MVC传值乱码解决

    在web.xml中进行配置,加入以下代码: <!-- 乱码解决 --> <filter> <filter-name>characterEncodingFilter& ...

  6. JavaScript 工厂模式和订阅模式

    设计模式的好处: 代码规范 // 例如表单验证,两个 input ,一个用户名,一个密码 // 通常做法是 function checkUser(){ //..... } function check ...

  7. Vb.net/VB 声明API函数实现父窗口功能

    回想第一次敲机房收费.自己调用了api函数实现了父窗口及其子窗口最小化的功能.现在再次遇到,自己就在思考,能不能继续使用API函数呢?答案当然是Of Course! 事实上细致看两者并没有多大的差别. ...

  8. 在命令行下运行Matlab

    2014-04-20 22:08:11 在命令行下执行: matlab -help 可以得到帮助文件: Usage: matlab [-h|-help] | [-n | -e] [-arch | v= ...

  9. vue 路由组件不重新加载

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Spring Boot + Spring Data JPA + PostgreSQL

    最近在用Java重写之前实习生用.netcore写的微信后台应用. 规定用Spring Boot框架,PostgreSQL数据库.之前一直习惯于基于XML的Spring app,也没用过Postgre ...