CF1771C 题解
shinzAnmono · · 题解
题意:
给定一个长度为
Solution:
我们发现
使用一个 std::map<int, int>,这个 std::map 的 Key 作为质数,Value 作为这个质数的出现序列中的次数(稍后解释),循环遍历 YES,否则我们输出 NO。
对于质数,我们可以使用线性筛,可以用暴力计算
CODE:
#include<iostream>
#include<map>
#include<cmath>
std::map<int, int> dict;
const int sz = 1e5 + 10;
bool numlist[sz];
int prime[sz], cnt;
void get(int n) {
for (int i = 2; i <= n; i++) {
if (!numlist[i])
prime[++cnt] = i;
for (int j = 1; j <= cnt && i * prime[j] <= n; j++) {
numlist[i * prime[j]] = true;
if (i % prime[j] == 0)
break;
}
}
} // 线性筛函数
int main() {
std::ios::sync_with_stdio(false);
int t;
std::cin >> t;
get(static_cast<int>(std::sqrt(1e9)));
while (t--) {
dict.clear();
int n;
std::cin >> n;
while (n--) {
int x;
std::cin >> x;
for (int i = 1; i <= cnt; i++) {
if (x % prime[i] == 0) dict[prime[i]]++;
while (x % prime[i] == 0) x /= prime[i];
}
if (x > 1) dict[x]++;
}
bool flag = false;
for (auto i: dict) {
if (i.second > 1) {
std::cout << "YES\n";
flag = true;
break;
}
}
if (!flag) std::cout << "NO\n";
}
return 0;
}