QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#310808 | #4781. 完美的集合 | hos_lyric# | 13 | 2ms | 5412kb | C++14 | 12.4kb | 2024-01-21 18:16:48 | 2024-07-04 03:20:23 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned long long M_> struct ModLong {
static constexpr unsigned long long M = M_;
static constexpr long double INV_M = 1.0L / M;
unsigned long long x;
ModLong() : x(0ULL) {}
ModLong(unsigned x_) : x(x_ % M) {}
ModLong(unsigned long long x_) : x(x_ % M) {}
ModLong(int x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModLong(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModLong &operator+=(const ModLong &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModLong &operator-=(const ModLong &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModLong &operator*=(const ModLong &a) {
// https://github.com/kth-competitive-programming/kactl/blob/main/doc/modmul-proof.pdf
// This works at least for M < 2^62, assuming the usual 80-bit long double.
const long long y = x * a.x - M * static_cast<unsigned long long>(INV_M * x * a.x);
x = (y < 0LL) ? (y + M) : (y >= static_cast<long long>(M)) ? (y - M) : y;
return *this;
}
ModLong &operator/=(const ModLong &a) { return (*this *= a.inv()); }
ModLong pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModLong a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModLong inv() const {
unsigned long long a = M, b = x; long long y = 0, z = 1;
for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast<long long>(q) * z; y = z; z = w; }
assert(a == 1ULL); return ModLong(y);
}
ModLong operator+() const { return *this; }
ModLong operator-() const { ModLong a; a.x = x ? (M - x) : 0ULL; return a; }
ModLong operator+(const ModLong &a) const { return (ModLong(*this) += a); }
ModLong operator-(const ModLong &a) const { return (ModLong(*this) -= a); }
ModLong operator*(const ModLong &a) const { return (ModLong(*this) *= a); }
ModLong operator/(const ModLong &a) const { return (ModLong(*this) /= a); }
template <class T> friend ModLong operator+(T a, const ModLong &b) { return (ModLong(a) += b); }
template <class T> friend ModLong operator-(T a, const ModLong &b) { return (ModLong(a) -= b); }
template <class T> friend ModLong operator*(T a, const ModLong &b) { return (ModLong(a) *= b); }
template <class T> friend ModLong operator/(T a, const ModLong &b) { return (ModLong(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModLong &a) const { return (x == a.x); }
bool operator!=(const ModLong &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModLong &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
using ModInt = ModLong<11920928955078125>;
struct BinomPE {
static constexpr int p = 5;
static constexpr int e = 23;
static constexpr unsigned long long M = 11920928955078125;
ModInt mod(int x_) const {
return x_;
}
ModInt mod(long long x_) const {
return x_;
}
ModInt add(ModInt a, ModInt b) const {
return a + b;
}
ModInt mul(ModInt a, ModInt b) const {
return a * b;
}
ModInt pow(ModInt a, long long e_) const {
return a.pow(e_);
}
ModInt inv(ModInt a) const {
return a.inv();
}
// pp[i] = p^i
vector<unsigned long long> pp;
// period mod p^e of (-1 mod p)
unsigned long long period;
// s1[n][k] = |s(n+1, k+1)| (in particular, s1[p-1][0] = (p-1)!)
vector<vector<ModInt>> s1;
// fs[u] = \prod[0<=i<u] range(i, p-1): poly in u of deg=2e-2
int deg;
vector<ModInt> fs;
// valFac[i] = v_p(i!), invFac[i] = (i! / p^(v_p(i!)))^-1
vector<int> valFac;
vector<ModInt> invFac;
BinomPE() {
assert(p >= 2); assert(e >= 1);
pp.resize(e + 1);
pp[0] = 1;
for (int i = 1; i <= e; ++i) pp[i] = pp[i - 1] * p;
// setM(pp[e]);
period = ((p == 2 && e >= 3) ? 1 : 2) * pp[e - 1];
s1.assign(p, vector<ModInt>(min(p, e), ModInt{0U}));
s1[0][0] = ModInt{1U};
for (int n = 1; n < p; ++n) {
s1[n][0] = mul(mod(n), s1[n - 1][0]);
for (int k = 1; k < min(p, e); ++k) s1[n][k] = add(s1[n - 1][k - 1], mul(mod(n), s1[n - 1][k]));
}
const ModInt invFac1 = inv(s1[p - 1][0]);
deg = 2 * e - 2;
fs.resize(deg + 1);
fs[0] = ModInt{1U};
for (int u = 0; u < deg; ++u) fs[u + 1] = mul(fs[u], mul(invFac1, range(u, p - 1)));
valFac.resize(deg + 1);
invFac.resize(deg + 1);
valFac[0] = 0;
invFac[0] = ModInt{1U};
ModInt prod{1U};
for (int i = 1; i <= deg; ++i) {
int ii = i, val = 0;
for (; ii % p == 0; ii /= p, ++val) {}
valFac[i] = valFac[i - 1] + val;
prod = mul(prod, invFac[i - 1] = mod(ii));
}
invFac[deg] = inv(prod);
for (int i = deg; i >= 1; --i) invFac[i - 1] = mul(invFac[i - 1], invFac[i]);
}
// v_p(n!)
long long valuationFac(long long n) const {
long long ret = 0;
for (; n /= p; ret += n) {}
return ret;
}
// (u p + 1) ... (u p + v)
ModInt range(int u, int v) const {
const ModInt up = mod(static_cast<long long>(u) * p);
ModInt ret{0U};
for (int k = min(p, e); --k >= 0; ) ret = add(mul(ret, up), s1[v][k]);
return ret;
}
// (\prod[1<=i<=up+v, p!|i] i) / (p-1)!^u
ModInt facSkipped(int u, int v) const {
const ModInt tail = range(u, v);
if (u <= deg) return mul(fs[u], tail);
vector<pair<int, ModInt>> works(deg + 1);
vector<int> valSuf(deg + 2);
vector<ModInt> prodSuf(deg + 2);
valSuf[deg + 1] = 0;
prodSuf[deg + 1] = ModInt{1U};
for (int i = deg; i >= 0; --i) {
int uu = u - i;
int val = 0;
for (; uu % p == 0; uu /= p) ++val;
works[i] = make_pair(val, mod(uu));
valSuf[i] = val + valSuf[i + 1];
prodSuf[i] = mul(works[i].second, prodSuf[i + 1]);
}
int valPre = 0;
ModInt prodPre = ModInt{1U};
ModInt sum = ModInt{0U};
for (int i = 0; i <= deg; ++i) {
// (u-0)...(u-(i-1))(u-(i+1))...(u-deg) / (i-0)...(i-(i-1))(i-(i+1))...(i-deg)
const int val = valPre + valSuf[i + 1] - valFac[i] - valFac[deg - i];
if (val < e) {
sum = add(sum, mul(mul(
mul(ModInt{pp[val]}, mul(prodPre, prodSuf[i + 1])),
mul(invFac[i], mul(invFac[deg - i], mod(((deg - i) & 1) ? -1 : +1)))
), fs[i]));
}
valPre += works[i].first;
prodPre = mul(prodPre, works[i].second);
}
return mul(sum, tail);
}
// n! / p^(v_p(n!))
ModInt facP(long long n) const {
long long sum = 0;
ModInt prod{1U};
for (; n; ) {
const long long q = n / p, v = n % p;
const long long u = q % period;
sum += u;
prod = mul(prod, facSkipped(u, v));
n = q;
}
return mul(pow(s1[p - 1][0], sum % period), prod);
}
// binom(n, k)
ModInt operator()(long long n, long long k) const {
if (n < 0) {
if (k >= 0) {
return mul(mod((k & 1) ? -1 : +1), (*this)(-n + k - 1, k));
} else if (n - k >= 0) {
return mul(mod(((n - k) & 1) ? -1 : +1), (*this)(-k - 1, n - k));
} else {
return ModInt{0U};
}
} else {
if (0 <= k && k <= n) {
const long long val = valuationFac(n) - valuationFac(k) - valuationFac(n - k);
if (val >= e) return ModInt{0U};
return mul(ModInt{pp[val]}, mul(facP(n), inv(mul(facP(k), facP(n - k)))));
} else {
return ModInt{0U};
}
}
}
} binom;
int N, M, K;
Int L;
vector<int> W;
vector<Int> V;
vector<int> A, B, C;
vector<vector<int>> G;
namespace brute {
ModInt run() {
vector<int> adj(N, 0);
for (int i = 0; i < N - 1; ++i) {
adj[A[i]] |= 1 << B[i];
adj[B[i]] |= 1 << A[i];
}
vector<int> WSum(1 << N, 0);
vector<Int> VSum(1 << N, 0);
vector<int> es(1 << N, 0);
for (int u = 0; u < N; ++u) for (int p = 0; p < 1 << u; ++p) {
WSum[p | 1 << u] = WSum[p] + W[u];
VSum[p | 1 << u] = VSum[p] + V[u];
es[p | 1 << u] = es[p] + __builtin_popcount(p & adj[u]);
}
Int maxV = -1;
vector<int> ps;
for (int p = 0; p < 1 << N; ++p) if (WSum[p] <= M && es[p] == __builtin_popcount(p) - 1) {
if (chmax(maxV, VSum[p])) ps.clear();
if (maxV == VSum[p]) ps.push_back(p);
}
cerr<<"ps = "<<ps<<endl;
vector<vector<int>> dist(N, vector<int>(N, 1001001001));
for (int u = 0; u < N; ++u) dist[u][u] = 0;
for (int i = 0; i < N - 1; ++i) dist[A[i]][B[i]] = dist[B[i]][A[i]] = C[i];
for (int w = 0; w < N; ++w) for (int u = 0; u < N; ++u) for (int v = 0; v < N; ++v) chmin(dist[u][v], dist[u][w] + dist[w][v]);
ModInt ans = 0;
for (int u = 0; u < N; ++u) {
Int way = 0;
for (const int p : ps) if (p >> u & 1) {
bool ok = true;
for (int v = 0; v < N; ++v) if (p >> v & 1) {
ok = ok && (dist[u][v] * V[v] <= L);
}
if (ok) ++way;
}
cerr<<COLOR("91")<<u<<": "<<way<<COLOR()<<endl;
ans += binom(way, K);
}
for (int i = 0; i < N - 1; ++i) {
Int way = 0;
for (const int p : ps) if ((p >> A[i] & 1) && (p >> B[i] & 1)) {
bool ok = true;
for (int v = 0; v < N; ++v) if (p >> v & 1) {
ok = ok && (max(dist[A[i]][v], dist[B[i]][v]) * V[v] <= L);
}
if (ok) ++way;
}
cerr<<COLOR("94")<<A[i]<<" "<<B[i]<<": "<<way<<COLOR()<<endl;
ans -= binom(way, K);
}
return ans;
}
} // brute
namespace sub3 {
ModInt run() {
Int maxV = -1;
if (M == 1) {
vector<int> us;
for (int u = 0; u < N; ++u) {
if (chmax(maxV, V[u])) us.clear();
if (maxV == V[u]) us.push_back(u);
}
return (int)us.size() * binom(1, K);
}
vector<int> is;
for (int i = 0; i < N - 1; ++i) if (W[A[i]] + W[B[i]] <= M) {
if (chmax(maxV, V[A[i]] + V[B[i]])) is.clear();
if (maxV == V[A[i]] + V[B[i]]) is.push_back(i);
}
cerr<<"is = "<<is<<endl;
ModInt ans = 0;
for (int u = 0; u < N; ++u) {
Int way = 0;
for (const int i : is) if (A[i] == u || B[i] == u) {
++way;
}
cerr<<COLOR("91")<<u<<": "<<way<<COLOR()<<endl;
ans += binom(way, K);
}
for (const int i : is) {
Int way = 0;
if (C[i] * max(V[A[i]], V[B[i]]) <= L) ++way;
cerr<<COLOR("94")<<A[i]<<" "<<B[i]<<": "<<way<<COLOR()<<endl;
ans -= binom(way, K);
}
return ans;
}
} // brute
int main() {
for (; ~scanf("%d%d%d%lld", &N, &M, &K, &L); ) {
W.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%d", &W[u]);
chmin(W[u], M + 1);
}
V.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%lld", &V[u]);
}
A.resize(N - 1);
B.resize(N - 1);
C.resize(N - 1);
for (int i = 0; i < N - 1; ++i) {
scanf("%d%d%d", &A[i], &B[i], &C[i]);
--A[i];
--B[i];
}
G.assign(N, {});
for (int i = 0; i < N - 1; ++i) {
G[A[i]].push_back(i);
G[B[i]].push_back(i);
}
ModInt ans = 0;
if (M <= 2 && W == vector<int>(N, 1)) {
ans = sub3::run();
} else {
ans = brute::run();
}
printf("%llu\n", ans.x);
}
return 0;
}
詳細信息
Subtask #1:
score: 13
Accepted
Test #1:
score: 13
Accepted
time: 2ms
memory: 4296kb
input:
16 109 1 4025082 46 68 46 1 46 67 111 1 156 1 45 45 1 45 45 45 8525 12789 8526 0 8526 12788 954 0 6 0 8525 8526 0 8525 8526 8526 1 2 290 1 3 188 1 4 420 1 5 6 2 6 29 1 7 643 1 8 461 4 9 468 1 10 228 5 11 428 2 12 71 4 13 290 1 14 957 2 15 955 4 16 549
output:
25
result:
ok 1 number(s): "25"
Test #2:
score: 0
Accepted
time: 0ms
memory: 5412kb
input:
17 144 2 3550388 2 1 3 1 60 88 2 2 59 60 88 2 1 2 1 89 60 0 0 0 0 6962 10443 0 0 6962 6962 10442 0 0 0 0 10443 6962 1 2 25 1 3 715 1 4 337 1 5 267 1 6 146 1 7 634 5 8 208 1 9 562 1 10 134 2 11 984 2 12 891 3 13 330 5 14 854 3 15 961 3 16 679 5 17 388
output:
116886
result:
ok 1 number(s): "116886"
Test #3:
score: 0
Accepted
time: 0ms
memory: 5212kb
input:
17 131 6 4918336 68 67 1 46 134 45 46 1 45 1 45 67 1 1 1 1 45 10569 10568 0 7046 9079 7046 7046 0 7046 0 7045 10569 0 0 0 0 7045 1 2 357 1 3 219 2 4 379 1 5 683 1 6 772 1 7 125 1 8 297 1 9 912 3 10 438 5 11 319 2 12 850 1 13 280 2 14 925 1 15 20 1 16 412 1 17 718
output:
12271512
result:
ok 1 number(s): "12271512"
Test #4:
score: 0
Accepted
time: 0ms
memory: 4256kb
input:
16 51 4 497 7 2 6 8 9 1 5 9 4 6 8 7 4 9 6 3 40 16 48 56 72 8 40 56 16 32 48 56 32 56 32 8 1 2 5 2 3 2 1 4 3 1 5 3 4 6 5 3 7 3 2 8 3 3 9 5 1 10 1 5 11 4 5 12 1 6 13 4 7 14 5 3 15 5 1 16 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3804kb
input:
15 11 1 214 2 2 1 2 2 1 3 2 3 2 3 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 2 3 2 3 5 1 4 2 2 5 5 2 6 4 2 7 1 2 8 4 1 9 1 5 10 1 1 11 2 1 12 1 1 13 4 1 14 2 2 15 3
output:
95
result:
ok 1 number(s): "95"
Test #6:
score: 0
Accepted
time: 2ms
memory: 5356kb
input:
17 15 4 609 1 2 3 1 2 2 3 2 2 1 3 3 3 2 3 2 1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 1 2 4 1 3 1 3 4 1 1 5 2 1 6 1 6 7 2 1 8 2 2 9 4 6 10 5 1 11 5 7 12 5 3 13 4 9 14 2 4 15 3 1 16 5 8 17 3
output:
35
result:
ok 1 number(s): "35"
Test #7:
score: 0
Accepted
time: 2ms
memory: 5268kb
input:
17 46 3 343 5 8 6 4 8 2 7 3 3 5 3 3 8 4 6 8 2 28 49 35 21 49 14 49 21 14 28 7 21 49 28 28 42 14 1 2 1 1 3 5 2 4 4 1 5 2 5 6 3 3 7 2 2 8 1 1 9 3 4 10 4 1 11 1 9 12 1 2 13 4 9 14 3 2 15 1 10 16 3 1 17 4
output:
56
result:
ok 1 number(s): "56"
Test #8:
score: 0
Accepted
time: 2ms
memory: 5280kb
input:
17 61 2 160 3 8 4 6 8 3 4 1 10 7 10 4 11 10 3 2 5 3 22 12 12 22 9 12 3 32 16 32 12 28 28 3 6 12 1 2 3 1 3 3 1 4 3 1 5 3 2 6 5 2 7 1 1 8 4 1 9 2 1 10 1 8 11 5 2 12 4 1 13 5 5 14 2 2 15 5 9 16 1 4 17 4
output:
0
result:
ok 1 number(s): "0"
Test #9:
score: 0
Accepted
time: 0ms
memory: 5224kb
input:
17 131 2 4260513 67 2 45 68 2 2 45 1 111 1 45 111 45 45 1 133 67 9141 0 6094 9141 0 0 6094 0 4805 0 6093 3025 6093 6094 0 8617 9141 1 2 962 1 3 31 2 4 347 1 5 351 2 6 799 1 7 486 1 8 763 2 9 538 1 10 263 6 11 311 1 12 779 1 13 987 1 14 118 3 15 933 3 16 668 3 17 677
output:
4561
result:
ok 1 number(s): "4561"
Test #10:
score: 0
Accepted
time: 1ms
memory: 4228kb
input:
16 124 4 3205604 177 1 52 152 1 127 77 2 51 76 2 1 52 1 76 1 6773 0 7470 10266 0 2242 11205 0 7470 11205 0 0 7470 0 11205 0 1 2 800 2 3 684 1 4 961 1 5 190 2 6 653 1 7 834 2 8 378 2 9 985 2 10 158 2 11 380 1 12 304 5 13 419 1 14 818 5 15 56 4 16 466
output:
0
result:
ok 1 number(s): "0"
Test #11:
score: 0
Accepted
time: 2ms
memory: 5264kb
input:
17 124 7 2772308 2 77 1 2 2 126 2 52 76 51 51 52 1 51 1 51 1 0 8264 0 0 0 3576 0 5510 8265 5510 5510 5510 0 5509 0 5509 0 1 2 714 1 3 913 1 4 884 1 5 821 1 6 145 2 7 749 4 8 645 2 9 79 1 10 945 1 11 130 2 12 817 1 13 311 1 14 808 2 15 351 2 16 278 13 17 430
output:
0
result:
ok 1 number(s): "0"
Test #12:
score: 0
Accepted
time: 2ms
memory: 4228kb
input:
16 124 4 6533568 2 2 1 51 76 1 152 77 51 2 1 2 1 52 76 77 0 0 0 7977 11967 0 5499 11967 7978 0 0 0 0 7978 11967 11967 1 2 691 1 3 957 1 4 858 3 5 318 1 6 3 1 7 155 3 8 888 1 9 32 1 10 434 2 11 609 2 12 591 2 13 784 4 14 87 1 15 558 2 16 168
output:
1088430
result:
ok 1 number(s): "1088430"
Subtask #2:
score: 0
Runtime Error
Test #13:
score: 0
Runtime Error
input:
40 816 1 285 46 124 125 137 90 33 15 73 67 41 134 106 3 163 152 151 14 77 157 82 40 9 151 148 60 60 163 71 40 134 152 145 70 59 26 64 94 38 158 57 2 2 1 1 1 1 3 3 1 1 3 3 1 3 1 3 2 1 3 3 1 1 2 2 3 2 1 2 1 2 2 1 3 3 3 1 1 1 3 3 1 2 20 1 3 26 2 4 14 3 5 25 1 6 17 4 7 49 1 8 37 1 9 50 2 10 52 4 11 55 2...
output:
result:
Subtask #3:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 2ms
memory: 4084kb
input:
60 2 14 266401688520 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 712303980 712303980 712303979 712303980 712303980 712303980 712303980 712303980 712303980 712303980 712303980 712303980 712303980 712303980 712303980 712303980...
output:
675248872536
result:
wrong answer 1st numbers differ - expected: '120', found: '675248872536'
Subtask #4:
score: 0
Runtime Error
Dependency #1:
100%
Accepted
Test #20:
score: 0
Runtime Error
input:
38 824 297026215 792467536225 43 44 43 43 44 43 42 44 44 43 44 41 42 42 42 41 42 41 43 42 43 43 44 42 41 42 44 41 41 42 43 41 44 42 41 41 43 41 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757275 695757...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #2:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%