USC newweek2 H hdu 3335
来源:互联网 发布:阿沁淘宝店叫什么 编辑:程序博客网 时间:2024/05/17 02:00
Problem Description
As we know,the fzu AekdyCoin is famous of math,especially in the field of number theory.So,many people call him "the descendant of Chen Jingrun",which brings him a good reputation.
AekdyCoin also plays an important role in the ACM_DIY group,many people always ask him questions about number theory.One day,all members urged him to conduct a lesson in the group.The rookie daizhenyang is extremely weak at math,so he is delighted.
However,when AekdyCoin tells us "As we know, some numbers have interesting property. For example, any even number has the property that could be divided by 2.",daizhenyang got confused,for he don't have the concept of divisibility.He asks other people for help,first,he randomizely writes some positive integer numbers,then you have to pick some numbers from the group,the only constraint is that if you choose number a,you can't choose a number divides a or a number divided by a.(to illustrate the concept of divisibility),and you have to choose as many numbers as you can.
Poor daizhenyang does well in neither math nor programming.The responsibility comes to you!
AekdyCoin also plays an important role in the ACM_DIY group,many people always ask him questions about number theory.One day,all members urged him to conduct a lesson in the group.The rookie daizhenyang is extremely weak at math,so he is delighted.
However,when AekdyCoin tells us "As we know, some numbers have interesting property. For example, any even number has the property that could be divided by 2.",daizhenyang got confused,for he don't have the concept of divisibility.He asks other people for help,first,he randomizely writes some positive integer numbers,then you have to pick some numbers from the group,the only constraint is that if you choose number a,you can't choose a number divides a or a number divided by a.(to illustrate the concept of divisibility),and you have to choose as many numbers as you can.
Poor daizhenyang does well in neither math nor programming.The responsibility comes to you!
Input
An integer t,indicating the number of testcases,
For every case, first a number n indicating daizhenyang has writen n numbers(n<=1000),then n numbers,all in the range of (1...2^63-1).
For every case, first a number n indicating daizhenyang has writen n numbers(n<=1000),then n numbers,all in the range of (1...2^63-1).
Output
The most number you can choose.
Sample Input
131 2 3
Sample Output
2Hint:If we choose 2 and 3,one is not divisible by the other,which is the most number you can choose.
这道题方法比较巧妙:
题意为:
要求从n个数中取出最多的数,使这些数两两互相不能够整除。
方法:
先对这些数进行排序;
从最小的数开始搜,每找到一个没有被标记的数,则表示个数的 sum 加一;并且每找到一个没被标记的数,都进入一次DFS。即对从该数开始的后面的没有被标记的数进行判断,如果能被该数整除,则将数标记;
并且跳出对本来的数的深搜,转而对能被整除的数进行上述的深搜。这样一来,完全跳出深搜后,删除的数这是两两之间可以互相整除的数,也就是说,对于这些删除的数,最后要得到的数列只需要从中取一个数。
这样一来,最开始的循环中可以进行多少次深搜,那么最后的数列中就能够得到多少数。
代码实现:
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;__int64 a[1010];int flag[1010];int n;int find(int re){ flag[re]=1; for(int k=re+1;k<n;k++){ if(flag[k]==0&&a[k]%a[re]==0){ // flag[k]=1; find(k); break; } }}int main(){ int m; cin>>m; while(m--){ memset(flag,0,sizeof(flag)); cin>>n; int sum=0; for(int i=0;i<n;i++){ scanf("%I64d",&a[i]); } sort(a,a+n); for(int j=0;j<n;j++){ if(flag[j]==0){ sum++; find(j); } } cout<<sum<<endl; } return 0;}
- USC newweek2 H hdu 3335
- USC newweek2 G
- USC 20121007 组队赛 H题 矩阵乘法
- H - H HDU 1559
- USC Computer Vision
- Training@USC Greedy Tino
- training@USC 12 2
- training@USC permutation
- 讲课@USC 杂谈
- USC 1143: 显示器
- usc@1002:项目投标
- usc@1113:奖学金
- 出国体检 西安 USC
- USC oj 1404: Search
- HDU 2546 H - 饭卡
- USC 1329 Decode 坑坑坑gets
- C/C++ Summary at USC
- USC CSCI 585 Specialization Hierarchy
- Letter Grid(动规)
- GCD & LCM 一个神奇的式子
- 图 poj 1985
- 图 poj 1511
- 旋转卡壳
- USC newweek2 H hdu 3335
- USC newweek2 G
- DP 记忆化搜索 poj 1088
- poj 1743 字符串 后缀数组 不可重叠最长重复子串
- 取多次方的前n位
- vector 可变长数组 hdu 3823
- 最长公共子序列 poj1458
- 回文串 poj 1159
- DFS 剪枝1 poj 1011