CometOJ_ sixty-one joy tournament title B: Treasure center can dream big showdown!
Watching feel quite simple, start thinking about direct violence, traverse the array again found pairwise relatively prime, then the mark, but found that this does not find the maximum number, read the official solution to a problem, there was still a little something. . . .
As used herein time complexity relatively high use recursion to enumerate
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int used[20];
int a[20];
int t,n;
int ans;
int gcd(int x,int y){
return y==0?x:gcd(y,x%y);
}
void dfs(int id){
if(id==n){
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(used[i]&&used[j]&&gcd(a[i],a[j])!=1){
return ;
}
}
}
int cnt=0;
for(int i=0;i<n;i++){
if(used[i]==1){
cnt++;
}
}
if(cnt>ans){
ans=cnt;
}
return ;
}
used[id]=1;
dfs(id+1);
used[id]=0;
dfs(id+1);
}
int main(){
scanf("%d",&t);
while(t--){
memset(used,0,sizeof(used));
memset(a,0,sizeof(a));
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
ans=0;
dfs(0);
printf("%d\n",ans);
}
return 0;
}