CODEFORCES PROBLEMEST 378QAQ and Mocha‘s Array(好题,很提升思维)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Mocha likes arrays, so before her departure, 378QAQ gave her an array a𝑎 consisting of n𝑛 positive integers as a gift.

Mocha thinks that a𝑎 is beautiful if there exist two numbers i𝑖 and j𝑗 (1≤i,j≤n i≠j) such that for all k𝑘 (1≤k≤n), ak𝑎𝑘 is divisible†† by either ai𝑎𝑖 or aj𝑎𝑗.

Determine whether a𝑎 is beautiful.

†† x is divisible by y if there exists an integer z such that 𝑥=𝑦⋅𝑧.

Input

Each test contains multiple test cases. The first line contains the number of test cases t𝑡 (1≤t≤500). The description of the test cases follows.

The first line of each test case contains a single integer 𝑛 (3≤n≤10^5) --- the length of the array a.

The second line of each test case contains n𝑛 integers a1,a2,...,an (1≤ai≤10^9) --- the elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 10^5.

Output

For each test case, output "Yes" if array a𝑎 is beautiful, and output "No" otherwise.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

这道题大致意思是:给定一个数组选取两个数a[i]、a[j],是否数组的任意数可以整除a[i]或a[j],如果是输出"Yes",不是就输出"No"

这道题其实没那么难也没那么简单,一眼看上去感觉非常好写,但真正上手写又会有一些问题

思路就是:找出数组的最小值,遍历数组,每个数都除以最小值,如果能整除跳过,不能就放到arr数组里面待定,如果arr的长度为0说明全部能整除,直接输出"Yes",不是的话就找出arr数组的最小值,然后在进行一次相同的操作,最后判断是输出"Yes"还是"No"

代码如下:

cpp 复制代码
​
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 5;
int a[MAXN];
int t,n;

int main()
{
	cin >> t;
	while(t--){
		cin >> n;
		int flag = 0;
		for(int i=1;i<=n;i++){
			if(a[i] == 1) flag = 1;
			cin >> a[i];
		}
		if(flag){
			cout << "Yes" << endl;
			continue;
		}
		sort(a+1,a+n+1);
		int k = a[1];
		vector<int> arr;
		for(int i=2;i<=n;i++){
			if(a[i] % k != 0) arr.push_back(a[i]);
		}
		if(int(arr.size()) == 0) cout << "Yes" << endl;
		else{
			int fl = 1,g = arr[0];
			for(int i=1;i<int(arr.size());i++){
				if(arr[i] % g != 0) fl = 0;
			}
			if(fl) cout << "Yes" << endl;
			else cout << "No" << endl;
		}
	}
	return 0;
}

​

加油

相关推荐
尘诞辰6 分钟前
【C语言】数据在内存中的储存
c语言·开发语言·数据结构·c++
图学习小组6 分钟前
PaCon:一种用于识别编程提交中问题求解策略的符号分析方法
人工智能·算法·机器学习
无敌最俊朗@8 分钟前
STL-关联容器(面试复习4)
开发语言·c++
JHC0000009 分钟前
119. 杨辉三角 II
python·算法·面试
剪一朵云爱着10 分钟前
PAT 1158 Telefraud Detection
算法·pat考试
无限进步_15 分钟前
【C语言】栈(Stack)数据结构的实现与应用
c语言·开发语言·数据结构·c++·后端·visual studio
闻缺陷则喜何志丹15 分钟前
【计算几何 SAT轴】P6732 「Wdsr-2」方分|普及+
c++·数学·计算几何·sat轴·凸多边形分离
embrace9915 分钟前
【C语言学习】预处理详解
java·c语言·开发语言·数据结构·c++·学习·算法
拼好饭和她皆失17 分钟前
《二分答案算法精讲:从原理到实战(上篇)》
c++·算法
好风凭借力,送我上青云20 分钟前
Pytorch经典卷积神经网络-----激活函数篇
人工智能·pytorch·深度学习·算法·矩阵·cnn