CodeChef - KSPHERES Spheres
vjudge传送
内含中文
Eugene has a sequence of upper hemispheres and another of lower hemispheres. The first set consists of N upper hemispheres indexed 1 to N and the second has M lower hemispheres indexed 1 to M. The hemispheres in any sequence can have different radii.
He is now set out on building spheres using them. To build a sphere of radius R, he must take one upper half of the radius R and one lower half of the radius R. Also, he can put a sphere into a bigger one and create a sequence of nested concentric spheres. But he can’t put two or more spheres directly into another one.
Examples:
If there is a sequence of (D+1) nested spheres, we can call this sequence as a D-sequence.
Eugene has to find out how many different X-sequence are possible (1 <= X <= C). An X sequence is different from another if the index of any of the hemisphere used in one X-sequence is different from the other. Eugene doesn’t know how to do it, so Chef agreed to help him. Of course, Chef is too busy now and he asks you to help him.
Input
The first line contains a three integers: N denoting the number of upper sphere halves, M denoting the number of lower sphere halves and C.
The second line contains N space-separated integers U1, U2, …, UN denoting the radii of upper hemispheres.
The third line contains M space-separated integers L1, L2, …, LM denoting the radii of lower hemispheres.
Output
Output a single line containing C space-separated integers D1, D2, …, DC, where Di is the number of ways there are to build i-sequence in modulo 109+7.
Constraints
1 ≤ N ≤ 105
1 ≤ M ≤ 105
1 ≤ C ≤ 103
1 ≤ Ui ≤ C
1 ≤ Li ≤ C
Subtasks
Subtask 1: 1 ≤ N, M, C ≤ 10 - 15 points
Subtask 2: 1 ≤ N, M, C ≤ 100 - 25 points
Subtask 3: Original constraints - 60 points
Example
Input:
3 4 3
1 2 3
1 1 3 2
Output:
5 2 0
Explanation
We can build spheres with such radii:
R=1 and there are 2 ways to do it. (We can choose any of 2 lower hemispheres with R=1)
R=2 and there is 1 way to do it.
R=3 and there is 1 way to do it.
We can build these 1-sequences:
1->2 and there are 2 ways to do it. ( sphere with radius 1 is inside sphere with radius 2, we can choose any of 2 ways of building sphere with radius 1)
1->3 and there are 2 ways to do it.
2->3 and there is 1 way to do it.
2 + 2 + 1 = 5
We can build these 2-sequences:
1->2->3 and there are 2 ways to do it.
We can’t build 3-sequences, because we don’t have 4 spheres of different radii.
题解:
先预处理出能构成的(各种半径)球的数量。
然后定义dp[i][j] 表示以第 i 个球结尾, 组成 [(j - 1) - 序列] 的方案数
再定义sum[i][j] 表示以第1 ~ i 个球, 组成 [(j - 1) - 序列] 的方案数
即sum[i][j] = dp[1][j] + … +dp[i][j] = sum[i - 1][j] + dp[i][j]
则最后的结果为 sum[cnt][k] (cnt为球的总数,详细看代码,
2<= k <= c + 1)
代码如下
#include <iostream>
#include <cstdio>
using namespace std;
long a[1005], b[1005];
long long num[1005], dp[1005][1005], sum[1005][1005];
const int mod = 1000000007;
int main(){
int n, m, c;
scanf("%d%d%d", &n, &m, &c);
int i, j, x;
for(i = 1; i <= n; i++){ //预处理
scanf("%d", &x);
a[x]++;
}
int cnt = 0;
for(i = 1; i <= m; i++){
scanf("%d", &x);
b[x]++;
}
for(i = 1; i <= 1001; i++){
if(a[i] != 0 && b[i] != 0){
cnt++;
num[cnt] = (a[i] * b[i]) % mod;
}
}
for(i = 1; i <= cnt; i++){ //核心
dp[i][1] = num[i];
for(j = 2; j <= i; j++) dp[i][j] = (sum[i - 1][j - 1] * num[i]) % mod;
for(j = 1; j <= i; j++) sum[i][j] = (sum[i - 1][j] + dp[i][j]) % mod;
}
for(i = 2; i <= c + 1; i++)
printf("%d ", sum[cnt][i]);
return 0;
}
是不是对本题有更深的认识?