C. Hossam and Trainees(欧拉筛+分解质因数)

题目链接:Problem -- Codeforces

题意:

给一个数量大小为n的数组a,如果这个数组中的任意两个数之间都没有大于1的公因数的话,输出”NO“,否则就输出"YES"。

数据范围:

2<=n<=1e5;

1<=ai<=1e9;

思路:

先找到1e9之间的素数,我们可以用欧拉筛来进行寻找(ps:欧拉筛一篇很详细的博客【算法/数论】欧拉筛法详解:过程详述、正确性证明、复杂度证明_seh_sjlj的博客-CSDN博客)它的思路为找到一个素数后,就将它的倍数标记为合数,也就是把它的倍数“筛掉”;如果一个数没有被比它小的素数“筛掉”,那它就是素数。后面我们就用map来记录数组中构成元素的质因数,有重复的就可以输出“YES”,没有就输出“NO”。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
const int mm = 1e6 + 60;
ll cnt = 0;
ll p[mm];//记录质数
ll st[mm];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	cin >> t;
	st[1] = 1;
	for (int i = 2; i <= 40000; i++) {
		if (st[i] == 0) p[++cnt] = i;
		for (int j = 1; j <= cnt && i * p[j] <= 40000; j++)
		{
			st[i * p[j]] = 1;
			if (i % p[j] == 0) break;
		}
	}
	while (t--)
	{
		ll n;
		cin >> n;
		ll f = 0;
		map<ll, ll>vis;
		for (ll i = 1; i <= n; i++)
		{
			ll x;
			cin >> x;
			if (f) continue;
			for (int j = 1; p[j] * p[j] <= x; j++)
			{
				if (x % p[j] == 0) {
					if (vis[p[j]]) {
						f = 1;
						break;
					}
					vis[p[j]] = 1;
					while (x % p[j] == 0) x /= p[j];
				}
			}
			if (x > 1)
			{
				if (vis[x])f = 1;
				vis[x] = 1;
			}
		}
		if (f) cout << "YES\n";
		else cout << "NO\n";
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值