Codeforces Gym - 101147G The Galactic Olympics
Discription
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
1
3 2
6
首先每个人至少要参加一个项目,并且一个项目最多只能属于一个人,所以我们可以很容易的建出模型: 有n个不同的物品,我们要把它们分别放入k个有序的集合,并且要求每个集合至少要有一个物品,求方案总数。
我们知道,当集合无序的时候,方案数就是 S(n,k) ,即第二类斯特林数的第n行第k列的值; 集合有序的时候,再乘上 k! 就行了。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1000000007;
const int maxn=1005;
int T,n,m,S[maxn][maxn],jc[maxn];
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline void init(){
S[0][0]=jc[0]=1;
for(int i=1;i<=1000;i++){
jc[i]=jc[i-1]*(ll)i%ha;
for(int j=1;j<=i;j++) S[i][j]=add(S[i-1][j-1],S[i-1][j]*(ll)j%ha);
}
}
int main(){
freopen("galactic.in","r",stdin);
init(),scanf("%d",&T);
while(T--) scanf("%d%d",&n,&m),printf("%d\n",n<m?0:(int)(S[n][m]*(ll)jc[m]%ha));
return 0;
}
Codeforces Gym - 101147G The Galactic Olympics的更多相关文章
- 【第二类Stirling数】Gym - 101147G - The Galactic Olympics
如果K>n,就无解: 如果K==n,就答案是P(n,n): 如果K<n,答案就是s(n,K)*P(K,K): P为排列数,s为第二类斯特林数. 第二类斯特林数就是将n个球,划分为K个非空集 ...
- Gym - 101147G G - The Galactic Olympics —— 组合数学 - 第二类斯特林数
题目链接:http://codeforces.com/gym/101147/problem/G G. The Galactic Olympics time limit per test 2.0 s m ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
随机推荐
- 中国电信物联网平台入门学习笔记2: DOME程序分析
"墨子号NB-IOT开发板"提供的dome: 程序只要分为延时,定时器,串口通讯…… 工程文件在:…\STM32L1xx_StdPeriph_Lib_V1.3.1\Project\ ...
- Mac中文乱码问题
在终端切换到文档所在的目录,输入下面的命令: iconv -c -f GB2312 -t UTF-8 乱码的文件名 >> 新文件的名称
- AND和OR
AND和OR用于组合多个选择条件,即用于组合where之中的多个条件
- Ubuntu首次登入与在线求助man page总结
1.为了避免瞬间断电造成的Linux系统的危害,建议做为服务器的Linux主机应该加上不断电系统来持续提供稳定的电力 2.默认的图形模式登入中,可以选择语系以及作业阶段.作业阶段为多种窗口管理员软件所 ...
- tarjan - tarjan的几种用法
前言 tarjan是一种神奇的算法, 它可以在线性时间内求强联通分量/缩点/LCA/割点/割边/... 但由于博主咸鱼,暂时掌握不了这么多, 先讲讲其中最简单的一些. 概述 tarjan是以DFS为基 ...
- LoadRunner 11破解方法
名称:HP Loadrunner Software 11.00 版本号:11.00.0.0 安装环境:Win 7 软件安装成功后,会弹出提示告知license的有效期为10天. 破解方法: 1.下载破 ...
- 25、Base64
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3 ...
- Mongodb 删除记录里的某个字段
//例如要把User表中address字段删除 db.User.update({},{$unset:{'address':''}},false, true)
- 九度oj 题目1357:疯狂地Jobdu序列
题目描述: 阳仔作为OJ的数据管理员,每一周的题目录入都让其很抓狂,因为题目不是他出的,他控制不了出题的速度……在等题目的时候,阳仔又不敢出去打篮球,所以只能在纸上乱涂乱写,这天,阳仔在纸上写下了这样 ...
- 用java通过键盘输入若干个int,直到输入#结束
转 import java.util.ArrayList; import java.util.Scanner; public class Test { public static void main( ...