
Divisibility
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Appoint description:
System Crawler (2015-04-10)
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!
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).
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
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理解还是不够深。移除的时候当前行不要移除,要保持联系这样才能左右移动,还加了个剪枝函数进去,若剩下的列加上已取的列小于答案值那么就不再取了。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std; const int HEAD = ;
const int SIZE = * ; int N,ANS;
int U[SIZE],D[SIZE],L[SIZE],R[SIZE],C[]; void ini(void);
void dancing(int);
void remove(int);
void resume(int);
int cut(void);
void debug(int);
int main(void)
{
int t;
long long s[]; //freopen("txt.txt","r",stdin);
scanf("%d",&t);
while(t --)
{
scanf("%d",&N);
for(int i = ;i <= N;i ++)
scanf("%lld",&s[i]); ini();
int count = N + ;
for(int i = ;i <= N;i ++)
{
int first = count;
for(int j = ;j <= N;j ++)
if(s[i] % s[j] == || s[j] % s[i] == )
{
U[count] = U[j];
D[count] = j;
L[count] = count - ;
R[count] = count + ; D[U[count]] = count;
U[j] = count;
C[count] = j; count ++;
}
L[first] = count - ;
R[count - ] = first;
}
dancing();
printf("%d\n",ANS);
} return ;
} void ini(void)
{
ANS = ;
R[HEAD] = ;
L[HEAD] = N;
for(int i = ;i <= N;i ++)
{
L[i] = i - ;
R[i] = i + ;
U[i] = D[i] = C[i] = i;
}
R[N] = ;
} void dancing(int k)
{
if(k + cut() <= ANS)
return ;
if(R[HEAD] == HEAD)
{
ANS = ANS > k ? ANS : k;
return ;
} int c = R[HEAD]; for(int i = D[c];i != c;i = D[i])
{
remove(i);
for(int j = R[i];j != i;j = R[j])
remove(j);
dancing(k + );
for(int j = L[i];j != i;j = L[j])
resume(j);
resume(i);
} return ;
} 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]] = i;
R[L[i]] = i;
}
} void debug(int count)
{
for(int i = ;i <= count;i ++)
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]);
return ;
} int cut(void)
{
int sum = ;
for(int i = R[HEAD];i;i = R[i])
sum ++;
return sum;
}