【noi 2.5_7834】分成互质组(dfs)
有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)的更多相关文章
- openjudge7834:分成互质组 解析报告
7834:分成互质组 总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个正整数,将它们分组,使得每组中任意两个数互质.至少要分成多少个组? 输入 第一行是一个正整数n.1 &l ...
- 若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)| ...
- C互质个数
C互质个数 Time Limit:1000MS Memory Limit:65536K Total Submit:55 Accepted:27 Description 贝贝.妞妞和康康都长大了,如今 ...
- Hello Kiki(中国剩余定理——不互质的情况)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- 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 ...
- X问题(中国剩余定理+不互质版应用)hdu1573
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5072 两两(不)互质个数逆向+容斥
http://acm.hdu.edu.cn/showproblem.php?pid=5072 求n个不同的数(<=1e5)中有多少组三元组(a, b, c)两两不互质或者两两互质. 逆向求解,把 ...
- hdu 1573 X问题 两两可能不互质的中国剩余定理
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...
- hdu X问题 (中国剩余定理不互质)
http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory ...
随机推荐
- redis持久化怎么选?成年人从来不做选择...
前言 面试官:你知道 redis 是的怎么做持久化的吗? 我:我知道 redis 有两种方式,一种是 RDB,一种是 AOF. 面试官:那这两种方式具体是怎么做的,它们的区别是什么,生产环境中到底应该 ...
- mysql—group_concat函数
MySQL中的group_concat函数的使用方法,比如select group_concat(name) . 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Or ...
- Electron实用技巧-开机启动时隐藏主窗口,只显示系统托盘
# 1 在桌面软件中,开机自启动是很常见的功能,在electron中也提供了很好的支持,以下是主要代码: //应用是否打包if (app.isPackaged) { //设置开机启动 app.se ...
- 1V升压5V和1.5V升压5V的集成电路芯片
1.5V和1V输入,要升压输出5V的集成电路芯片合适? 干电池标准电压是1.5V,放电电压后面在0.9V-1V左右,如果要选用干电池1.5V升压到5V的合适的芯片,需要满足低压1V或者0.9V更好的低 ...
- C++ 中assert断言函数的基本用法
在我们的实际开发过程之中,常常会出现一些隐藏得很深的BUG,或者是一些概率性发生的BUG,通常这些BUG在我们调试的过程中不会出现很明显的问题,但是如果我们将其发布,在用户的各种运行环境下,这些程序可 ...
- Redis 实战 —— 09. 实现任务队列、消息拉取和文件分发
任务队列 P133 通过将待执行任务的相关信息放入队列里面,并在之后对队列进行处理,可以推迟执行那些耗时对操作,这种将工作交给任务处理器来执行对做法被称为任务队列 (task queue) . P13 ...
- Mybatis plus 报错Invalid bound statement (not found) 终极解决办法
我产生的错误原因是写的mapper继承BaseMapper没有添加泛型: 点进去: 为了解决这个bug,网上很多人也提出了解决办法:1.检查xml文件的namespace是否正确 2.Mapper.j ...
- 扩展PE头属性说明
CRC检测的算法就是checksum 以下是DllCharacteristics的参数说明
- 陈思淼:阿里6个月重写Lazada,再造“淘宝”的技术总结
小结: 1. 所谓的中台技术,就是从 IDC,网络,机房,操作系统,中间件,数据库,算法平台,数据平台,计算平台,到业务平台,每一层都有清晰的定义和技术产品. 具体来看,首先,集团技术的分层和每层的产 ...
- # Set the asyncio reactor's event loop as global # TODO: Should we instead pass the global one into the reactor?
daphne/server.py at master · django/daphne https://github.com/django/daphne/blob/master/daphne/serve ...