【codevs1553】互斥的数

problem

solution

codes

/*
贪心
1.找出不互质的数的集合,就是把互斥的数删去.
2.那么当有两个互斥的数时,如果删掉前面(小)的,这个数后面的与它互斥的数也会入选,所以删掉后面的更优。
3.因为每个数都是不同的。
*/
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int maxn = 1e5+10;
int n, p, a[maxn], ans;
map<int,int>ma;
int main(){
    cin>>n>>p;
    for(int i = 1; i <= n; i++)cin>>a[i];
    sort(a+1,a+n+1);
    //枚举每个数,如果当前数没有被删,那么集合元素+1,然后把与他互斥的数删了。
    for(int i = 1; i <= n; i++)
        if(!ma[a[i]]){ ma[a[i]*p]=1; ans++; }
    cout<<ans<<"\n";
    return 0;
}
posted @ 2018-06-05 21:18  gwj1139177410  阅读(196)  评论(0编辑  收藏  举报
选择