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

互斥的数(hash)

时间:2016-12-18 17:38:12      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:bsp   lap   scan   space   stream   子集   one   main   com   

1553 互斥的数

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y满足y = P*x,那么就认为x,y这两个数是互斥的,现在想知道给定的一个集合的最大子集满足两两之间不互斥。

输入描述 Input Description

输入有多组数据,每组第一行给定两个数N和P(1<=N<=10^5, 1<=P<=10^9)。接下来一行包含N个不同正整数ai(1<=ai<=10^9)。

输出描述 Output Description

输出一行表示最大的满足要求的子集的元素个数。

样例输入 Sample Input

4 2

1 2 3 4

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

 

技术分享
//双哈希 
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod1 10007
#define mod2 10009

using namespace std;
int n,tot,p;
int a[1000010],b[10100][10100];

int main()
{
    scanf("%d%d",&n,&p);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        long long x,y,hash1,hash2;
        x=a[i]%mod1;hash1=((a[i]%mod1)*p)%mod1;
        y=a[i]%mod2;hash2=((a[i]%mod2)*p)%mod2;
        if(b[x][y]==0) b[hash1][hash2]=1,tot++;
    }
    printf("%d\n",tot);
    return 0;
}


/*
贪心思想+map实现 
找出不互质的数的集合,就是把互斥的数删去,那么当有两个互斥的数时,删掉哪一个呢?删掉后面的。为什么?当删掉后面的数时,这个数前面的会入选,这个数后面的与它互斥的数也会入选,因为每个数都是不同的。 
举个例子: 
3 2 
1 2 4 
当枚举到1时 会发现1和2有冲突 
我们毫不犹豫的删去2 这样4才能也被选入 
样例2 
4 2 
1 2 4 8 
当我们枚举到1时 还是发现1和2有冲突 
还是删去2 这样4能被选入 而8必须被删去
*/ 

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=100010;
int n,m,ans,a[maxn];
map<int,int> hash;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    if(!hash[a[i]])
    {
        hash[a[i]*m]=1;
        ans++;
    }
    cout<<ans;
    return 0;
}
心若向阳,无言悲伤

 

互斥的数(hash)

标签:bsp   lap   scan   space   stream   子集   one   main   com   

原文地址:http://www.cnblogs.com/L-Memory/p/6194736.html

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