Kevin's Problem

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4921

Description

In his applied probability class, Kevin learned about the Secretary Problem.

There are N applicants applying for secretary position in a company. The applicants are

interviewed by a hiring manager one by one in arbitrary order. During the interview, the

manager can rank the applicant’s quality uniquely relative to all applicants which have been

interviewed so far, but he has no idea of the quality of applicants yet to be interviewed.

The decision whether the manager should hire or reject the applicant must be done right

after the interview ended (before he starts an interview with other applicant) and cannot be

changed, i.e. once rejected, that particular applicant cannot be considered anymore. There

is only one position available, so what is the best decision strategy for this problem?

One reasonable strategy is: reject the first K applicants and hire the first remaining applicant who

is better than all previous interviewed applicants, or hire the last one if there is no such applicant.

Unfortunately, Kevin did not pay a full attention in his class. He misunderstood the strategy;

instead of “. . . hire the first remaining applicant who is better than all previous interviewed applicants

. . . ”, he thought it is “. . . hire the first remaining applicant who is better than the (immediate) previous

interviewed applicant . . . ”. Let’s call this variation as Kevin’s strategy.

Given N, K, and p determine in how many ways (interview order) such that the applicant whose

rank is p among all applicants will be selected by Kevin’s strategy. For example, let N = 4, K = 2,

and p = 2. Among all permutation of 1 . . . N, there are only 7 possible permutation such that the 2nd

rank applicant is selected by Kevin’s strategy.

• 1, 3, 2, 4 — 2 is selected because it’s better than 3.

• 1, 3, 4, 2 — 2 is selected because 4 is worse than 3, and 2 is better than 4.

• 1, 4, 2, 3

• 3, 1, 4, 2

• 3, 4, 2, 1

• 4, 1, 3, 2

• 4, 3, 2, 1

Note that the first 2 applicants will not be hired in this example (K = 2). Clearly, the 2nd rank

applicant will not be selected in any permutation where she appears in the first K.

An applicant has a better rank if her rank is higher (smaller number), e.g. 2nd rank is better than

5th.

Input

The first line of input contains an integer T (T ≤ 100) denoting the number of cases. Each case contains

three integers: N, K, and p (2 ≤ N ≤ 500; 1 ≤ K < N; 1 ≤ p ≤ N) as explained in the problem

description above.

Output

For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is the number

of permutation of 1 . . . N such that the p-th rank applicants is selected by Kevin’s strategy for that

particular case. As this number could be very large, modulo Y by 1,000,000,007.

Explanation for 3rd sample case:

There are 5 applicants and Kevin’s strategy will reject the first 3. 5th rank applicant will be

selected only if she is interviewed last, thus the manager has no choice but to hire her. Among 24

possible permutations where 5th rank applicant appears last, only 12 permutations which make her

hired:

1, 2, 3, 4, 5 2, 1, 3, 4, 5 3, 1, 2, 4, 5 4, 1, 2, 3, 5

1, 3, 2, 4, 5 2, 3, 1, 4, 5 3, 2, 1, 4, 5 4, 2, 1, 3, 5

1, 4, 2, 3, 5 2, 4, 1, 3, 5 3, 4, 1, 2, 5 4, 3, 1, 2, 5

Some examples where 5th rank will not be hired even though she is the last:

1, 2, 4, 3, 5 – 3rd rank will be hired.

1, 4, 3, 2, 5 – 2nd rank will be hired.

3, 2, 4, 1, 5 – 1st rank will be hired.

. . .

Sample Input

4

4 2 2

5 2 3

5 3 5

8 4 2

Sample Output

Case #1: 7

Case #2: 26

Case #3: 12

Case #4: 7890

Hint

题意

有一个公司,有编号为1~n,也是他们能力排名的人来应聘。

公司准备只招一个人,他的招聘采用如下策略:

先排除前k个人,然后再看,如果这个人比前一个人厉害,那就录取。

如果没有人录取的话,那就录取最后一个人。

问你有n个人,现在k个人排除,录取能力为p的人的方案数有多少种。

题解:

数学题,前面k个我们不用管,然后到第p个人,那么第p个人前面的应该是一个能力递减编号递增的序列,然后剩下位置就全排列。

然后这样枚举p的位置,然后搞一波就好了。

代码

#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
#define lowbit(x) ((x)&(-x))
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std; inline void up(int & x ,int v){ x += v; if( x >= mod ) x -= mod; }
/*
dp(i , j , k , f)
已经放了 i 个 , 有 j 个比 p 小的已经放了, k -> 前面一个放的是谁 , f 表示 p 放了没有
*/ const int maxn = 500 + 15;
int N,K,P,C[maxn][maxn],fac[maxn]; void init(){
fac[0] = 1;
for(int i = 1 ; i < maxn ; ++ i) fac[i] = mul( fac[i - 1] , i );
C[0][0] = 1;
for(int i = 1 ; i < maxn ; ++ i){
C[i][0] = 1;
for(int j = 1 ; j <= i ; ++ j) up( C[i][j] , C[i - 1][j - 1] + C[i - 1][j] );
}
} int A(int n , int m){
return mul( fac[n] , qpow( fac[n - m] , mod - 2 ) );
} int main(int argc,char *argv[]){
init();
int T=read(),cas=0;
while(T--){
N=read(),K=read(),P=read();
int ans = 0;
up( ans , A( N - 1 , K - 1 ) );
for(int i = K + 1 ; i < N ; ++ i){
for(int j = P + 1 ; j <= N ; ++ j){
if( j - 2 < 0 || j - 2 < i - K - 1 ) continue;
int y = C[ j - 2 ][ i - K - 1 ];
up( ans , mul( y , fac[N - i + K - 1] ) );
}
}
pf("Case #%d: %d\n" , ++ cas , ans );
}
return 0;
}

