跳到正文

题解

题解:P10443 「MYOI-R3」消消乐

dongzhiyuan1007约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
获赞
1
题目
P10443

版本

  1. v1当前

P10443 「MYOI-R3」消消乐

蒟蒻的第一篇题解,大佬勿喷

题目传送门

思路

题干要求消除数列 a1a_1 到 ana_n 中的 n−2n − 2 个数,那么我们就从剩下的这两个数考虑。

而我们知道,由于被消除的数都是剩下某两个数的最大公约数,所以每次被消除的数都是较小的,剩下的两个数一定是最大的。

又因为最后一个被消除的数,即 resres ,是剩下两个数的最大公约数,所以之前被消除的一定是此三个数中某两个数的最大公约数,即 resres 。

因此只需判断其它数是否等于 resres 就可以了。

code

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
int t;
int n;
int a[N];
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+n+1);
		int res=gcd(a[n-1],a[n]);
		bool st=false;
		for(int i=1;i<=n-2;i++)
		{
			if(a[i]!=res) st=true;
		}
		if(st) cout<<"No"<<endl;
		else cout<<"Yes"<<endl;
	}
	return 0;
}
原文