QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#225594#7613. Inverse Problemucup-team1209AC ✓33164ms383852kbC++203.4kb2023-10-24 20:28:192023-10-24 20:28:21

Judging History

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

  • [2023-10-24 20:28:21]
  • 评测
  • 测评结果:AC
  • 用时:33164ms
  • 内存:383852kb
  • [2023-10-24 20:28:19]
  • 提交

answer

#include<bits/stdc++.h>
using std::cin, std::cout;

const int N = 300;
const int mod = 1e9 + 7;
std::bitset<mod> vis;
using u64 = unsigned long long;
int fac[N], inv[N], ifac[N];
int r[N];
std::vector<int> ans[N];

void output(std::vector<int> deg) {
	cout << deg.size() << '\n';
	for(int i = 0;i < (int) deg.size();++i) {
		for(int j = 0;j < (int) deg.size();++j) if(deg[j] == 1) {
			-- deg[j];
			int k = max_element(deg.begin(), deg.end()) - deg.begin();
			--deg[k];
			cout << j + 1 << ' ' << k + 1 << '\n';
		}
	}
}
std::vector<int> dp[N][N];
void clear(int n) {
	for(int i = 0;i <= n;++i)
		for(int j = 0;j <= n + n - 2;++j)
			dp[i][j].clear();
}
void ins(int x, int n, int * f) {
	for(int i = 0;i < n;++i) {
		for(int j = 0;j + (n - i) * x <= n + n - 2;++j) {
			for(int w : dp[i][j]) {
				dp[i + 1][j + x].push_back((u64) w * f[n - 1 - x] % mod);
			}
		}
	}
}
void push(int cnt, int deg, int * f, int w, auto & d, int n) {
	for(int s = 6;s >= 1;--s) {
		for(;s <= deg && cnt;) {
			auto & v = dp[cnt - 1][deg - s];
			int nxt = (u64) w * f[n - 1 - s] % mod;
			auto it = std::ranges::find(v, nxt);
			if(it == v.end()) break;
			d.push_back(s);
			w = nxt;
			cnt -= 1;
			deg -= s;
		}
	}
}
std::vector<int> qy;
int ok[N];
int main() {
#ifdef zqj
	freopen("$.in", "r", stdin);
#endif
	std::ios::sync_with_stdio(false), cin.tie(0);
	fac[0] = ifac[0] = fac[1] = ifac[1] = inv[1] = 1;
	for(int i = 2;i < N;++i) {
		inv[i] = u64(mod - mod / i) * inv[mod % i] % mod;
		fac[i] = (u64) fac[i - 1] * i % mod;
		ifac[i] = (u64) ifac[i - 1] * inv[i] % mod;
	}
	int t; cin >> t;
	for(int i = 0;i < t;++i) {
		cin >> r[i];
		if(r[i] == 1) {
			ans[i] = {0};
			ok[i] = 1;
		}
	}
	for(int n = 2;;++n) {
		dp[0][0] = {1};
		const int lim = 6;
		for(int i = 1;i <= lim;i += 1) {
			ins(i, n, ifac);
		}

		u64 val = (u64) inv[n] * inv[n - 1] % mod;
		for(int i = 0;i < n;++i) {
			val = val * ifac[n - 2] % mod;
		}
		std::vector<u64> z(t);
		for(int i = 0;i < t;++i) {
			z[i] = val * r[i] % mod;
		}
		int sdp = 0;
		for(int i = 0;i <= n;++i) {
			for(int j = 0;j <= n + n - 2;++j) {
				int rem = n - i, deg = n + n - 2 - j;
				if(dp[i][j].empty()) continue;
				if(deg < rem * (lim + 1)) continue;
				sdp += dp[i][j].size();
				for(int x : dp[i][j]) {
					vis.set(x);
				}
				int st[300] = {};
				auto dfs = [&](auto dfs, int m, int s, int min, int prod) -> void {
					if(s < min * m) return ;
					if(s > (n - 1) * m) return ;
					if(m == 1) {
						min = s;
						st[0] = min;
						s = 0;
						m = 0;
						prod = (u64) prod * fac[n - 1 - min] % mod;
					}
					if(m == 0) {
						for(int id = 0;id < t;++id) {
							if(ok[id]) continue;
							u64 q = z[id] * prod % mod;
							if(vis.test(q)) {
								ans[id] = std::vector<int>(st, st + rem);
								push(i, j, fac, q, ans[id], n);
								ok[id] = 1;
							}
						}
						return ;
					}
					for(int x = min;x <= s && x < n && x * m <= s;++x) {
						st[m - 1] = x;
						dfs(dfs, m - 1, s - x, x, (u64) prod * fac[n - 1 - x] % mod);
					}
					return ;
				};
				dfs(dfs, rem, deg, lim + 1, 1);
				for(int x : dp[i][j]) {
					vis.reset(x);
				}
			}
		}
		std::cerr << n << ' ' << sdp << '\n';
		clear(n);
		
		if(std::find(ok, ok + t, 0) == ok + t || n == 125) {
			break;
		}
	}
	for(int i = 0;i < t;++i) {
		output(ans[i]);
	}
	std::cerr << double(clock()) / CLOCKS_PER_SEC << '\n';
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 105996kb

input:

4
2
360
1
509949433

output:

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

result:

ok OK (4 test cases)

Test #2:

score: 0
Accepted
time: 20628ms
memory: 383572kb

input:

9
185396120
468170792
837583517
696626231
338497514
762842660
800028852
928391161
733524004

output:

14
11 1
12 2
13 1
14 2
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10
9 10
122
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 2
38 3
39 1
40 2
41 3
42 4
43 1
44 2
45 3
46 4
47 1
48 2
49 3
50 4
51 1
52 2
53 3
54 4
55 5
56 1
57 2
58 3
59 4
60 5
61 1
62 2
63 3
64 4
65 5
66 1
67 2
...

result:

ok OK (9 test cases)

Test #3:

score: 0
Accepted
time: 33164ms
memory: 383852kb

input:

10
338497514
733524004
447182954
415993605
453460670
50499055
648088551
986982752
907925397
315315230

output:

124
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 2
44 1
45 2
46 1
47 2
48 1
49 2
50 1
51 2
52 1
53 2
54 1
55 2
56 3
57 4
58 1
59 2
60 3
61 4
62 5
63 1
64 2
65 3
66 4
67 5
68 6
69 7
70 8
71 1
72 2
73 3
74 4
75 5
76 6
77 7
78 8
79 9
80 1
81 2
8...

result:

ok OK (10 test cases)

Test #4:

score: 0
Accepted
time: 3436ms
memory: 231924kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1
2
1 2
102
16 1
17 1
18 1
19 1
20 1
21 1
22 2
23 1
24 2
25 1
26 2
27 1
28 2
29 1
30 2
31 1
32 2
33 3
34 1
35 2
36 3
37 1
38 2
39 3
40 1
41 2
42 3
43 4
44 1
45 2
46 3
47 4
48 5
49 1
50 2
51 3
52 4
53 5
54 6
55 1
56 2
57 3
58 4
59 5
60 6
61 1
62 2
63 3
64 4
65 5
66 6
67 7
68 1
69 2
70 3
71 4
72 5
73 ...

result:

ok OK (10 test cases)

Test #5:

score: 0
Accepted
time: 1025ms
memory: 179280kb

input:

10
269199917
392009324
753889928
751355133
472639410
132096559
331333826
40414701
72847302
475706026

output:

55
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 1
51 1
52 1
53 1
54 1
55 1
84
36 1
37 1
38 1
39 2
40 3
41 4
42...

result:

ok OK (10 test cases)

Extra Test:

score: 0
Extra Test Passed