UVALive 6909 Kevin's Problem 数学排列组合的更多相关文章

  1. 4535 ACM 礼尚往来 数学排列组合

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4535 题意:每个礼物都不相同的组合个数 数学规律: 将每个女友排序为1···n,对应的女友送男友的礼物排序 ...

  2. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合

    E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...

  3. 【BZOJ1008】【HNOI2008】越狱(数学排列组合题)

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3140  Solved: 1317[Submit][Status] ...

  4. UVa Problem 10132 File Fragmentation (文件还原) 排列组合+暴力

    题目说每个相同文件(01串)都被撕裂成两部分,要求拼凑成原来的样子,如果有多种可能输出一种. 我标题写着排列组合,其实不是什么高深的数学题,只要把最长的那几个和最短的那几个凑一起,然后去用其他几个验证 ...

  5. UVaLive 7360 Run Step (排列组合,枚举)

    题意:给定一个数 n ,表示一共有 n 步,然后你可以迈一步也可以迈两步,但是左腿和右腿的一步和两步数要一样,并且两步数不小于一步数,问你有多少种方式. 析:虽然是排列组合,但还是不会做.....水啊 ...

  6. 【排列组合】ZSC1076: 数学、不容易系列之三——考新郎

    国庆期间,省城刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的: 首先,给每位新娘打扮得几乎一模一样,并 ...

  7. UVa 12712 && UVaLive 6653 Pattern Locker (排列组合)

    题意:给定 一个n * n 的宫格,就是图案解锁,然后问你在区间 [l, r] 内的所有的个数进行组合,有多少种. 析:本来以为是数位DP,后来仔细一想是排列组合,因为怎么组合都行,不用考虑实际要考虑 ...

  8. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合

    C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  9. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

随机推荐

  1. bzoj千题计划186:bzoj1048: [HAOI2007]分割矩阵

    http://www.lydsy.com/JudgeOnline/problem.php?id=1048 #include<cmath> #include<cstdio> #i ...

  2. Oozie 生成JMS消息并向 JMS Provider发送消息过程分析

    一,涉及到的工程 从官网下载源码,mvn 编译成 Eclipse工程文件:

  3. 产品排序(2015 年北大自招夏令营) (与栈相关的区间DP)

    题面: \(solution:\) 又是一道\(DP\)的好题啊!状态并不明显,需要仔细分析,而且还结合了栈的特性! 做这一类题,只要出题人有点理想,一定会在栈的性质上做点文章,所以我们尽量围绕栈的性 ...

  4. 关于cookie和session

    在设置cookie的时候,它会保留在本地,无论你有没有退出浏览器都是.但是session只能在登录状态有效.退出浏览器过后就会消除掉.同时设置也是有问题的. @app.route('/login',m ...

  5. 视觉中的经典图像特征小结(一): 颜色直方图, HOG, LBP

    [普兒原创, 如有错误和纰漏欢迎指正. 更新中...] 1. 颜色直方图 颜色空间在本质上是定义在某种坐标系统下的子空间,空间中的每一个坐标表示一种不同的颜色.颜色空间的目的在于给出某种颜色标准,使得 ...

  6. 面积并+扫描线 覆盖的面积 HDU - 1255

    题目链接:https://cn.vjudge.net/problem/HDU-1255 题目大意:中文题目 具体思路:和上一篇的博客思路差不多,上一个题求的是面积,然后我们这个地方求的是啊覆盖两次及两 ...

  7. Dream_Spark-----Spark 定制版:003~Spark Streaming(三)

    Spark 定制版:003~Spark Streaming(三) 本讲内容: a. Spark Streaming Job 架构和运行机制 b. Spark Streaming Job 容错架构和运行 ...

  8. 云计算--hbase shell

    具体的 HBase Shell 命令如下表 1.1-1 所示: 下面我们将以“一个学生成绩表”的例子来详细介绍常用的 HBase 命令及其使用方法. 这里 grad 对于表来说是一个列,course ...

  9. 在 Linux 上找出并解决程序错误的主要方法【转】

    转自:https://www.ibm.com/developerworks/cn/linux/sdk/l-debug/index.html 本文讨论了四种调试 Linux 程序的情况.在第 1 种情况 ...

  10. lvs+keepalived+nginx实现高性能负载均衡集群【转】

    转自 lvs+keepalived+nginx实现高性能负载均衡集群 - 青衫lys - 博客园http://www.cnblogs.com/liuyisai/p/5990645.html 一.为什么 ...