题目大意
给定n个数,求至少能分成多少互质组
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <ctime>
using namespace std;
int n,a[100],b[100]={0},maxd(0),f(0),gcd[20005][20005];
int Stein(int u,int v)
{
if (u == 0) return v;
if (v == 0) return u;
// look for factors of 2
if (~u & 1) // u is even
{
if (v & 1) // v is odd
return Stein(u >> 1, v);
else // both u and v are even
return Stein(u >> 1, v >> 1) << 1;
}
if (~v & 1) // u is odd, v is even
return Stein(u, v >> 1);
// reduce larger argument
if (u > v)
return Stein((u - v) >> 1, v);
return Stein((v - u) >> 1, u);
}
int Gcd(int x,int y){
if(y==0) return x;
else return Gcd(y,x%y);
}
int Dfs(int x){
if(f) return 1;
if(x>n){
f=1;
return 1;
}else{
for(int i=1;i<=maxd;i++){
b[x]=i;
int flag=0;
for(int j=1;j<x;j++){
if(b[j]==i){
if((gcd[j][x]==1)||(gcd[x][j]==1)){
continue;
}else{
flag=1;
break;
}
}
}
if(flag==1){
continue;
}
Dfs(x+1);
}
return 0;
}
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(Stein(a[i],a[j])==1){
gcd[i][j]=1;
}else{
gcd[i][j]=-1;
}
}
}
for(maxd=1;maxd<=n;maxd++){
memset(b,0,sizeof(0));
Dfs(1);
if(f==1){
printf("%d",maxd);
break;
}
}
return 0;
}