Codeforces 274A:k-Multiple Free Set(初识set集合)

A. k-Multiple Free Set
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.

Examples
input
6 2
2 3 6 5 4 10
output
3
Note

In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}.


题目大意:给你一个含n个数的序列和一个数k,从中去掉一些元素,使得剩下的集合中任何两个数y都不等于kx,求集合中元素的数量的最大值。

解题思路:逐步删除集合中符合k倍关系的数。

代码如下:

#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
#define LL __int64
LL a[100010];
int main()
{
	LL n,k;
	scanf("%I64d%I64d",&n,&k);
	for(LL i=0;i<n;i++)
	{
		scanf("%I64d",&a[i]);
	}
	sort(a,a+n);//排序必不可少 
	set<LL>q;//q集合存储a集合中每个元素的k倍 
	q.clear();
	LL cnt=0;
	for(LL i=0;i<n;i++)
	{
		if(q.count(a[i])==0)//如果这个数没被放进q集合,那么他的k倍就被放进集合q中, 
		{
			q.insert(a[i]*k);
			cnt++;//每放进一个,说明留下了一个,所以+1 
		}
	}
/*
1 2 4 6 8  k=2


1  4  6  原集合


2  8  12  q集合         理解不动的话看这个例子想想过程
*/
	printf("%I64d\n",cnt);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值