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

Copy
6 2
2 3 6 5 4 10
output

Copy
3
Note

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

题意:给你n个数,找出在这n个数中最多有多少个x使得所选出的数中比x大的数不是x的k倍

注意:先选择所有的数,若x的k倍在原序列存在,则看x的k倍的k倍是否存在,若存在则删去x的k倍,因为这样就可以选x和x的k倍的k倍,若x的k倍的k倍不存在,则删x的k倍

排序后二分查找,k和a[i]最大是1e9,所以要注意k*k*a[i]是否小于1e18,为防止溢出可用if(k*a[i]<=(1e18)/k)

自己的测试数据:

2 1000000000
1000000000 1000000000

4 2
2 3 4 8

5 1
1 2 3 4 5

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=1e5+;
const ll inf=1e18; int n,k,mid;
ll a[amn]; ll fd(ll b){
int l=,r=n;
mid=(l+r)/;
while(l<r){
if(a[mid]<b)l=mid+;
else if(a[mid]>b) r=mid-;
else break;
mid=(l+r)/;
}
return a[mid];
} int main(){
ios::sync_with_stdio();
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>a[i];
}
sort(a+,a++n);
int ans=n,pos,jg,jg1;
if(k!=)
for(int i=;i<n;i++){
if(!a[i])continue;
jg=fd(k*a[i]);
if(k*a[i]==jg){
pos=mid;
if(k*a[i]<=inf/k){
jg1=fd(k*k*a[i]);
if(k*k*a[i]==jg1){
a[pos]=;
}
else a[i]=;
}
else a[i]=;
ans--;
}
}
printf("%d\n",ans);
}

[二分]codeforces 274A k-Multiple Free Set的更多相关文章

  1. 模板—算法—整体二分(区间k小值)

    模板—算法—整体二分(区间k小值) Code: #include <cstdio> #include <algorithm> using namespace std; #def ...

  2. 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)

    M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...

  3. hihoCoder 1133 二分·二分查找之k小数(TOP K算法)

    #1133 : 二分·二分查找之k小数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回里我们知道Nettle在玩<艦これ>,Nettle的镇守府有很 ...

  4. hiho week 37 P1 : 二分·二分查找之k小数

    P1 : 二分·二分查找之k小数 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回里我们知道Nettle在玩&l ...

  5. codeforces 1269E K Integers (二分+树状数组)

    链接:https://codeforces.com/contest/1269/problem/E 题意:给一个序列P1,P2,P3,P4....Pi,每次可以交换两个相邻的元素,执行最小次数的交换移动 ...

  6. POJ 3294 Life Forms 后缀数组+二分 求至少k个字符串中包含的最长子串

    Life Forms   Description You may have wondered why most extraterrestrial life forms resemble humans, ...

  7. POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串

                                                                              Life Forms Time Limit: 500 ...

  8. Codeforces gym102152 K.Subarrays OR

    传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...

  9. POJ 3579 3685(二分-查找第k大的值)

    POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...

随机推荐

  1. 算法小练#1 - Dany Yang

    开始记录每周做过的算法题,这是第一周,新的开始 1021. 删除最外层的括号 题目要求如下: 有效括号字符串为空 ("")."(" + A + ")& ...

  2. 图示JVM工作原理

    JDK,JRE,JVM的联系是啥? JVM Java Virtual Machine JDK Java Development Kit JRE Java Runtime Environment 看上图 ...

  3. Python——2list和tuple类型

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  4. sql -- update表子查询、多条件判断case when

    表结构: 需求 思路: 求出平均数 select avg(user_total) as avg from user_level 更新他的等级 update user_level set user_ra ...

  5. java反序列化-ysoserial-调试分析总结篇(4)

    1.前言 这篇文章继续分析commoncollections4利用链,这篇文章是对cc2的改造,和cc3一样,cc3是对cc1的改造,cc4则是对cc2的改造,里面chained的invoke变成了i ...

  6. 从头认识js-DOM1

    前面说过一个完整的js实现,包括ECMAScript,BOM,DOM三部分,现在就来讲讲DOM的有关知识. DOM(文档对象模型)是针对HTML和XML文档的一个API(应用程序接口).DOM描绘来一 ...

  7. node--静态文件托管,路由,模板引擎

    1.路由 路由是由一个URI和一个特定的HTTP方法(GET/POST)组成的 涉及到应用如何响应客户端对某个网站节点的访问 2.ejs 3.get/post 1)get获取数据 通过Url类中的qu ...

  8. 数据加密标准(DES)详解

    1 简介 1.1 历史 DES(Data Encryption Standard)是由IBM公司在1974年提出的加密算法,在1977年被NIST定位数据加密标准.随后的很多年里,DES都是最流行的对 ...

  9. bootstrapValidator验证的remote中data属性里获取select一直是默认值

    budgetEditionNo:{ message:'版本号输入不正确' , validators:{ notEmpty:{ message:'版本号不能为空,请填写' } , remote:{ ur ...

  10. 在云服务搭建jupyter环境

    一.环境配置 centeos7 python3 二.安装jupyter notebook 1.安装jupyter 安装Jupyter Notebook 通过pip安装Jupyter Notebook ...