Gym - 101147G G - The Galactic Olympics —— 组合数学 - 第二类斯特林数
题目链接:http://codeforces.com/gym/101147/problem/G
2.0 s
64 MB
galactic.in
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.
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.
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
1
3 2
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 —— 组合数学 - 第二类斯特林数的更多相关文章
- BZOJ 2159: Crash 的文明世界(组合数学+第二类斯特林数+树形dp)
传送门 解题思路 比较有意思的一道数学题.首先\(n*k^2\)的做法比较好想,就是维护一个\(x^i\)这种东西,然后转移的时候用二项式定理拆开转移.然后有一个比较有意思的结论就是把求\(x^i\) ...
- 【cf961G】G. Partitions(组合意义+第二类斯特林数)
传送门 题意: 给出\(n\)个元素,每个元素有价值\(w_i\).现在要对这\(n\)个元素进行划分,共划分为\(k\)组.每一组的价值为\(|S|\sum_{i=0}^{|S|}w_i\). 最后 ...
- Gym Gym 101147G 第二类斯特林数
题目链接:http://codeforces.com/gym/101147/problem/G 题意:n个人,去参加k个游戏,k个游戏必须非空,有多少种放法? 分析: 第二类斯特林数,划分好k个集合后 ...
- Gym 101147G 第二类斯特林数
大致题意: n个孩子,k场比赛,每个孩子至少参加一场比赛,且每场比赛只能由一个孩子参加.问有多少种分配方式. 分析: k>n,就无法分配了. k<=n.把n分成k堆的方案数乘以n的阶乘.N ...
- 【BZOJ5093】图的价值(第二类斯特林数,组合数学,NTT)
[BZOJ5093]图的价值(第二类斯特林数,组合数学,NTT) 题面 BZOJ 题解 单独考虑每一个点的贡献: 因为不知道它连了几条边,所以枚举一下 \[\sum_{i=0}^{n-1}C_{n-1 ...
- 【BZOJ4555】求和(第二类斯特林数,组合数学,NTT)
[BZOJ4555]求和(第二类斯特林数,组合数学,NTT) 题面 BZOJ 题解 推推柿子 \[\sum_{i=0}^n\sum_{j=0}^iS(i,j)·j!·2^j\] \[=\sum_{i= ...
- Codeforces 932 E Team Work ( 第二类斯特林数、下降阶乘幂、组合数学 )
题目链接 题意 : 其实就是要求 分析 : 先暴力将次方通过第二类斯特林数转化成下降幂 ( 套路?) 然后再一步步化简.使得最外层和 N 有关的 ∑ 划掉 这里有个技巧就是 将组合数的表达式放到一边. ...
- 【51NOD 1847】奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数)
[51NOD 1847]奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数) 题面 51NOD \[\sum_{i=1}^n\sum_{j=1}^nsgcd(i,j)^k\] 其中\( ...
- 【BZOJ2159】Crash的文明世界(第二类斯特林数,动态规划)
[BZOJ2159]Crash的文明世界(第二类斯特林数,动态规划) 题面 BZOJ 洛谷 题解 看到\(k\)次方的式子就可以往二项式的展开上面考,但是显然这样子的复杂度会有一个\(O(k^2)\) ...
随机推荐
- OpenLayers3 动画
参考文章 openlayers3中三种动画实现
- IOS开发之----两种保存用户名和密码实现记住密码库
使用Keychain存储用户敏感信息 iOS的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书等)的方式,每个ios程序都有一个独立的keychain存储.相对于 NSUserDe ...
- gitignore 规则
在使用git的过程中,总有一些我们不想被跟踪的文件,例如vim的交换文件,编译产生的文件等等.这时,我们可以在项目的根目录下创建一个名为 .gitignore 的文件,列出不想被跟踪的文件模式即可.下 ...
- Swagger学习和实践
Swagger学习和实践 学习了:https://www.cnblogs.com/zxtceq/p/5530396.html swagger 英 [ˈswægə(r)] 美 [ˈswæɡɚ] vi.昂 ...
- 解决Sophos UTM 9防火墙上的“根分区填满”问题
Resolving 'Root Partition Is Filling Up' Issue on Sophos UTM Firewall 收到“Sophos UTM 9”防火墻的“根分區填满”问题的 ...
- 串匹配算法之BM算法
参考资料: http://blog.csdn.net/eric491179912/article/details/6210009 http://blog.163.com/pengfeicui@ye ...
- Selenium + Nightwatch 自动化测试环境搭建
首先要安装 Java 7 或更高 ,(http://www.oracle.com/technetwork/java/javase/downloads/index.html) 并且 java 命令可正常 ...
- 函数指针使用演示样例(參考Linux-内核代码)
本文有xhz1234(徐洪志)编写,转载请注明出处. http://blog.csdn.net/xhz1234/article/details/36635083 作者:徐洪志 近期阅读Linux-内核 ...
- android4.4 evaluateJavascript 到android2.X上不能调用的问题
android4.4上想用js注入的话.不能用旧的loadUrl()方法,每次load都会将页面又一次刷新一次. 可是在2.X的系统版本号上,evaluateJavascript 方法会报异常.解决的 ...
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...