有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 }

方法2的90分RE代码——

 1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<iostream>
5 using namespace std;
6
7 const int N=15;
8 int n,ans;
9 int a[N],b[N][N],t[N];
10
11 int mmin(int x,int y)
12 { return x<y?x:y; }
13 int gcd(int x,int y)
14 { return (!y)?x:gcd(y,x%y); }
15 bool addit(int x,int i)
16 {
17 for (int j=1;j<=t[i];j++)
18 if (gcd(x,b[i][j])!=1) return false;
19 return true;
20 }
21 void dfs(int x,int h)
22 {
23 if (x>n) {ans=mmin(h,ans);return;}
24 for (int i=1;i<=h;i++)
25 if (addit(a[x],i))
26 {
27 b[i][++t[i]]=a[x];
28 dfs(x+1,h);
29 t[i]--;
30 }
31 b[h+1][++t[h+1]]=a[x];
32 dfs(x+1,h+1);
33 }
34 int main()
35 {
36 scanf("%d",&n);
37 for (int i=1;i<=n;i++)
38 scanf("%d",&a[i]);
39 memset(t,0,sizeof(t));
40 ans=N;
41 dfs(1,0);
42 printf("%d\n",ans);
43 return 0;
44 }

其实noi上的数据还有个问题——1应该专门放在一组中,而该题数据没有考虑到这点......

【noi 2.5_7834】分成互质组(dfs)的更多相关文章

  1. openjudge7834:分成互质组 解析报告

    7834:分成互质组 总时间限制:  1000ms 内存限制:  65536kB 描述 给定n个正整数,将它们分组,使得每组中任意两个数互质.至少要分成多少个组? 输入 第一行是一个正整数n.1 &l ...

  2. 若a与m互质,则a不影响m的完全剩余组

    [若a与m互质,则a不影响m的完全剩余组] 设t通过m的完全剩余组,若at不通过m的完全剩余组, 则会有at1=at2(mod m),即a(t1-t2)|m. 因为(a,m)=1,所以(t1-t2)| ...

  3. C互质个数

    C互质个数 Time Limit:1000MS  Memory Limit:65536K Total Submit:55 Accepted:27 Description 贝贝.妞妞和康康都长大了,如今 ...

  4. Hello Kiki(中国剩余定理——不互质的情况)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. UVA12493 - Stars(求1-N与N互质的个数)欧拉函数

    Sample Input 3 4 5 18 36 360 2147483647 Sample Output 1 1 2 3 6 48 1073741823 题目链接:https://uva.onlin ...

  6. X问题(中国剩余定理+不互质版应用)hdu1573

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. hdu 5072 两两(不)互质个数逆向+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 求n个不同的数(<=1e5)中有多少组三元组(a, b, c)两两不互质或者两两互质. 逆向求解,把 ...

  8. hdu 1573 X问题 两两可能不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  9. hdu X问题 (中国剩余定理不互质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory ...

随机推荐

  1. RabbitMQ常用的几种消息模型

    第一种模型(HelloWorld) 上图来自官方文档 P代表生产者用来生产消息,发送给消费者C,中间的共色部分代表消息队列,用来缓存消息. 首先导入依赖 <dependency> < ...

  2. rename 表名

    rename table 旧表名1 to 新表名1,旧表名2 to 新表名2;

  3. 【Docker】runtime create failed: container_linux.go:345: 解决

    ------------------------------------------------------------------------------------------------- | ...

  4. 【DBA】非常好的一个脚本网站

    今天无意间发下了一个特别好的一个oracle脚本的网站.网站地址如下: https://oracle-base.com/dba/scripts 里面都是一些非常实用的脚本.

  5. Test typora

    目录 0. test 0.5 easy test 1. problem 1 2. problem 2 3. problem 3 import numpy as np import matplotlib ...

  6. AVA编程中button按钮,actionlistener和mouseClicked区别

    在java的编程中,对于按钮button 有两个事件: 1.actionPerformed 2.mouseClicked 区别: actionPerformed:一般事件,仅侦听鼠标左键的单击事件,右 ...

  7. 如何创建一个 PostgreSQL 数据库?

    PostgreSQL 官网截图 PostgreSQL 是什么? PostgreSQL 是一个功能非常强大的,历史悠久,开源的关系数据库.PostgreSQL支持大部分的SQL标准并且提供了很多其他现代 ...

  8. 1V升3V芯片,1V升3.3V芯片,大电流的,低功耗

    一般来说,1V的电压实在很低了,即使是干电池的话,再1V时,也是基本属于没电状态了.还有一种是干电池输出电流大时,也会把干电池的电压从1.5V拉低到1V左右. 更多的是客户对于1V时要能升到3V或者3 ...

  9. 量子化学Gaussian技术实战课 2021年4月9号--12号 远程在线教学

    材料模拟分子动力学课程 3月19号--22号 远程在线课 lammps分子动力学课程 3月12号--15号 远程在线课 第一性原理VASP实战课 3月25号-28号 远程在线课 量子化学Gaussia ...

  10. Angular入门到精通系列教程(11)- 模块(NgModule),延迟加载模块

    1. 摘要 2. NgModule举例.说明 3. Angular CLI生成模块 4. 延迟加载模块 5. 总结 环境: Angular CLI: 11.0.6 Angular: 11.0.7 No ...