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. 编写高质量 Objective-C 代码

    第一章 熟悉 Objective-C 第一条:了解 Objective-C 起源 Objective-C 是 C 语言动态性扩充.使用"消息结构"而非"函数调用" ...

  2. jQuery学习笔记三

    使用fadeIn()js解释器会将所选元素的CSS opacity属性从0改为100,fadeTo()会动画显示所选元素,将它为改为某个特定的透明度百分比,使用fadeOut()js解释器会将所选元素 ...

  3. PS4游戏将登陆PC:一曲属于主机的悲歌

    ​​ ​ 曾经,红白机.PS游戏机等成为一代人难以磨灭的记忆.而随后的索尼PS3.微软Xbox 360.任天堂Wii U等,也称霸了次时代主机时代,成为家庭娱乐的中心.但面对着依托于PC和智能移动终端 ...

  4. 关于.net MVC中主视图和分部视图的数据共享遇到的问题

    今天在开发web时因为调用到的分部视图需要有个隐藏域.然后因为当我们第一次调用分部视图时,是用 @Html.Partial("DetailDataPart")在主视图里把它嵌进去主 ...

  5. 可视化工作流程设计开发OA系统,一两个程序员就搞定!

    随着信息化的发展,越来越多的公司老板要求实现企业审批流程化.一个公司在初期,人员少,流程简单,员工也会经常不按工作流程来走,甚至有些跨部门的工作因为关系原因,没有走工作流程就实施,导致后期出现问题或者 ...

  6. [译]HTML&CSS Lesson7: 设置背景和渐变色

    背景对网站的设计有重大的影响.它有利于建立网站的整体感觉,设置分组,分配优先级,对网站的可用性也有相当大的影响. 在CSS中,元素的背景可以是一个纯色,一张图,一个渐变色或者它们的组合.在我们决定如何 ...

  7. nx-admin1.2版本发布

    nx-admin 是一个开源的管理系统前端集成方案 github地址 nx-admin的初心 组件更易用, 让每个小白快速上手, 最大程度上帮助个人,企业节省时间成本和费用开支. 尽管这个过程不简单, ...

  8. 使用SpringMVC实现文件上传和下载

    文件上传 第一步,加入jar包: commons-fileupload-1.3.1.jar commons-io-2.4.jar 第二步,在SpringMVC配置文件中配置CommonsMultipa ...

  9. JZOJ 1774. 合并果子 (Standard IO)

    1774. 合并果子 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 在一个果园里,多多已经将所有的果子打 ...

  10. flask前端上传图片/文件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...