记录,有什么玄学方式可以优化一下??我大号关!!!
#include <iostream>
#include <vector>
#include <cmath>
std::string check_coprime(int n, std::vector<int>& a) {
std::vector<int> V(1e8 + 1, 0); // 存储素数的标记数组
int flag = 0; // 表示是否存在两两互质的情况
for (int i = 0; i < n; i++) {
int x = a[i]; // 当前的数值
if (flag) {
continue;
}
// 尝试寻找x的所有质因子
for (int j = 2; j * j <= x; j++) {
if (x < j) {
break;
}
if (x % j == 0) {
if (V[j]) { // 如果当前质因子已经出现过,则存在两个数具有相同的质因子
flag = 1; // 设置flag为1,表示存在两两互质的情况
break;
}
while (x % j == 0) {
x /= j;
}
V[j]++; // 将当前质因子标记为已经出现过
}
}
// 处理大于sqrt(x)的剩余质因子
if (x > 1) {
if (V[x]) {
flag = 1;
}
V[x]++;
}
}
// 判断输出结果
if (n == 1 && a[0] == 1) {
return "Yes";
} else if (n == 1) {
return "No";
} else if (n == 2) {
return "Yes";
} else if (flag) {
return "No"; // 存在两两互质的情况,输出No
} else {
return "Yes"; // 不存在两两互质的情况,输出Yes
}
}
main() {
// 读取测试数据组数
int T;
std::cin >> T;
// 处理每组数据
for (int t = 0; t < T; t++) {
// 读取整数个数和整数列表
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
// 调用函数进行判断,并输出结果
std::string result = check_coprime(n, a);
std::cout << result << std::endl;
}
return 0;
}