QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#442141#7940. Impossible NumbersHKOI0#TL 1941ms6144kbC++142.7kb2024-06-15 09:08:332024-06-15 09:08:34

Judging History

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

  • [2024-06-15 09:08:34]
  • 评测
  • 测评结果:TL
  • 用时:1941ms
  • 内存:6144kb
  • [2024-06-15 09:08:33]
  • 提交

answer

#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define int long long
using namespace std;

vector<string> ans;

const int N = 111;
int d[N][6];
int n, k; 

string s;
int cnt[10];

typedef long long ll;
#define vi vector<int>
#define sz(a) (a).size()
struct Dinic {
	struct Edge{
		int to, rev;
		ll c, oc;
		ll flow() { return max(oc - c, 0LL); }
	};
	vi lvl, ptr, q;
	vector<vector<Edge>> adj;
	Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {};
	void addEdge(int a, int b, ll c, ll rcap = 0) {
		adj[a].push_back({b, (int)adj[b].size(), c, c});
		adj[b].push_back({a, (int)adj[a].size() - 1, rcap, rcap});
	}

	ll dfs(int v, int t, ll f) {
		if (v == t || !f) return f;
		for (int& i = ptr[v]; i < adj[v].size(); i++) {
			Edge& e = adj[v][i];
			if (lvl[e.to] == lvl[v] + 1)
				if (ll p = dfs(e.to, t, min(f, e.c))) {
					e.c -= p, adj[e.to][e.rev].c += p;
					return p;
				}
		}
		return 0;
	}
	ll tot_flow = 0;
	ll calc(int s, int t) {
		ll flow = 0; q[0] = s;
		for (int L = 30; L < 31; L++) do {
			lvl = ptr = vi(sz(q));
			int qi = 0, qe = lvl[s] = 1;
			while (qi < qe && !lvl[t]) {
				int v = q[qi++];
				for (Edge e : adj[v])
					if (!lvl[e.to] && e.c >> (30 - L))
						q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
			}
			while (ll p = dfs(s, t, LLONG_MAX)) flow += p;
		} while (lvl[t]);
		tot_flow += flow;
		return tot_flow;
	}
};

void rec(int l, int rm, Dinic& dinic){
	if (k == 0) return;
	if (rm == 0) {
		if (dinic.calc(n + 10, n + 11) != l) {
			ans.push_back(s); --k;
		}
		return;
	}
	bool might_fail = false;
	for (int dig = 0; dig < 10; dig++) {
		dinic.addEdge(n + dig, n + 11, rm);
		if (dinic.calc(n + 10, n + 11) != l) {
			might_fail = true;
		}
		dinic.addEdge(n + 10, n + dig, rm);
		dinic.tot_flow -= rm;
	}
	if (!might_fail) return;
	for (char c = (rm == l) ? '1' : '0'; c <= '9'; c++) {
		cnt[c - '0']++;
		dinic.addEdge(n + (c - '0'), n + 11, 1);
		s.push_back(c); rec(l, rm - 1, dinic);
		dinic.addEdge(n + 10, n + (c - '0'), 1);
		dinic.tot_flow -= 1;
		cnt[c - '0']--; s.pop_back();
		if (k == 0) return;
	}
}

void solve(){
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 6; j++) cin >> d[i][j];
	}
	Dinic dinic(n + 12);
	for (int i = 0; i < n; i++) {
		dinic.addEdge(n + 10, i, 1);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 6; j++) {
			dinic.addEdge(i, n + d[i][j], 1);
		}
	}
	for (int l = 1; k; l++) {
		rec(l, l, dinic);
	}
	for (auto x : ans) {
		cout << x << ' ';
	}
	cout << '\n';
}

int32_t main(){
#ifndef LOCAL
	cin.tie(0)->sync_with_stdio(false);
#endif
	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3636kb

input:

2 3
1 8 7 0 6 2
1 2 5 4 9 3

output:

33 34 35 

result:

ok single line: '33 34 35 '

Test #2:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

1 10
1 5 2 2 6 4

output:

3 7 8 9 10 11 12 13 14 15 

result:

ok single line: '3 7 8 9 10 11 12 13 14 15 '

Test #3:

score: 0
Accepted
time: 3ms
memory: 3868kb

input:

4 10
1 5 7 1 2 4
0 1 5 8 9 4
3 5 2 2 7 8
6 1 7 0 2 2

output:

33 66 99 133 166 199 233 266 299 303 

result:

ok single line: '33 66 99 133 166 199 233 266 299 303 '

Test #4:

score: 0
Accepted
time: 1ms
memory: 3652kb

input:

5 10
5 9 4 8 3 3
1 1 9 2 8 9
6 3 3 0 2 1
2 6 0 3 6 4
3 6 4 2 9 4

output:

7 17 27 37 47 55 57 67 70 71 

result:

ok single line: '7 17 27 37 47 55 57 67 70 71 '

Test #5:

score: 0
Accepted
time: 4ms
memory: 3740kb

input:

5 10
8 7 1 4 8 9
2 5 0 1 0 1
9 5 5 3 9 7
6 0 0 2 3 1
1 0 0 4 9 3

output:

66 88 166 188 222 226 262 266 288 366 

result:

ok single line: '66 88 166 188 222 226 262 266 288 366 '

Test #6:

score: 0
Accepted
time: 1ms
memory: 3804kb

input:

5 10
6 8 7 7 0 0
0 5 1 9 4 1
5 9 6 9 5 4
0 4 6 9 1 6
2 8 7 4 4 0

output:

3 13 22 23 30 31 32 33 34 35 

result:

ok single line: '3 13 22 23 30 31 32 33 34 35 '

Test #7:

score: 0
Accepted
time: 1941ms
memory: 6144kb

input:

5 1000
0 4 1 3 9 6
9 6 2 1 8 6
5 3 0 7 7 3
0 2 8 0 8 4
2 4 1 2 9 7

output:

55 155 255 333 335 353 355 455 505 515 525 533 535 545 550 551 552 553 554 555 556 557 558 559 565 575 577 585 595 655 666 755 757 775 777 855 888 955 1055 1111 1116 1119 1155 1161 1166 1169 1191 1196 1199 1255 1333 1335 1353 1355 1455 1505 1515 1525 1533 1535 1545 1550 1551 1552 1553 1554 1555 1556...

result:

ok single line: '55 155 255 333 335 353 355 455... 10053 10055 10111 10116 10119 '

Test #8:

score: -100
Time Limit Exceeded

input:

5 10000
1 4 7 5 6 0
2 3 8 4 9 0
1 2 8 8 3 0
7 9 9 7 2 9
4 7 1 9 3 6

output:


result: