QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602244 | #990. Colorful Components | Made_in_Code | AC ✓ | 55ms | 5412kb | C++14 | 2.8kb | 2024-09-30 21:46:24 | 2024-09-30 21:46:25 |
Judging History
answer
#include <iostream>
#define LL long long
using namespace std;
const int kMaxN = 301, kMod = 1e9 + 7;
int n, m, p, a[kMaxN];
LL pw[kMaxN], fact[kMaxN], _fact[kMaxN];
LL f[kMaxN][kMaxN]; // i 个点实线连成 j 棵树使得每棵树不超过 m 且最后用虚线连成 1 棵树的方案数
LL g[kMaxN][kMaxN]; // g_{i, 1} 为容斥后 i 个点连成 1 棵树的方案数 \
容斥的效果为若干 g_{i, 1} 分别以 i 个标号再次连成 1 棵树的方案数恰好为 f_{i, 1} \
g_{i, j} 为 i 个点 j 个 g_{x, 1} 的并集的方案数 (即 Exp) \
且为了之后统计答案每个 g_{x, 1} 带 x 的权
LL h[kMaxN][kMaxN]; // 考虑了颜色 i, 形成了 j 个 g_{x, 1} 的并集的方案数, 同样带权
LL ans; // 将 h_{?, i} 的 i 个 g_{x, 1} 连成 1 棵树, 统计答案
LL Pow(LL x, int y = kMod - 2) {
LL ans = 1;
y = (y + kMod - 1) % (kMod - 1);
for (int i = 1; i <= y; i <<= 1) {
if (i & y) {
ans = ans * x % kMod;
}
x = x * x % kMod;
}
return ans;
}
LL C(int x, int y) { return fact[x] * _fact[y] % kMod * _fact[x - y] % kMod; }
void Add(LL &x, LL y) { x = (x + y) % kMod; }
int main() {
cin.tie(0), cout.tie(0);
ios::sync_with_stdio(0);
fact[0] = 1;
for (int i = 1; i < kMaxN; i++) {
fact[i] = fact[i - 1] * i % kMod;
}
_fact[kMaxN - 1] = Pow(fact[kMaxN - 1]);
for (int i = kMaxN - 1; i >= 1; i--) {
_fact[i - 1] = _fact[i] * i % kMod;
}
cin >> n >> m;
for (int i = 1, x; i <= n; i++) {
cin >> x, a[x]++;
}
for (int i = 1; i <= m; i++) {
pw[i] = Pow(i, i - 1);
}
f[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
for (int k = 1; k <= m && i + k <= n; k++) {
Add(f[i + k][j + 1], f[i][j] * C(i + k - 1, i) % kMod * pw[k]);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
f[i][j] = f[i][j] * Pow(i, j - 2) % kMod;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
Add(g[i][1], j & 1 ? f[i][j] : kMod - f[i][j]);
}
g[i][1] = g[i][1] * i % kMod;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
for (int k = 1; i + k <= n; k++) {
Add(g[i + k][j + 1], g[i][j] * C(i + k - 1, i) % kMod * g[k][1]);
}
}
}
h[0][0] = 1;
for (int i = 1; i <= n; i++) {
if (a[i]) {
for (int j = 0; j <= n; j++) {
for (int k = 1; k <= a[i] && j + k <= n; k++) {
Add(h[i][j + k], h[p][j] * g[a[i]][k]);
}
}
p = i;
}
}
for (int i = 1; i <= n; i++) {
Add(ans, h[p][i] * Pow(n, i - 2));
}
cout << ans << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
input:
5 3 1 1 3 1 5
output:
125
result:
ok "125"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
4 2 2 1 1 1
output:
7
result:
ok "7"
Test #3:
score: 0
Accepted
time: 33ms
memory: 5068kb
input:
300 16 2 2 2 4 5 1 5 3 5 4 2 1 4 4 4 5 2 1 5 4 3 4 5 3 5 5 1 3 1 1 2 5 5 3 3 2 5 2 3 2 2 4 2 2 2 4 4 2 2 4 1 3 3 4 1 3 3 4 3 4 3 5 5 4 3 3 1 2 1 2 5 2 2 4 3 3 5 3 2 4 3 5 1 4 5 5 2 3 2 3 4 4 5 5 5 5 4 5 3 2 4 4 4 3 5 3 1 1 3 5 5 4 5 2 5 5 5 2 2 2 3 1 5 4 1 4 3 5 1 4 4 2 5 2 2 4 5 3 4 3 3 4 2 5 1 1 3...
output:
540253743
result:
ok "540253743"
Test #4:
score: 0
Accepted
time: 34ms
memory: 5000kb
input:
300 20 2 7 4 5 1 10 3 10 9 2 6 4 9 4 5 2 6 10 9 8 4 10 8 5 5 1 3 1 1 7 5 5 3 8 7 10 2 3 2 7 4 7 7 7 9 9 2 7 9 6 3 3 4 1 8 8 4 3 4 8 5 10 9 3 3 6 7 6 7 10 7 2 9 3 3 10 3 7 4 8 10 1 4 5 10 2 3 7 3 4 9 5 10 5 10 9 5 3 2 9 4 4 3 10 3 6 1 8 5 10 4 5 7 10 5 5 7 2 7 3 1 5 4 1 9 3 5 1 9 4 7 10 2 2 4 10 8 9 ...
output:
616258392
result:
ok "616258392"
Test #5:
score: 0
Accepted
time: 29ms
memory: 5412kb
input:
300 1 124 25 131 70 63 150 139 62 46 94 9 34 25 102 66 120 139 28 134 120 98 135 95 21 43 71 11 87 45 15 33 58 37 70 12 63 132 47 104 97 67 17 9 119 72 87 29 96 53 103 34 71 58 78 34 3 94 78 115 60 139 43 63 46 127 146 37 60 37 12 59 23 43 120 53 107 54 68 70 21 94 125 10 22 143 117 133 64 129 55 14...
output:
450350134
result:
ok "450350134"
Test #6:
score: 0
Accepted
time: 26ms
memory: 5400kb
input:
300 2 131 70 63 150 139 62 46 94 9 34 25 102 66 120 139 28 134 120 98 135 95 21 43 71 11 87 45 15 33 58 37 70 12 63 132 47 104 97 67 17 9 119 72 87 29 96 53 103 34 71 58 78 34 3 94 78 115 60 139 43 63 46 127 146 37 60 37 12 59 23 43 120 53 107 54 68 70 21 94 125 10 22 143 117 133 64 129 55 140 75 10...
output:
942808207
result:
ok "942808207"
Test #7:
score: 0
Accepted
time: 47ms
memory: 5132kb
input:
300 150 1 2 2 2 1 2 1 2 2 2 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 1 2 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 1 2 1 2 2 2 1 2 2 1 2 1 1 1 2 1 2 1 2 1 2 1 1 1 2 1 1 2 2 2 1 2 1 2 2 1 1 1 2 1 1 2 1 2 1 1 1 2 1 2 2 1 2 1 2 1 2 1 2 2 1 1 2 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1 2 1 2 2 2 2 2 2 1 1 1 2 2 1 1 1 1 1 2 1 1 2 1 1 1 ...
output:
169425341
result:
ok "169425341"
Test #8:
score: 0
Accepted
time: 50ms
memory: 5072kb
input:
300 290 2 2 1 2 1 2 2 2 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 1 2 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 1 2 1 2 2 2 1 2 2 1 2 1 1 1 2 1 2 1 2 1 2 1 1 1 2 1 1 2 2 2 1 2 1 2 2 1 1 1 2 1 1 2 1 2 1 1 1 2 1 2 2 1 2 1 2 1 2 1 2 2 1 1 2 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1 2 1 2 2 2 2 2 2 1 1 1 2 2 1 1 1 1 1 2 1 1 2 1 1 1 1 2 ...
output:
394671363
result:
ok "394671363"
Test #9:
score: 0
Accepted
time: 55ms
memory: 4972kb
input:
300 290 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 ...
output:
0
result:
ok "0"
Test #10:
score: 0
Accepted
time: 44ms
memory: 5052kb
input:
300 80 1 3 3 3 1 1 2 3 2 3 2 3 1 2 2 3 3 3 3 1 1 1 3 3 3 2 2 1 1 2 3 2 3 3 2 3 2 1 1 2 1 3 1 3 1 3 1 3 1 1 3 1 1 2 1 3 1 3 2 2 1 3 2 2 3 2 1 3 1 2 1 1 2 3 1 1 3 1 2 3 1 1 1 1 1 2 1 1 2 1 3 2 2 2 1 3 1 2 3 3 1 3 3 1 3 1 2 2 2 2 1 1 2 1 1 1 1 2 1 3 2 2 1 1 1 3 1 3 1 1 3 1 2 2 1 3 1 1 3 1 2 3 2 2 1 3 2...
output:
428950103
result:
ok "428950103"
Test #11:
score: 0
Accepted
time: 39ms
memory: 5068kb
input:
300 50 3 3 1 1 2 3 2 3 2 3 1 2 2 3 3 3 3 1 1 1 3 3 3 2 2 1 1 2 3 2 3 3 2 3 2 1 1 2 1 3 1 3 1 3 1 3 1 1 3 1 1 2 1 3 1 3 2 2 1 3 2 2 3 2 1 3 1 2 1 1 2 3 1 1 3 1 2 3 1 1 1 1 1 2 1 1 2 1 3 2 2 2 1 3 1 2 3 3 1 3 3 1 3 1 2 2 2 2 1 1 2 1 1 1 1 2 1 3 2 2 1 1 1 3 1 3 1 1 3 1 2 2 1 3 1 1 3 1 2 3 2 2 1 3 2 2 3...
output:
283683661
result:
ok "283683661"
Test #12:
score: 0
Accepted
time: 39ms
memory: 5368kb
input:
300 50 1 1 2 3 2 3 2 3 1 2 2 3 3 3 3 1 1 1 3 3 3 2 2 1 1 2 3 2 3 3 2 3 2 1 1 2 1 3 1 3 1 3 1 3 1 1 3 1 1 2 1 3 1 3 2 2 1 3 2 2 3 2 1 3 1 2 1 1 2 3 1 1 3 1 2 3 1 1 1 1 1 2 1 1 2 1 3 2 2 2 1 3 1 2 3 3 1 3 3 1 3 1 2 2 2 2 1 1 2 1 1 1 1 2 1 3 2 2 1 1 1 3 1 3 1 1 3 1 2 2 1 3 1 1 3 1 2 3 2 2 1 3 2 2 3 2 1...
output:
280919911
result:
ok "280919911"
Test #13:
score: 0
Accepted
time: 54ms
memory: 5132kb
input:
300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 ...
output:
394671363
result:
ok "394671363"
Test #14:
score: 0
Accepted
time: 54ms
memory: 5056kb
input:
300 299 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 ...
output:
0
result:
ok "0"