Codevs 1533互斥的数-hash

题目链接:右转进入题目

题目大意:

给定n个不同的数和p,要求选择尽可能多的数,使得选择的数中,任意两个大的都不是小的p倍。

数值<=1e9。

题解:

首先排个序。然后从小到大考虑每个数字,如果当前数字没有被“排斥”,就cnt++,并把它排斥的(即比他大的且是它p倍)数排斥走。

最后cnt就是答案。

如果不用map的话辣不就是hash么。

然而并不想写双hash,一开始mod用的1e5+3,全WA

后来改为1e6+7,WA四个

后来改为1e8+3,WA一个

后来改为102030405,就AC了啊啊23333333336666666666..........

hash代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 100010
#define mod 102030405
#define ull unsigned long long int
using namespace std;
int a[MAXN];
bool hash[mod+5];
int main()
{
	int n,p;scanf("%d%d",&n,&p);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		hash[a[i]%mod]=true;
	}
	sort(a+1,a+n+1);
	int cnt=0;
	for(int i=1;i<=n;i++)
		if(hash[a[i]%mod])
		{
			cnt++;
			if(hash[(ull)a[i]*p%mod])
				hash[(ull)a[i]*p%mod]=false;
		}
	printf("%d\n",cnt);return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值