题目描述:
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, 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.
输入描述:
The first line of 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, …, an (1 ≤ ai ≤ 109).
All the numbers in the lines are separated by single spaces.
输出描述:
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, …, an}.
输入:
6 2
2 3 6 5 4 10
输出:
3
题意:
给你一个含有n个数的集合,现在给一个倍数k,问在这个集合任意两个元素之间不存在k倍关系 的子集中,子集的最大元素个数为多少
题解:
二分去找x的k倍
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100000 + 5;
int n;
ll k;
ll a[maxn];
int vis[maxn];
int main(){
while(scanf("%d%lld",&n,&k)!=EOF){
for(int i = 1; i <= n; i ++) scanf("%lld",&a[i]);
sort(a + 1,a + n + 1);
int ans = n;
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; i ++){
if(vis[i]) continue;
int l = i + 1;
int r = n;
ll tmp = a[i] * k;
while(l <= r){
//cout<<mid<<endl;
int mid = (l + r) / 2;
if(a[mid] > tmp) r = mid - 1;
else if(a[mid] == tmp){
ans --;
vis[mid] = 1;
break;
}
else l = mid + 1;
}
}
printf("%d\n",ans);
}
return 0;
}