HDU4045 Machine scheduling —— 隔板法 + 第二类斯特林数

题目链接:https://vjudge.net/problem/HDU-4045

Machine scheduling

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1907    Accepted Submission(s): 702


Problem Description
A Baidu’s engineer needs to analyze and process large amount of data on machines every day. The machines are labeled from 1 to n. On each day, the engineer chooses r machines to process data. He allocates the r machines to no more than m groups ,and if the difference of 2 machines' labels are less than k,they can not work in the same day. Otherwise the two machines will not work properly. That is to say, the machines labeled with 1 and k+1 can work in the same day while those labeled with 1 and k should not work in the same day. Due to some unknown reasons, the engineer should not choose the allocation scheme the same as that on some previous day. otherwise all the machines need to be initialized again. As you know, the initialization will take a long time and a lot of efforts. Can you tell the engineer the maximum days that he can use these machines continuously without re-initialization.
 
Input
Input end with EOF.
Input will be four integers n,r,k,m.We assume that they are all between 1 and 1000.
 
Output
Output the maxmium days modulo 1000000007.
 
Sample Input
5 2 3 2
 
Sample Output
6
Hint
Sample input means you can choose 1 and 4,1 and 5,2 and 5 in the same day. And you can make the machines in the same group or in the different group. So you got 6 schemes. 1 and 4 in same group,1 and 4 in different groups. 1 and 5 in same group,1 and 5 in different groups. 2 and 5 in same group,2 and 5 in different groups. We assume 1 in a group and 4 in b group is the same as 1 in b group and 4 in a group.
 
Source

题意:

从1~n中选出r个数,要求这r个数之间每对数的差值大于等于k;选出之后,再将这r个数分成m组。问总共有多少种方案?

题解:

问题分为两个部分进行求解:

1.如果正确选出这r个数呢?

如图,O代表选出的r个数,双下划线代表相邻两个数之间的差值。由于数字从1开始,所以最左边应该填上1;由于相邻两个数之间差值最小为k,所以出于中间的下划线应该填上k,这样就满足题目的限定。还剩下 n-1-(r-1)*k,然后再把他们分到r+1个下划线上。根据隔板法,总共有 C[n-1-(r-1)*k+r+1-1][r+1-1] = C[n-1-(r-1)*k+r][r] 。

2.把r个数分成m组,就是第二类斯特林数了。注意r可能小于m, 所以应为 S[r][min(r,m)] 。

3.所以总的方案数为: C[n-1-(r-1)*k+r][r] * S[r][min(r,m)] 。

4.注意,当n<1+(r-1)*k时,即连最基本的条件都满足不了时,方案数为0,需要特判。

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 1e3+10;
18 
19 LL S[MAXN][MAXN], f[MAXN][MAXN], C[MAXN][MAXN];
20 
21 void init()
22 {
23     for(int i = 0; i<MAXN; i++)
24     {
25         C[i][0] = 1;
26         for(int j = 1; j<=i; j++)
27             C[i][j] = (C[i-1][j-1]+C[i-1][j])%MOD;
28     }
29 
30     for(int i = 1; i<MAXN; i++)
31     {
32         S[i][0] = 0; S[i][i] = 1;
33         for(int j = 1; j<i; j++)
34             S[i][j] = ((j*S[i-1][j])%MOD + S[i-1][j-1])%MOD;
35     }
36 
37     memset(f, 0, sizeof(f));
38     for(int i = 1; i<MAXN; i++)
39         for(int j = 1; j<=i; j++)
40             f[i][j] = (f[i][j-1] + S[i][j])%MOD;
41 }
42 
43 int main()
44 {
45     init();
46     int n, r, k, m;
47     while(scanf("%d%d%d%d", &n,&r,&k,&m)!=EOF)
48     {
49         LL ans;
50         if(1+(r-1)*k>n) ans = 0;
51         else ans = (1LL*C[n-1-(r-1)*k+r][r]*f[r][min(r,m)])%MOD;
52         printf("%lld
", ans);
53     }
54 }
View Code
原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8335724.html