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!

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).

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

 

原创粉丝点击