Time Limit: 2 second(s) | Memory Limit: 32 MB |
You will be given two sets of integers. Let's call them set A and set B. Set A contains n elements and set B contains m elements. You have to remove k1 elements from set A and k2 elements from setB so that of the remaining values no integer in set B is a multiple of any integer in set A. k1 should be in the range [0, n] and k2 in the range [0, m].
You have to find the value of (k1 + k2) such that (k1 + k2) is as low as possible. P is a multiple of Q if there is some integer K such that P = K * Q.
Suppose set A is {2, 3, 4, 5} and set B is {6, 7, 8, 9}. By removing 2 and 3 from A and 8 from B, we get the sets {4, 5} and {6, 7, 9}. Here none of the integers 6, 7 or 9 is a multiple of 4 or 5.
So for this case the answer is 3 (two from set A and one from set B).
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
The first line of each case starts with an integer n followed by n positive integers. The second line starts with m followed by m positive integers. Both n and m will be in the range [1, 100]. Each element of the two sets will fit in a 32 bit signed integer.
Output
For each case of input, print the case number and the result.
Sample Input | Output for Sample Input |
2 4 2 3 4 5 4 6 7 8 9 3 100 200 300 1 150 | Case 1: 3 Case 2: 0 |
题意:
给出两个数组a,b 求b 中有多少个数是a 中的数的倍数,并且同一个数字只能使用一次
题解:
转化为最大匹配问题,匈牙利算法模板题目......
/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
const int maxn=1005;
int link[maxn],head[maxn],vis[maxn],cnt;
int x[maxn],y[maxn];
struct node
{
int to,next;
}edge[maxn*maxn];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
memset(link,-1,sizeof(link));
memset(vis,0,sizeof(vis));
}
void add(int u,int v)
{
node tp={v,head[u]};
edge[cnt]=tp;
head[u]=cnt++;
}
int dfs(int u)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v])
{
vis[v]=1;
if(link[v]==-1||dfs(link[v]))
{
link[v]=u;
return 1;
}
}
}
return 0;
}
int match(int n)
{
int ans=0;
for(int i=0;i<n;++i)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
{
++ans;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
for(int k=1;k<=t;++k)
{
init();
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
scanf("%d",&x[i]);
}
int m;
scanf("%d",&m);
for(int i=0;i<m;++i)
{
scanf("%d",&y[i]);
}
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
if(y[j]%x[i]==0)
{
add(i,j);
}
}
}
printf("Case %d: %d\n",k,match(n+m));
}
return 0;
}