QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#306883#4781. 完美的集合chenxinyang200645 141ms13324kbC++145.5kb2024-01-17 14:59:112024-01-17 14:59:12

Judging History

你现在查看的是最新测评结果

  • [2024-01-17 14:59:12]
  • 评测
  • 测评结果:45
  • 用时:141ms
  • 内存:13324kb
  • [2024-01-17 14:59:11]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
#define mod 11920928955078125
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
	if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
	if(x > y) x = y;
}

inline int popcnt(int x){
	return __builtin_popcount(x);
}

inline int ctz(int x){
	return __builtin_ctz(x);
}

int n,m;
ll K,MAX;
int W[65],Val[65];
int _u[65],_v[65],_w[65];

int cnt;
int head[65];
struct eg{
	int to,nxt,w;
}edge[125];

void make(int u,int v,int w){
	edge[++cnt].w = w;
	edge[cnt].to = v;
	edge[cnt].nxt = head[u];
	head[u] = cnt;
}

int dis[65][65];
void dfs(int u,int f,int s){
	for(int i = head[u];i;i = edge[i].nxt){
		int v = edge[i].to;
		if(v == f) continue;
		dis[s][v] = dis[s][u] + edge[i].w;
		dfs(v,u,s);
	}
}
pll dp[65][10005];
pll operator +(pll p,pll q){
	pll ret;
	ret = mkp(max(p.first,q.first),0);
	if(p.first == ret.first) ret.second += p.second;
	if(q.first == ret.first) ret.second += q.second;
	return ret;
}

int _ok[65];
void dfs(int u,int f){
	per(i,m,W[u]) dp[u][i] = mkp(dp[u][i - W[u]].first + Val[u],dp[u][i - W[u]].second);
	rep(i,0,W[u] - 1) dp[u][i] = mkp(-linf,0);

	if(!_ok[u]) rep(i,0,m) dp[u][i] = mkp(-linf,0);

	for(int i = head[u];i;i = edge[i].nxt){
		int v = edge[i].to;
		if(v == f) continue;
		rep(k,0,m) dp[v][k] = dp[u][k];
		dfs(v,u);
		rep(k,0,m) dp[u][k] = dp[u][k] + dp[v][k];
	}
}

template <class ADD>
inline void add(ADD &x,ADD y){
	x += y;
	if(x >= mod) x -= mod;
}

template <class SUB>
inline void sub(SUB &x,SUB y){
	x -= y;
	if(x < 0) x += mod;
}
#define lll __int128
ll power(ll p,ll _k = 9536743164062499ll){
	ll ans = 1;
	while(_k){
		if(_k % 2 == 1) ans = (lll)ans * p % mod;
		p = (lll)p * p % mod;
		_k /= 2;
	}
	return ans;
}

const int deg = 22;
struct poly{
	ll val[25];//0~22 次
}P[65],__;

poly operator *(poly p,poly q){
	poly c;
	fill(c.val,c.val + deg + 1,0);
	rep(i,0,deg){
		rep(j,0,deg - i) c.val[i + j] += (lll)p.val[i] * q.val[j] % mod;
	}
	rep(i,0,deg) c.val[i] %= mod;
	return c;
}

ll _tmp[25],C[25][25];
poly Delta(poly &p,ll N){//展开 (x+N)
	poly c;
	fill(c.val,c.val + deg + 1,0);

	_tmp[0] = 1;
	rep(i,1,deg) _tmp[i] = (lll)_tmp[i - 1] * N % mod;
	rep(i,0,deg){
		rep(j,0,i) c.val[j] += (lll)p.val[i] * C[i][j] % mod * _tmp[i - j] % mod;
	}
	rep(i,0,deg) c.val[i] %= mod;
	return c;
}

ll getval(poly &p,ll N){
	assert(N % 5 == 0);
	ll ans = 0,prd = 1;
	rep(i,0,deg){
		ans += (lll)prd * p.val[i] % mod;
		prd = (lll)prd * N % mod;
	}
	return ans % mod;
}

void init(){
	P[0].val[0] = 24;
	P[0].val[1] = 50;
	P[0].val[2] = 35;
	P[0].val[3] = 10;
	P[0].val[4] = 1;
	rep(k,1,60){
		P[k] = P[k - 1] * Delta(P[k - 1],5ll << (k - 1));
	}
}

