HDU 3335 Divisibility (DLX)

Divisibility

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 3335

Appoint description: 
System Crawler  (2015-04-10)

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

1
3
1 2 3

Sample Output

2

Hint:
If we choose 2 and 3,one is not divisible by the other,which is the most number you can choose.

错了好久啊,DLX理解还是不够深。移除的时候当前行不要移除,要保持联系这样才能左右移动,还加了个剪枝函数进去,若剩下的列加上已取的列小于答案值那么就不再取了。

  1 #include <iostream>
  2 #include <string>
  3 #include <cstring>
  4 #include <cstdio>
  5 using    namespace    std;
  6
  7 const    int    HEAD = 0;
  8 const    int    SIZE = 1005 * 1005;
  9
 10 int    N,ANS;
 11 int    U[SIZE],D[SIZE],L[SIZE],R[SIZE],C[1005];
 12
 13 void    ini(void);
 14 void    dancing(int);
 15 void    remove(int);
 16 void    resume(int);
 17 int    cut(void);
 18 void    debug(int);
 19 int    main(void)
 20 {
 21     int        t;
 22     long    long    s[1005];
 23
 24     //freopen("txt.txt","r",stdin);
 25     scanf("%d",&t);
 26     while(t --)
 27     {
 28         scanf("%d",&N);
 29         for(int i = 1;i <= N;i ++)
 30             scanf("%lld",&s[i]);
 31
 32         ini();
 33         int    count = N + 1;
 34         for(int i = 1;i <= N;i ++)
 35         {
 36             int    first = count;
 37             for(int j = 1;j <= N;j ++)
 38                 if(s[i] % s[j] == 0 || s[j] % s[i] == 0)
 39                 {
 40                     U[count] = U[j];
 41                     D[count] = j;
 42                     L[count] = count - 1;
 43                     R[count] = count + 1;
 44
 45                     D[U[count]] = count;
 46                     U[j] = count;
 47                     C[count] = j;
 48
 49                     count ++;
 50                 }
 51             L[first] = count - 1;
 52             R[count - 1] = first;
 53         }
 54         dancing(0);
 55         printf("%d\n",ANS);
 56     }
 57
 58     return    0;
 59 }
 60
 61
 62 void    ini(void)
 63 {
 64     ANS = 0;
 65     R[HEAD] = 1;
 66     L[HEAD] = N;
 67     for(int i = 1;i <= N;i ++)
 68     {
 69         L[i] = i - 1;
 70         R[i] = i + 1;
 71         U[i] = D[i] = C[i] = i;
 72     }
 73     R[N] = 0;
 74 }
 75
 76 void    dancing(int k)
 77 {
 78     if(k + cut() <= ANS)
 79         return    ;
 80     if(R[HEAD] == HEAD)
 81     {
 82         ANS = ANS > k ? ANS : k;
 83         return    ;
 84     }
 85
 86     int    c = R[HEAD];
 87
 88     for(int i = D[c];i != c;i = D[i])
 89     {
 90         remove(i);
 91         for(int j = R[i];j != i;j = R[j])
 92             remove(j);
 93         dancing(k + 1);
 94         for(int j = L[i];j != i;j = L[j])
 95             resume(j);
 96         resume(i);
 97     }
 98
 99     return    ;
100 }
101
102 void    remove(int c)
103 {
104     for(int i = D[c];i != c;i = D[i])
105     {
106         L[R[i]] = L[i];
107         R[L[i]] = R[i];
108     }
109 }
110
111 void    resume(int c)
112 {
113     for(int i = U[c];i != c;i = U[i])
114     {
115         L[R[i]] = i;
116         R[L[i]] = i;
117     }
118 }
119
120 void    debug(int count)
121 {
122     for(int i = 0;i <= count;i ++)
123         printf("U[%d]=%d D[%d]=%d L[%d]=%d R[%d]=%d c[%d]=%d\n",i,U[i],i,D[i],i,L[i],i,R[i],i,C[i]);
124     return    ;
125 }
126
127 int    cut(void)
128 {
129     int    sum = 0;
130     for(int i = R[HEAD];i;i = R[i])
131         sum ++;
132     return    sum;
133 }
时间: 2024-10-11 04:47:39

HDU 3335 Divisibility (DLX)的相关文章

HDU 3335 Divisibility(DLX可重复覆盖)

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 th

HDU 3335 Divisibility dancing links 重复覆盖

分析: dlx重复覆盖的巧用,重复覆盖的原理恰好符合本题的筛选方式,即选择一个数后,该数的倍数或约数可以保证在之后的搜索中不会被选择 于是修改一下启发函数,求解最大的重复覆盖即可. 其实不一定不被选择,只是选择以后,要么达不成目标,要不达到目标,也不如不选择更优 举下面的例子 3 2 3 6 答案一看就是 2 初始的dancing links的表示是这样的 2   3   6 2    1   0   1 3    0   1   1 6    1   1   1 然后肯定先选第一列进行删 删

HDU 3335 Divisibility(二分图)

Divisibility Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1714    Accepted Submission(s): 651 Problem Description As we know,the fzu AekdyCoin is famous of math,especially in the field of nu

最小路径覆盖 hdu 1151 hdu 3335

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3122    Accepted Submission(s): 2027 Problem Description Consider a town where all the streets are one-way and each street leads from on

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in

(中等) HDU 3335 , DLX+重复覆盖。

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_D

HDU 3335

http://acm.hdu.edu.cn/showproblem.php?pid=3335 题意:在给出的n个数中找出一个集合,使得其中的数互不整除,求该集合最大的元素数量 首先要对输入的数去重,输入的数是64位的,开始没用__int64坑了好久 一上来我以为是求二分图最大独立集,利用相反的关系建边(整除即连边),再用n去减,这样发现没过样例(因为拆点了,求得的最大匹配数要除以二),想了下发现这个解法的bug,用最大独立集求出来的互不整除的最多元素每两个之间关系是双向的,相互不整除,a不整除b

HDU 5046 Airport(dlx)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:n个城市修建m个机场,使得每个城市到最近进场的最大值最小. 思路:二分+dlx搜索判定. const int INF=1000000005; const int N=4444; int m; struct node { int L[N],R[N],D[N],U[N],e; int col[N]; int H[N],num[N]; int visit[N],KK; void init(in

HDU 2295.Radar (DLX重复覆盖)

2分答案+DLX判断可行 不使用的估计函数的可重复覆盖的搜索树将十分庞大 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <vector> using namespace std; #define FOR(i,A,s) for(int i = A[s]; i != s; i = A[i]) #define exp 1e-8 con