QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#711238 | #4781. 完美的集合 | NineSuns | 43 | 194ms | 344164kb | C++14 | 6.0kb | 2024-11-05 07:29:08 | 2024-11-05 07:29:09 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define pii pair <int, int>
#define fi first
#define se second
#define pb push_back
#define pil pair <int, ll>
#define LL __int128
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 65, S = 10005;
const ll mod = 11920928955078125, inf = 0x3f3f3f3f3f3f3f3f, imod = mod/5*4;
int n, m, c, mk[N][N], vis[N], sm[N], sk[N];
ll M, w[N], v[N], k, dis[N][N], f[N][N][S], g[S];
vector <pil> e[N];
void dfs (int rt, int p, int fa) {
for (auto i : e[p]) {
if (i.fi == fa) continue;
dis[rt][i.fi] = dis[rt][p]+i.se; dfs(rt, i.fi, p);
}
}
struct val {
ll v, c;
friend val operator * (const val & a, const val & b) {
return (val){a.v+b.v, a.c*b.c};
}
friend val operator + (const val & a, const val & b) {
if (a.v^b.v) return a.v > b.v ? a : b;
return (val){a.v, a.c+b.c};
}
void clr () {
v = -inf; c = 0;
}
}fs[N][S], gs[S];
vector <int> td;
void dp (int p, int fa) {
if (mk[p][fa]) return;
mk[p][fa] = 1;
if (w[p] <= m) f[p][fa][w[p]] = v[p];
for (auto i : e[p]) {
if (i.fi == fa) continue;
dp(i.fi, p);
memset(g, -0x3f, sizeof g);
td.clear();
for (int j = 0;j <= m;j++) if (f[i.fi][p][j] >= 0) td.pb(j);
for (int j = 0;j <= m;j++) {
if (f[p][fa][j] < 0) continue;
for (int z : td) if (j+z <= m) g[j+z] = max(g[j+z], f[i.fi][p][z]+f[p][fa][j]);
}
memcpy(f[p][fa], g, sizeof g);
}
f[p][fa][0] = 0;
}
inline bool chk (int i, int j) {
return v[j] == 0 || dis[i][j] <= M/v[j];
}
void dp1 (int p, int fa) {
for (int j = 0;j <= m;j++) fs[p][j].clr();
if (vis[p]) {
if (w[p] <= m) fs[p][w[p]] = (val){v[p], 1};
for (auto i : e[p]) {
if (i.fi == fa) continue;
dp1(i.fi, p);
td.clear();
for (int j = 0;j <= m;j++) gs[j].clr();
for (int j = 0;j <= m;j++) if (fs[i.fi][j].v >= 0) td.pb(j);
for (int j = 0;j <= m;j++) {
if (fs[p][j].v < 0) continue;
for (int z : td) if (j+z <= m) {
gs[j+z] = gs[j+z]+(fs[p][j]*fs[i.fi][z]);
}
}
memcpy(fs[p], gs, sizeof gs);
}
}
if (!sk[p]) fs[p][0] = (val){0, 1};
}
ll qpow (LL x, ll y = imod-1) {
LL res = 1;
while (y) {
if (y&1) res = res*x%mod;
x = x*x%mod; y >>= 1;
}
return res;
}
ll lC (ll n, ll m) {
if (m > n) return 0;
if (m > n-m) m = n-m;
LL res = 1;
ll c = 0, x = 1;
LL k = n; while (k) c += k/5, k /= 5;
k = m; while (k) c -= k/5, k /= 5;
k = n-m; while (k) c -= k/5, k /= 5;
if (c > 22) return 0;
while (c--) x = x*5%mod;
for (ll i = n;i > n-m;i--) {
k = i; while (k%5 == 0) k /= 5;
res = res*k%mod;
}
for (ll i = 1;i <= m;i++) {
k = i; while (k%5 == 0) k /= 5;
res = res*qpow(k)%mod;
}
return res*x%mod;
}
struct vc {
LL f[25];
void clr () { memset(f, 0, sizeof f); }
friend vc operator + (const vc & a, const ll &b) {
vc c; c.clr();
for (int i = 0;i < 25;i++) {
for (int j = 0;j <= i;j++) c.f[i-j] += a.f[i]*qpow(b, j)%mod*lC(i, j)%mod, c.f[i-j] %= mod;
}
return c;
}
friend vc operator * (const vc & a, const vc & b) {
vc c; memset(c.f, 0, sizeof c.f);
for (int i = 0;i < 25;i++) {
for (int j = 0;i+j < 25;j++) c.f[i+j] += a.f[i]*b.f[j]%mod, c.f[i+j] %= mod;
}
return c;
}
}dd;
void init () {
dd.clr();
for (int i = 0;i < 16;i++) {
ll k = 1;
for (int j = 0;j < 4;j++) if (i>>j&1) k = k*(j+1);
dd.f[4-__builtin_popcount(i)] += k;
}
}
vc gv (ll n) {
if (n == 0) return dd;
vc k = gv(n-1>>1);
vc res = k*(k+(((n-1)/2+1)*5%mod));
if (n&1^1) res = res*(dd+(n*5%mod));
return res;
}
LL sc (ll n) {
if (n < 0) return 1;
return gv(n).f[0];
}
LL fac (ll n) {
if (n == 0) return 1;
LL res = 1;
for (ll i = n/5*5+1;i <= n;i++) res = res*i%mod;
res = res*fac(n/5)%mod*sc(n/5-1)%mod;
return res;
}
ll C (ll n, ll m) {
if (m > n) return 0;
if (m > n-m) m = n-m;
LL res = 1;
ll c = 0, x = 1;
LL k = n; while (k) c += k/5, k /= 5;
k = m; while (k) c -= k/5, k /= 5;
k = n-m; while (k) c -= k/5, k /= 5;
if (c > 22) return 0;
while (c--) x = x*5%mod;
return fac(n)*qpow(fac(n-m)*fac(m)%mod)%mod*x%mod;
}
void solve () {
init();
memset(f, -0x3f, sizeof f);
cin >> n >> m >> k >> M;
for (int i = 1;i <= n;i++) cin >> w[i];
for (int i = 1;i <= n;i++) cin >> v[i];
for (int i = 1;i < n;i++) {
int x, y; ll c; cin >> x >> y >> c;
e[x].pb({y, c}); e[y].pb({x, c});
}
for (int i = 1;i <= n;i++) dfs(i, i, 0);
for (int i = 1;i <= n;i++) dp(i, 0);
ll ans = -inf, cnt = 0;
for (int i = 1;i <= n;i++) for (int j = 0;j <= m;j++) if (f[i][0][j] > ans) ans = f[i][0][j]; //, cout << "DP:" << i << " " << j << " " << f[i][0][j] << endl;
// cout << ans << endl;
for (int i = 1;i <= n;i++) {
memset(sk, 0, sizeof sk);
for (int j = 1;j <= n;j++) vis[j] = chk(i, j); //, cout << vis[j] << " "; cout << endl;
sk[i] = 1; dp1(i, 0);
ll sc = 0;
for (int j = 0;j <= m;j++) {
if (fs[i][j].v == ans) sc += fs[i][j].c;
}
cnt += C(sc, k), cnt %= mod;
}
for (int i = 1;i <= n;i++) {
for (auto j : e[i]) {
if (j.fi > i) continue;
memset(sk, 0, sizeof sk);
for (int z = 1;z <= n;z++) vis[z] = chk(i, z) && chk(j.fi, z);
sk[i] = sk[j.fi] = 1;
dp1(i, j.fi); dp1(j.fi, i);
for (int z = 0;z <= m;z++) gs[z].clr();
td.clear();
for (int z = 0;z <= m;z++) if (fs[j.fi][z].v >= 0) td.pb(z);
for (int z = 0;z <= m;z++) {
if (fs[i][z].v < 0) continue;
for (int x : td) if (z+x <= m) gs[z+x] = gs[z+x]+(fs[i][z]*fs[j.fi][x]);
}
ll sc = 0;
for (int z = 0;z <= m;z++) if (gs[z].v == ans) sc += gs[z].c;
// cout << i << " " << j.fi << " " << -sc << " " << C(sc, k) << endl;
cnt -= C(sc, k), cnt %= mod;
}
}
(cnt += mod) %= mod;
cout << cnt;
}
signed main () {
// freopen("t.out", "w", stdout);
// ios::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
int T = 1;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 13
Accepted
Test #1:
score: 13
Accepted
time: 36ms
memory: 337432kb
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: 13
Accepted
time: 139ms
memory: 338356kb
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: 13
Accepted
time: 56ms
memory: 337808kb
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: 13
Accepted
time: 15ms
memory: 338048kb
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: 13
Accepted
time: 113ms
memory: 336352kb
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: 13
Accepted
time: 20ms
memory: 338348kb
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: 13
Accepted
time: 11ms
memory: 337860kb
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: 13
Accepted
time: 16ms
memory: 336708kb
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: 13
Accepted
time: 55ms
memory: 336828kb
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: 13
Accepted
time: 4ms
memory: 336632kb
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: 13
Accepted
time: 12ms
memory: 336840kb
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: 13
Accepted
time: 31ms
memory: 338480kb
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: 11
Accepted
Test #13:
score: 11
Accepted
time: 51ms
memory: 341844kb
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:
3
result:
ok 1 number(s): "3"
Test #14:
score: 11
Accepted
time: 19ms
memory: 341444kb
input:
39 617 1 172 60 66 69 46 26 120 82 68 86 29 11 115 56 36 97 89 43 101 92 25 57 45 26 1 111 67 93 84 4 74 36 67 63 60 64 83 104 5 110 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 2 2 2 1 2 52 1 3 22 1 4 50 3 5 19 1 6 26 1 7 48 1 8 30 3 9 55 2 10 18 5 11 28 5 12 19 1 13 55 1...
output:
3
result:
ok 1 number(s): "3"
Test #15:
score: 11
Accepted
time: 135ms
memory: 343692kb
input:
59 9134 1 30156516 1838 15 2444 1837 3666 1840 2447 1835 10 3057 9 2444 13 3662 13 1836 3057 2448 3055 12 15 11 8 4271 9 12 11 3055 1835 3057 2446 2449 2446 12 3663 12 2440 8 3666 1837 1837 3057 9 2447 3057 1835 1837 2446 1838 2442 2443 2447 2449 1835 3058 11 4274 3664 2441 13038 0 17384 13037 7589 ...
output:
129
result:
ok 1 number(s): "129"
Test #16:
score: 11
Accepted
time: 194ms
memory: 344164kb
input:
58 8015 1 8074143 2517 2009 1516 2517 9 6 13 3 3517 9 10 9 9 9 2512 11 2510 2012 8 3518 9 8 3520 7 9 3517 6 5 8 9 10 6 10 9 2012 10 2514 9 3017 10 1514 9 7 2513 4 3515 9 3513 8 7 10 11 2512 5 2013 1511 9 9 20765 16611 12458 2031 0 0 0 0 9640 0 0 0 0 0 20765 0 20764 16611 0 7291 0 0 9833 0 0 13633 0 ...
output:
552960
result:
ok 1 number(s): "552960"
Subtask #3:
score: 19
Accepted
Test #17:
score: 19
Accepted
time: 15ms
memory: 343404kb
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:
120
result:
ok 1 number(s): "120"
Test #18:
score: 19
Accepted
time: 32ms
memory: 344052kb
input:
56 2 7 534719494983 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 649719921 649719921 649719920 649719921 649719920 649719921 649719921 649719921 649719921 649719921 649719921 649719921 649719921 649719921 649719921 649719921 64971992...
output:
12620256
result:
ok 1 number(s): "12620256"
Test #19:
score: 19
Accepted
time: 43ms
memory: 343676kb
input:
59 2 18 362091866924 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 542053693 542053693 542053693 542053693 542053693 542053693 542053693 542053693 542053693 542053693 542053693 542053692 542053693 542053693 542053693 542053693 5...
output:
1037158320
result:
ok 1 number(s): "1037158320"
Subtask #4:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #20:
score: 0
Time Limit Exceeded
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
Time Limit Exceeded
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #28:
score: 0
Time Limit Exceeded
input:
58 7773 3707 498735913284 267 266 265 268 268 267 267 265 266 266 266 265 267 265 267 266 266 268 266 267 269 264 265 267 265 264 265 267 266 265 266 266 264 265 266 267 265 267 265 264 265 265 265 265 267 265 266 265 268 265 268 265 263 267 267 265 265 266 562272732 562272732 562272732 562272732 56...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
0%