ll getprd(ll N){//0~N 除了 5 倍数之外项的乘积.
	ll ret = 1;
	while(N >= 0 && N % 5 != 4){
		if(N % 5) ret = (lll)ret * N % mod;
		N--;
	}
	if(N < 0) return ret;

	N = N / 5 + 1;
	ll cur = 0;
	per(k,59,0){
		if(N >= (1ll << k)){
			ret = (lll)ret * getval(P[k],5 * cur) % mod;
			cur += 1ll << k;
			N -= 1ll << k;
		}
	}
	return ret;
}

/*ll bruteprd(ll N){
	ll ret = 1;
	rep(i,0,N) if(i % 5) ret = (lll)ret * i % mod;
	return ret;
}*/

pll Calc(ll N){//多少个因子 5,除去因子 5 之外.
//	printf("Calc %lld ",N);
	ll sum = 0,prd = 1;//,prd2 = bruteprd(N);
	while(N){
		sum += N / 5;
		prd = (lll)prd * getprd(N) % mod;
		N /= 5;
	}
//	printf("sum=%lld prd=%lld prd2=%lld\n",sum,prd,prd2);
	return mkp(sum,prd);
}

ll calc(ll N){//计算 C(N,K)
	if(N < K) return 0;
//	printf("ccalc %lld\n",N);
	pll tmp = Calc(N);
	ll sum = tmp.first,prd = tmp.second;
	tmp = Calc(K);
	sum -= tmp.first;prd = (lll)prd * power(tmp.second) % mod;
	tmp = Calc(N - K);
	sum -= tmp.first;prd = (lll)prd * power(tmp.second) % mod;
	return (lll)prd * power(5,min(sum,23ll)) % mod;
}

