QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#304064#8005. Crossing the Borderucup-team1453#ML 1486ms342852kbC++143.1kb2024-01-13 13:37:042024-01-13 13:37:04

Judging History

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

  • [2024-01-13 13:37:04]
  • 评测
  • 测评结果:ML
  • 用时:1486ms
  • 内存:342852kb
  • [2024-01-13 13:37:04]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int >
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
#define pb emplace_back
using namespace std;
const int N = 1 << 22, mod = 998244353;
const int M = 25;
int n, W;
int w[M], c[M]; 
int vis[N];
int subw[N], subc[N];
int ok[N];

int dp[N][M];
int ndp[N];
int fw[N][M];

int f[M];

ull D[M];

void Add(int &x, int y){
	x = x + y >= mod ? x + y - mod : x + y;
}
void Dec(int &x, int y){
	x = x >= y ? x - y : x - y + mod;
}

int ds[N];
int fan[N];
int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> W;
	L(i, 0, n - 1) 
		cin >> w[i] >> c[i];
//	n = 22, W = 5;
//	L(i, 0, n - 1) 
//		w[i] = 1, c[i] = 1;
	L(i, 0, n - 1) 
		L(j, i + 1, n - 1) 
			if(c[i] > c[j]) 
				swap(w[i], w[j]), swap(c[i], c[j]);
	L(p, 0, n - 1) {
		L(i, 0, (1 << p) - 1) {
			subw[i + (1 << p)] = subw[i] + w[p];
			subc[i + (1 << p)] = subc[i] + c[p];
		}
	}
	L(i, 0, (1 << n) - 1) {
		ok[i] = subw[i] <= W; 
	}
	L(i, 0, (1 << n) - 1) 
		dp[i][0] = 1;
	int hb = min(n, 18);
	R(p, n - 1, hb) {
		L(s, 0, (1 << p) - 1) {
			me(fw[s], 0);
			if(ok[s + (1 << p)]) {
				fw[s][__builtin_popcount(s) + 1] = 1;
			}
		}
		L(d, 0, p - 1) {
			for(int i = 0; i < (1 << p); i += (1 << (d + 1))) {
				L(j, i, i + (1 << d) - 1) 
					L(k, 0, p) 
						Add(fw[j + (1 << d)][k], fw[j][k]);
			}
		}
		for(int i = 0; i < (1 << n); i += (1 << (p + 1))) {
			L(j, i, i + (1 << p) - 1) {
				L(k, 0, n) 
					Dec(dp[j + (1 << p)][k], dp[j][k]);
				int pos = j - i;
				L(u, 0, n) 
					L(v, 0, n - u)
						D[u + v] += (ull) dp[j][u] * fw[pos][v];
				L(i, 0, n) dp[j][i] = D[i] % mod;
				L(i, 0, n) D[i] = 0;
			}
		}
	}
	L(p, 0, hb - 1) 
		for(int i = 0; i < (1 << n); i += (1 << (p + 1)))
			L(j, i, i + (1 << p) - 1) 
				L(k, 0, n) 
					Dec(dp[j + (1 << p)][k], dp[j][k]);
	L(i, 0, (1 << hb) - 1) 
		ds[i] = 2e9, fan[i] = 0;
	int nao = 0;
	L(i, hb, n - 1) 
		nao += c[i];
	for(int i = 0; i < (1 << n); i += (1 << hb)) {
		int v = nao - subc[i];
		for(int j = 0; j < (1 << hb); ++j) {
			int pc = __builtin_popcount(j);
			if(dp[i + j][pc + n - hb]) {
				if(v < ds[j]) {
					ds[j] = v, fan[j] = dp[i + j][pc + n - hb];
				} else if(v == ds[j]) {
					Add(fan[j], dp[i + j][pc + n - hb]);
				}
			}
		}
	}
	R(p, hb - 1, 0) {
		L(i, 0, (1 << p) - 1) {
			int s = (1 << p) - i - 1;
			for(int cur = s; ; cur = (cur - 1) & s) if(ok[cur + (1 << p)]) {
				int go = i + cur + (1 << p);
				if(ds[go] > ds[i] + c[p]) {
					ds[go] = ds[i] + c[p];
					fan[go] = fan[i];
				} else if(ds[go] == ds[i] + c[p]) {
					Add(fan[go], fan[i]);
				}
				if(!cur)break;
			}
		}
		L(i, 0, (1 << p) - 1) {
			fan[i] = fan[i + (1 << p)];
			ds[i] = ds[i + (1 << p)];
		}
	}
	cout << ds[0] << ' ' << fan[0] << '\n';
	return 0;
} 
/*
5 5
3 5
1 4
2 3
2 2
2 1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9640kb

input:

5 5
3 5
1 4
2 3
2 2
2 1

output:

9 4

result:

ok 2 number(s): "9 4"

Test #2:

score: 0
Accepted
time: 235ms
memory: 37332kb

input:

18 10000000
956231 904623
1692946 1796774
1081323 1170319
3218792 2542661
3183376 3037270
1869132 1442561
35436 35018
1564635 1939950
1847344 2006043
755870 899310
1671882 2057413
1369264 1338951
3132483 3504034
2056224 1825640
1840949 1562071
1514040 1405352
2300821 2421801
2466540 3004920

output:

9391997 70

result:

ok 2 number(s): "9391997 70"

Test #3:

score: 0
Accepted
time: 800ms
memory: 175704kb

input:

20 10000000
1289384 1416015
1692778 1966748
1747794 1708650
2885387 2925290
2516650 2410838
2202363 2092667
368691 407497
1897764 1902790
180541 224758
1089173 1075924
2005212 1743637
702568 566295
465783 369143
2722863 2902398
174068 150211
513930 519657
1634023 1313239
1133070 1040937
961394 11066...

output:

6331196 89

result:

ok 2 number(s): "6331196 89"

Test #4:

score: 0
Accepted
time: 1486ms
memory: 342852kb

input:

21 10000000
1432782 1230128
1693282 1456826
605524 521515
2742745 3427204
2231114 2129928
2345527 2397808
511783 521160
2041234 2313965
2323807 2603481
1232121 1410811
719508 850004
416942 495559
2180169 2579591
1580089 1786914
2317568 2292171
1514260 1143717
1348703 1495001
562052 525544
2818854 23...

output:

9336572 5

result:

ok 2 number(s): "9336572 5"

Test #5:

score: -100
Memory Limit Exceeded

input:

22 10000000
1562592 1176882
1693226 1513484
2293770 2757728
2612851 3010518
1971354 2392268
2475363 2035487
641627 684375
2171036 2181775
1544541 1633457
1361981 1060447
2277948 2792254
157192 141039
1011327 1139897
541119 577682
1538276 1451191
2423314 2061841
1088919 1154927
42526 43789
1779858 16...

output:

8019829 516

result: