使用c++设计:有一个数组a有n个元素,把数组中的0等概率替换成1~m的某个数,然后对数组排序,计算第k个位置的数,就是ak,计算ak的期望(不能使用rand()函数,确切的计算出来),并对结果mod998244353,输入是n,m,k,以及数组的n个值,使用O(n^2logn)以内的算法设计,直接写代码就可以了
时间: 2023-05-29 18:02:35 浏览: 152
基于C++设计哈夫曼压缩软件与解决男女运动员匹配问题【100012410】
#include <stdio.h>
#include <stdlib.h>
#define MOD 998244353
int n, m, k;
int a[100005];
int binom[100005][505]; // combinatorics: binom[n][k] = n choose k
// precomputes binom coefficients
void precompute_binom() {
for (int i = 0; i <= n; i++) {
binom[i][0] = binom[i][i] = 1;
for (int j = 1; j < i; j++) {
binom[i][j] = (binom[i-1][j-1] + binom[i-1][j]) % MOD;
}
}
}
int main() {
scanf("%d %d %d", &n, &m, &k);
precompute_binom();
int zero_count = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] == 0) zero_count++;
}
// compute expected value of each position
long long expected[100005] = {0};
for (int i = 0; i <= zero_count; i++) { // number of zeros replaced
for (int j = 1; j <= m; j++) { // value to replace with
int p = binom[zero_count][i]; // number of ways to choose i zeros to replace
p = 1LL * p * binom[n-zero_count][k-i-1] % MOD; // number of ways to choose k-i-1 other elements
expected[k] = (expected[k] + 1LL*p*j) % MOD; // add to expected value of kth position
}
}
// sort the array
qsort(a, n, sizeof(int), [](const void* a, const void* b) -> int {
return *(int*)a - *(int*)b;
});
// output answer
printf("%lld\n", expected[k]);
return 0;
}
阅读全文