A K-multiple Free set is a set of integers where there are no pair of integers where one is equal to another integer multip Lied by K. That's, there are no two integers x and y (x<y) from the set, such that Y=x K.
You ' re given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The ' The ' input contains two integers n and K (1≤n≤105,1≤k≤109). The next line contains a list of n distinct positive integers a1,a2,..., a (1≤ai≤109).
The numbers in the lines are separated by single spaces.
Output
On the ' the ' output print ' the size of the largest k-multiple free subset of {A1,A2,..., a}.
Sample Input
Input
6 2
2 3 6 5 4 10
Output
3
Hint
In the sample input one of the possible maximum 2-multiple free subsets are {4, 5, 6}.
Given a set, there is no subset of y = k*x.
Greed: From small to general election past, if selected, then his K *x can not choose, with set mark down
Code:
#include <stdio.h>
#include <string.h>
#include <set>
#include <algorithm>
using namespace std;
int N, K, I, J;
Long Long num[100005], ans;
Set<long long> Save;
int main () {
ans = 0;
scanf ("%d%d", &n, &k);
for (i = 0; i < n; i + +) {
scanf ("%lld", &num[i])
;
Sort (num, num + N);
for (i = 0; i < n; i + +) {
if (Save.count (num[i]) = = 0) {
ans + +;
Save.insert (num[i] * k);
}
printf ("%lld\n", ans);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/