int main(){
//	freopen("test.in","r",stdin);
	rep(i,0,deg){
		C[i][0] = 1;
		rep(j,1,i) C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
	}
	init();
/*	ll awa;
	scanf("%lld%lld",&awa,&K);
	printf("%lld\n",calc(awa));
	return 0;*/
	scanf("%d%d%lld%lld",&n,&m,&K,&MAX);
	rep(u,1,n) scanf("%d",&W[u]);
	rep(u,1,n) scanf("%d",&Val[u]);
	rep(i,1,n - 1){
		scanf("%d%d%d",&_u[i],&_v[i],&_w[i]);
		make(_u[i],_v[i],_w[i]);make(_v[i],_u[i],_w[i]);
	}
	rep(u,1,n) dfs(u,0,u);

	ll ret = -1,ans = 0,qwq;
	rep(u,1,n){
		rep(v,1,n) _ok[v] = (1ll * dis[u][v] * Val[v] <= MAX);

		rep(k,0,m) dp[u][k] = mkp(-1,0);
		dp[u][0] = mkp(0,1);
		dfs(u,0);

		qwq = 0;
		rep(k,0,m){
			if(ret < dp[u][k].first){
				ret = dp[u][k].first;
				ans = 0;
				qwq = dp[u][k].second;
			}else if(ret == dp[u][k].first){
				qwq += dp[u][k].second;
			}
		}
		add(ans,calc(qwq));
	}

	rep(i,1,n - 1){
		rep(u,1,n) _ok[u] = (1ll * max(dis[u][_u[i]],dis[u][_v[i]]) * Val[u] <= MAX);
		rep(k,0,m) dp[_u[i]][k] = mkp(-1,0);
		dp[_u[i]][0] = mkp(0,1);
		dfs(_u[i],_v[i]);
		rep(k,0,m) dp[_v[i]][k] = dp[_u[i]][k];
		dfs(_v[i],_u[i]);

		qwq = 0;
		rep(k,0,m) if(ret == dp[_v[i]][k].first) qwq += dp[_v[i]][k].second;
		sub(ans,calc(qwq));
	}
	if(ret != -1) printf("%lld\n",ans);
	else printf("0\n");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 13
Accepted
time: 0ms
memory: 7880kb

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: 1ms
memory: 6188kb

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: 1ms
memory: 5900kb

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: 1ms
memory: 5892kb

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: 6180kb

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: 1ms
memory: 6176kb

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: 1ms
memory: 5892kb

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
Wrong Answer
time: 1ms
memory: 7800kb

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:

28

result:

wrong answer 1st numbers differ - expected: '0', found: '28'

Subtask #2:

score: 11
Accepted

Test #13:

score: 11
Accepted
time: 3ms
memory: 10356kb

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: 0
Accepted
time: 5ms
memory: 10096kb

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: 0
Accepted
time: 141ms
memory: 12980kb

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: 0
Accepted
time: 118ms
memory: 13324kb

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: 0ms
memory: 12044kb

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: 0
Accepted
time: 0ms
memory: 12296kb

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: 0
Accepted
time: 2ms
memory: 12032kb

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
Skipped

Dependency #1:

0%

Subtask #5:

score: 15
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #28:

score: 15
Accepted
time: 115ms
memory: 12716kb

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:

760088902714635

result:

ok 1 number(s): "760088902714635"

Test #29:

score: 0
Accepted
time: 121ms
memory: 12712kb

input:

58 7895 4910 653150269368
270 270 269 271 270 270 271 269 273 269 271 270 270 269 272 271 272 268 271 269 269 269 269 271 271 268 271 269 271 270 270 270 269 269 270 269 269 270 270 270 270 269 269 270 271 269 269 269 268 269 272 270 270 268 269 271 269 272
679656888 679656888 679656888 679656888 67...

output:

8160378842014935

result:

ok 1 number(s): "8160378842014935"

Test #30:

score: 0
Accepted
time: 113ms
memory: 12264kb

input:

57 7831 2796 279686596890
278 278 278 279 279 277 277 278 277 278 275 278 275 275 276 277 279 274 275 276 277 276 279 277 277 276 277 276 277 276 276 276 276 276 278 277 276 275 279 275 279 279 279 278 280 277 277 277 278 278 277 278 276 278 277 278 278
321478847 321478847 321478847 321478847 321478...

output:

4509998364070125

result:

ok 1 number(s): "4509998364070125"

Test #31:

score: 0
Accepted
time: 137ms
memory: 13100kb

input:

60 8799 6468 13989018
11 11 6 9 2212 7 7 13 10 3309 2208 12 8 9 1658 10 3858 3311 10 10 3310 10 3855 7 2207 2757 7 3863 2762 9 9 11 2210 2207 11 9 12 9 3309 7 13 6 2208 8 7 3858 2756 9 7 7 9 9 2758 3311 1664 13 12 3857 3857 10
0 0 0 0 14756 0 0 0 0 7299 14756 0 0 0 11067 0 12089 324 0 0 5330 0 3166 ...

output:

3868988691264880

result:

ok 1 number(s): "3868988691264880"

Test #32:

score: 0
Accepted
time: 137ms
memory: 13100kb

input:

60 8879 7168 12509148
14 1785 8 2380 1784 9 11 11 1782 9 9 10 8 1787 2377 12 10 13 2969 3562 9 9 4153 9 1786 8 11 2969 14 11 7 9 11 7 1788 1790 3564 2375 12 3562 9 9 2377 2969 13 11 3562 2970 2969 2965 2381 2967 2972 11 6 11 8 4156 1786 9
0 11706 0 15608 11706 0 0 0 11706 0 0 0 0 11706 15608 0 0 0 4...

output:

711105703368750

result:

ok 1 number(s): "711105703368750"

Test #33:

score: 0
Accepted
time: 116ms
memory: 12492kb

input:

56 8699 6609 16065728
10 1747 3491 1746 7 11 9 15 10 4070 3490 12 8 2331 1747 2331 2911 13 2910 17 11 1751 2330 15 3492 1749 9 2910 8 11 3488 10 2913 9 2908 7 13 2911 13 7 1755 11 2330 8 10 3489 9 2909 1751 1753 4072 2912 2909 11 4069 2910
0 11424 9794 11424 0 0 0 0 0 11155 1206 0 0 15231 11424 1523...

output:

1610532740112500

result:

ok 1 number(s): "1610532740112500"

Test #34:

score: 0
Accepted
time: 31ms
memory: 12160kb

input:

60 1000 10000 10000000
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
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1...

output:

8133992117154305

result:

ok 1 number(s): "8133992117154305"

Subtask #6:

score: 0
Skipped

Dependency #1:

0%