Codeforces 929 div3 D

题目:D. Turtle Tenacity: Continual Mods
题目链接:https://codeforces.com/contest/1933/problem/D
算法:数论、贪心。

一开始没思路,后面看了别人的题解才搞懂的。

思路:

1.将原数组a从大到小排序后可以得到的数组b有两种情况。一种是b0!=b1,另一种则是b0=b1(下标从0开始)。对于第一种情况并结合样例可以发现在b1!=b2的情况下,数组元素依次取模后结果必然不为0的,此为恒成立。

2.对于第二中情况,若b0=b1,那么b0modb1=0,这将导致最终结果为0,所以我们只需要找到b0之后有无存在一个bi使得bimodb0!=0,有则将其放到b0之前,满足第一种情况。没有则为NO。

详细AC码:

点击查看代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N];
 
void quick_sort(int l, int r)   //快速排序算法
{
	if (l >= r) return;
	int i = l - 1, j = r + 1, x = a[(l + r) >> 1];
	while (i < j)
	{
		do i++;while (a[i] < x);
		do j--;while (a[j] > x);
		if (i < j) swap(a[i], a[j]);
	}
	quick_sort(l, j);
	quick_sort(j + 1, r);
}
int main()
{
	int t, n;
	cin >> t;
	while (t--)
	{
		cin >> n;
		for (int i = 0;i < n;i++)
		{
			cin >> a[i];
		}
		quick_sort(0, n - 1);  //排序
		
		//1.若排序后a1!=a2 取模后结果必不为0
		if (a[0] != a[1])
		{
			cout << "YES" << endl;
			continue;
		}
		else {    //2.
			int flag = -1;
			for (int i = 2;i < n;i++)
			{
				if (a[i] % a[0] != 0)
				{
					flag = 1;
					break;
				}
			}
			if (flag == 1)
			{
				cout << "YES" << endl;
			}
			else {
				cout << "NO" << endl;
			}
		}
	}

	return 0;
}
posted @ 2024-07-25 10:47  Tree907  阅读(26)  评论(0)    收藏  举报