求正解+能否优化AC
查看原帖
求正解+能否优化AC
661534
Dehydration楼主2023/7/22 19:28

求正解。

下面是暴力,80分,还能优化吗?

#include<bits/stdc++.h>
using namespace std;
const int M=5e5+5; 
int n,cnt,T,a[M];
int gcd(int a,int b)
{
	return b?gcd(b,a%b):a;
}
bool J()
{
	for(int i=1;i<=n;i++)
	for(int j=i+1;j<=n;j++)
	{
		if(gcd(a[i],a[j])!=1)return false;
	}
	return true;
}
typedef int LL;
inline LL read()
{
	register LL x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	T=read();
	while(T--)
	{
		n=read();
		for(int i=1;i<=n;i++)a[i]=read();
		if(n==2)puts("Yes");else
		if(J())puts("Yes");else puts("No");
	}
} 
2023/7/22 19:28
加载中...