-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.cpp
More file actions
164 lines (146 loc) · 4.46 KB
/
J.cpp
File metadata and controls
164 lines (146 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <numeric>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
// 获取数字x的质因数集合
set<int> get_prime_factors(int x) {
if (x == 1) return set<int>();
set<int> factors;
int d = 2;
while (d * d <= x) {
while (x % d == 0) {
factors.insert(d);
x /= d;
}
d++;
}
if (x > 1) {
factors.insert(x);
}
return factors;
}
// 识别等价类的辅助函数
vector<vector<int>> identify_equivalence_classes(int n) {
map<set<int>, vector<int>> classes;
for (int x = 1; x <= n; x++) {
set<int> factors = get_prime_factors(x);
classes[factors].push_back(x);
}
vector<vector<int>> result;
for (const auto& [_, nums] : classes) {
result.push_back(nums);
}
return result;
}
// 解决单个测试用例
int solve_case(int n, vector<int>& a) {
// 识别等价类
auto eq_classes = identify_equivalence_classes(n);
// 确定已固定的槽位和可用的数
map<int, int> fixed;
set<int> available;
for (int i = 1; i <= n; i++) {
available.insert(i);
}
for (int i = 0; i < n; i++) {
int slot = i + 1;
if (a[i] != 0) {
if (available.find(a[i]) == available.end()) {
return 0; // 重复赋值,不可能
}
fixed[slot] = a[i];
available.erase(a[i]);
}
}
// 预处理槽位之间的互质关系
vector<set<int>> coprime_slots(n + 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j && gcd(i, j) == 1) {
coprime_slots[i].insert(j);
}
}
}
// 预处理数与数之间的互质关系
vector<set<int>> num_coprime(n + 1);
for (int x = 1; x <= n; x++) {
for (int y = 1; y <= n; y++) {
if (gcd(x, y) == 1) {
num_coprime[x].insert(y);
}
}
}
// 确定需要分配的槽位和可用数
vector<int> slots_to_assign;
for (int s = 1; s <= n; s++) {
if (fixed.find(s) == fixed.end()) {
slots_to_assign.push_back(s);
}
}
vector<int> avail_numbers(available.begin(), available.end());
// 如果槽位数大于可用数,返回0
if (slots_to_assign.size() > avail_numbers.size()) {
return 0;
}
// 回溯法计数
int count = 0;
function<void(int, map<int, int>&, set<int>&)> backtrack =
[&](int index, map<int, int>& current_assignment, set<int>& used) {
if (index == slots_to_assign.size()) {
count = (count + 1) % MOD;
return;
}
int slot = slots_to_assign[index];
for (int num : avail_numbers) {
if (used.find(num) == used.end()) {
// 检查与已分配的互质槽位
bool valid = true;
for (const auto& [assigned_slot, assigned_num] : current_assignment) {
if (coprime_slots[slot].find(assigned_slot) != coprime_slots[slot].end()) {
if (gcd(num, assigned_num) != 1) {
valid = false;
break;
}
}
}
if (valid) {
// 选择num分配给slot
current_assignment[slot] = num;
used.insert(num);
backtrack(index + 1, current_assignment, used);
// 撤销选择
current_assignment.erase(slot);
used.erase(num);
}
}
}
};
// 初始化当前分配为固定分配
map<int, int> initial_assignment = fixed;
set<int> used_numbers;
for (const auto& [_, val] : fixed) {
used_numbers.insert(val);
}
backtrack(0, initial_assignment, used_numbers);
return count;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << solve_case(n, a) << '\n';
}
return 0;
}