LionBlaze @ 2024-11-28 18:09:35
rt。
问题:
给定
显然生成所有数字再排序不是正确的,当
容易验证概率和为
更进一步地,如果我们有一个能够均匀随机地产生某种类型(比如一个类)的随机对象产生器,同时有这种类型的小于号重载(可以比较两个对象,并且性质良好),还有这种类型的等于号重载,给定 a<b || a==b
)的序列
解答必关。
by LionBlaze @ 2024-11-28 19:07:30
普通版的加强版:随机的区间可以不是
加强版的加强版:
by djfuck @ 2024-11-28 19:07:42
#include <bits/stdc++.h>
int N = 5, M = 5;
int64_t factorial(int n) {
int64_t res = 1;
for (int i = 1; i <= n; ++i) {
res *= i;
}
return res;
}
int64_t binom(int n, int m) { return factorial(n) / factorial(m) / factorial(n - m); }
int64_t f(int n, int m) { return binom(n + m - 1, n); }
int64_t g(int start, int len) { return f(len, M - start + 1); }
void generate(int x) {
std::vector<int> v(1, 1);
for (int i = 1; i <= N; ++i) {
int tmp = 0;
for (int d = v.back(); d <= M; ++d) {
tmp += g(d, N - i);
if (tmp >= x) {
tmp -= g(d, N - i);
x -= tmp;
v.push_back(d);
break;
}
}
}
for (int i = 1; i <= N; ++i) {
std::cout << v[i] << " ";
}
std::cout << std::endl;
}
int main() {
int S = f(N, M);
for (int x = 1; x <= S; ++x) {
generate(x);
}
return 0;
}
好像是这个. 我再看一下
by LionBlaze @ 2024-11-28 19:11:08
这么强 /bx/bx
by LionBlaze @ 2024-11-28 19:11:26
/bx/sto/orz/%%%
by LionBlaze @ 2024-11-28 19:15:37
by djfuck @ 2024-11-28 19:17:52
这个方法本身就有很大的缺陷啊,
一旦
by LionBlaze @ 2024-11-28 19:21:42
emmm 确实。
by djfuck @ 2024-11-28 19:24:26
#include <bits/stdc++.h>
int N = 7, M = 7;
int64_t factorial(int n) {
int64_t res = 1;
for (int i = 1; i <= n; ++i) {
res *= i;
}
return res;
}
int64_t f(int n, int m) {
int64_t res = 1;
for (int i = n + 1; i <= n + m - 1; ++i) {
res *= i;
}
for (int i = 1; i <= m - 1; ++i) {
res /= i;
}
return res;
}
void generate(int x) {
std::vector<int> v(1, 1);
for (int i = 1; i <= N; ++i) {
for (int d = v.back();; ++d) {
x -= f(N - i, M - d + 1);
if (x <= 0) {
x += f(N - i, M - d + 1);
v.push_back(d);
break;
}
}
}
for (int i = 1; i <= N; ++i) {
std::cout << v[i] << " ";
}
std::cout << std::endl;
}
int main() {
int S = f(N, M);
std::random_device rd;
std::uniform_int_distribution<int> dist(1, S);
int x = dist(rd);
generate(x);
return 0;
}
by not_clever_syl @ 2024-11-28 19:27:44
考虑把序列映射成 shuffle
by djfuck @ 2024-11-28 19:31:20
但是这种做法是否只能使