hdu 3335 Divisibility(Dancing Links 重复覆盖)

Divisibility

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1592    Accepted Submission(s): 608


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


给n个数 取出一些数 使得这些数互相之间不能整除  求出最多的个数

转化为Dancing Links模型  n行n列的01矩阵 

i行j列为1表示 a[i] a[j]互相之间可以整除 求出最多有多少行使得每列至少有一个1

(为何是最多 现在我也没太想明白)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;


const int MAXM=1010;
const int MAXN=1010;
const int maxnode=MAXN*MAXM;

struct DLX
{
    int n,m,size;
    int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
    int H[MAXN],S[MAXM];
    int ansd;
    void init(int _n,int _m)
    {
        n=_n; m=_m;
        for(int i=0;i<=m;i++)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
        }
        R[m]=0; L[0]=m;
        size=m;
        for(int i=1;i<=n;i++) H[i]=-1;
    }
    void Link(int r,int c)
    {
        ++S[Col[++size]=c];
        Row[size]=r;
        D[size]=D[c];
        U[D[c]]=size;
        U[size]=c;
        D[c]=size;
        if(H[r]<0) H[r]=L[size]=R[size]=size;
        else
        {
            R[size]=R[H[r]];
            L[R[H[r]]]=size;
            L[size]=H[r];
            R[H[r]]=size;
        }
    }
    void remove(int c)
    {
        for(int i=D[c];i!=c;i=D[i])
            L[R[i]]=L[i], R[L[i]]=R[i];
    }
    void resume(int c)
    {
        for(int i=U[c];i!=c;i=U[i])
            L[R[i]]=R[L[i]]=i;
    }
    bool v[MAXM];
    int f()
    {
        int ret=0;
        for(int c=R[0];c!=0;c=R[c]) v[c]=1;
        for(int c=R[0];c!=0;c=R[c])
        {
            if(v[c])
            {
                ret++;
                v[c]=0;
                for(int i=D[c];i!=c;i=D[i])
                    for(int j=R[i];j!=i;j=R[j])
                        v[Col[j]]=0;
            }
        }
        return ret;
    }
    void Dance(int d)
    {
        if(d+f()<=ansd) return;
        if(R[0]==0)
        {
            if(d>ansd) ansd=d;
            return;
        }
        int c=R[0];
        for(int i=R[0];i!=0;i=R[i])
            if(S[i]<S[c])
                c=i;
        for(int i=D[c];i!=c;i=D[i])
        {
            remove(i);
            for(int j=R[i];j!=i;j=R[j]) remove(j);
            Dance(d+1);
            for(int j=L[i];j!=i;j=L[j]) resume(j);
            resume(i);
        }
    }
};
DLX dlx;
ll a[MAXN];

int main()
{
//    fread;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        dlx.init(n,n);
        for(int i=1;i<=n;i++)
        {
//            dlx.Link(i,i);
            for(int j=1;j<=n;j++)
            {
                if(a[i]%a[j]==0||a[j]%a[i]==0)
                {
                    dlx.Link(i,j);
                    dlx.Link(j,i);
                }
            }
        }
        dlx.ansd=0;
        dlx.Dance(0);
        printf("%d\n",dlx.ansd);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值