求助普及组月赛T3
  • 板块学术版
  • 楼主neilliu
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/7/22 17:37
  • 上次更新2023/11/3 08:13:54
查看原帖
求助普及组月赛T3
600315
neilliu楼主2023/7/22 17:37

96分,最后一个点TLE,帮我看看呗


#include<bits/stdc++.h>
using namespace std;
long long a[500005];
long long gcd(long long a,long long b){
	if(a % b == 0) return b;
	else return gcd(b,a%b);
}
void solve(){
	int n; scanf("%d",&n);
	for(int i = 1; i <= n; i++) scanf("%lld",&a[i]);
	if(n == 2){
		cout << "Yes\n";
		return ;
	}
	long long gc = a[1];
	for(int i = 2; i <= n; i++) gc = gcd(gc,a[i]);
	if(gc != 1){
		cout << "No\n";
		return ;
	}
	set<long long> s;
	for(int i = 1; i <= n; i++){
		for(long long j = 2; j * j <= a[i]; j++){
			if(a[i] < j) break;
			if(a[i] % j == 0){
				if(s.count(j) != 0){
					cout << "No\n";
					return ;
				}
				s.insert(j);
				while(a[i] % j == 0) a[i] /= j;
			} 
		}
		if(a[i] != 1){
			if(s.count(a[i]) != 0){
				printf("No\n");
				return ;
			}
			s.insert(a[i]);
		}
		//for(auto it = s.begin(); it != s.end(); it++) cout << *it << " ";
	}
	printf("Yes\n");
	return ;
}
int main()
{
	int t; cin >> t;
	while(t--) solve();
	return 0;
}
2023/7/22 17:37
加载中...