HDU 3335 Divisibility

Divisibility

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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个整数,问最多能取几个不能相互整除的数字。

思路:

其实就是重复覆盖问题,我们可以将可以整除的看成是i行j列的1,然后用重复覆盖求出最大的覆盖。

#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int maxn = 1010;
const int maxnode = maxn * maxn + 10;
struct DLX {
    int n;
    int m;
    int size;
    int u[maxnode];
    int d[maxnode];
    int l[maxnode];
    int r[maxnode];
    int col[maxnode];
    int row[maxnode];
    int h[maxn];
    int s[maxn];
    int ans;
    void init(int n, int m) {
        n = n;
        m = m;
        ans = 0;
        for (int i = 0; i <= m; i++) {
            s[i] = 0;
            u[i] = d[i] = i;
            l[i] = i - 1;
            r[i] = i + 1;
        }
        l[0] = m;
        r[m] = 0;
        size = m;
        memset(h, -1, sizeof(h));
    }
    void link(int ro, int c) {
        size++;
        s[c]++;
        col[size] = c;
        row[size] = ro;
        d[size] = d[c];
        u[d[c]] = size;
        u[size] = c;
        d[c] = size;
        if (h[ro] < 1) h[ro] = r[size] = l[size] = size;
        else {
            r[size] = r[h[ro]];
            l[r[h[ro]]] = size;
            l[size] = h[ro];
            r[h[ro]] = size;
        }
    }
    void removen(int c) {
        for (int i = d[c]; i != c; i = d[i]) {
            l[r[i]] = l[i];
            r[l[i]] = r[i];
        }
    }
    void resumen(int c) {
        for (int i = u[c]; i != c; i = u[i]) {
            l[r[i]] = i;
            r[l[i]] = i;
        }
    }
    bool book[maxnode];
    int f() {
        int sum = 0;
        for (int i = r[0]; i != 0; i = r[i]) book[i] = true;
        for (int i = r[0]; i != 0; i = r[i]) {
            if (book[i]) {
                sum++;
                book[i] = false;
                for (int j = d[i]; j != i; j = d[j]) {
                    for (int k = r[j]; k != j; k = r[k]) {
                        book[k] = false;
                    }
                }
            }
        }
        return sum;
    }
    void dance(int step) {
        if (r[0] == 0) {
            if (step > ans) ans = step;
            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]) {
            removen(i);
            for (int j = r[i]; j != i; j = r[j]) removen(j);
            dance(step + 1);
            for (int j = l[i]; j != i; j = l[j]) resumen(j);
            resumen(i);
        }
    }
};
DLX dlx;
int main() {
    int t, n;
    ll a[maxn];
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        dlx.init(n, n);
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (a[i] % a[j] == 0 || a[j] % a[i] == 0) {
                    dlx.link(i, j);
                }
            }
        }
        dlx.dance(0);
        printf("%d\n", dlx.ans);
    }
    return 0;
}

你可能感兴趣的:(HDU)