码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3335 Divisibility(DLX可重复覆盖)

时间:2014-12-25 23:41:56      阅读:443      评论:0      收藏:0      [点我收藏+]

标签:

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.
 

题意:选出尽可能多的数,两个数(x,y)之间满足一个整除另一个即可。
DLX做法:满足条件的我们bool 矩阵取值为1,取最大可重复覆盖即可解决。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn = 1000+5;
const int maxnnode=maxn*maxn;
const int mod = 1000000007;
struct DLX{
    int n,m,size;
    int U[maxnnode],D[maxnnode],L[maxnnode],R[maxnnode],Row[maxnnode],Col[maxnnode];
    int H[maxn],S[maxn];//H[i]位置,S[i]个数
    int ansd;
    void init(int a,int b)
    {
        n=a;  m=b;
        REPF(i,0,m)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
        }
        R[m]=0; L[0]=m;
        size=m;
        REPF(i,1,n)
           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[maxn];
    int f()
    {
        int ret = 0;
        for(int c = R[0];c != 0;c = R[c])v[c] = true;
        for(int c = R[0];c != 0;c = R[c])
            if(v[c])
            {
                ret++;
                v[c] = false;
                for(int i = D[c];i != c;i = D[i])
                    for(int j = R[i];j != i;j = R[j])
                        v[Col[j]] = false;
            }
        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 L;
LL num[maxn];
int t,n;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        L.init(n,n);
        REPF(i,1,n)  scanf("%I64d",&num[i]);
        REPF(i,1,n)
        {
            REPF(j,1,n)
              if(num[i]%num[j]==0||num[j]%num[i]==0)  L.link(i,j);
        }
        L.ansd=0;
        L.Dance(0);
        printf("%d\n",L.ansd);
    }
    return 0;
}


HDU 3335 Divisibility(DLX可重复覆盖)

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/42155203

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!