Codevs 1533互斥的数-hash

来源:互联网 发布:肌研洗面奶怎么样 知乎 编辑:程序博客网 时间:2024/06/16 10:43

题目链接:右转进入题目

题目大意:

给定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 intusing 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;}

1 0
原创粉丝点击