有2种dfs的方法:

1.存下每个组的各个数和其质因数,每次对于新的一个数,与各组比对是否互质,再添加或不添加入该组。

2.不存质因数了,直接用gcd,更加快。P.S.然而我不知道为什么RE,若有好心人发现请教教我吧,谢谢~ :-)

下面附上方法1的AC代码——

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 using namespace std;
 7 
 8 int n,ans=12;
 9 int a[12];
10 struct node {int t;int p[3000];}
11 b[12];
12 
13 int mmin(int x,int y)
14 {   return x<y?x:y;  }
15 int com(int id,int x)
16 {
17     for (int i=1;i<=b[id].t;i++)
18       if (x%b[id].p[i]==0) return 0;
19     return 1;
20 }
21 void dfs(int id,int h)
22 {
23     int x=a[id];
24     if (id>n) {ans=mmin(ans,h); return;}
25     for (int i=1;i<=h;i++)
26       if (com(i,x))
27       {
28         int y=x,tt=b[i].t;
29         for (int j=2;j<=y;j++)//sqrt(y) wrong
30           if (y%j==0) y/=j, b[i].p[++b[i].t]=j;
31         dfs(id+1,h);
32         b[i].t=tt;
33       }
34     int y=x;
35     b[h+1].t=0;
36     for (int j=2;j<=y;j++)
37       if (y%j==0) y/=j, b[h+1].p[++b[h+1].t]=j;
38     dfs(id+1,h+1);
39 }
40 int main()
41 {
42     scanf("%d",&n);
43     for (int i=1;i<=n;i++)
44       scanf("%d",&a[i]);
45     dfs(1,0);
46     printf("%d",ans);
47     return 0;
48 }
View Code

相关文章:

  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
  • 2021-11-07
  • 2021-12-28
  • 2022-12-23
猜你喜欢
  • 2021-08-21
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
相关资源
相似解决方案