[二分]codeforces 274A k-Multiple Free Set

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

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int amn=1e5+5;
 5 const ll inf=1e18;
 6 
 7 int n,k,mid;
 8 ll a[amn];
 9 
10 ll fd(ll b){
11     int l=1,r=n;
12     mid=(l+r)/2;
13     while(l<r){
14         if(a[mid]<b)l=mid+1;
15         else if(a[mid]>b) r=mid-1;
16         else break;
17         mid=(l+r)/2;
18     }
19     return a[mid];
20 }
21 
22 int main(){
23     ios::sync_with_stdio(0);
24     cin>>n>>k;
25     for(int i=1;i<=n;i++){
26         cin>>a[i];
27     }
28     sort(a+1,a+1+n);
29     int ans=n,pos,jg,jg1;
30     if(k!=1)
31     for(int i=1;i<n;i++){
32         if(!a[i])continue;
33         jg=fd(k*a[i]);
34         if(k*a[i]==jg){
35             pos=mid;
36             if(k*a[i]<=inf/k){
37                 jg1=fd(k*k*a[i]);
38                 if(k*k*a[i]==jg1){
39                 a[pos]=0;
40                 }
41                 else a[i]=0;
42             }
43             else a[i]=0;
44             ans--;
45         }
46     }
47     printf("%d\n",ans);
48 }

 

全部评论

相关推荐

点赞 评论 收藏
分享
今天 16:40
已编辑
门头沟学院 C++
26学院本太难了,很多公司机筛就给我刷了。机会都难拿到如果是简历存在问题也欢迎拷打————————————————————分割线——————————————————————2026.3.4更新:发完贴之后,时不时投递又收到了不少的笔试/面试邀请。主要是之前投递简历出去之后基本上都是沉默状态,年后好转了不少timeline:2026.01.21&nbsp;文远知行笔试,半年多没刷算法题&nbsp;-&gt;挂&nbsp;(后续HR说春招可以重新安排笔试)2026.2.4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小鹏汇天&nbsp;技术一面,第二周收到结果&nbsp;-&gt;挂2026.2.12&nbsp;&nbsp;&nbsp;大众Cariad代招&nbsp;技术二面&nbsp;-&gt;Offer2026.2.28&nbsp;&nbsp;&nbsp;多益网络技术面试,由于风评太差,一直在犹豫要不要接面试&nbsp;-&gt;推迟-----------分割线-----------2026.3&nbsp;月前的某一天,临时去电网报名了二批计算机岗位的笔试2026.3.6&nbsp;从上家公司实习离职,氛围最好的一家公司,leader&nbsp;说可以帮忙转正,但是流程太长,而且我们部门据说只有一个&nbsp;hc,更想要研究生,我很有可能是会被签外包公司在这里干活,就离职了。2026.3.9&nbsp;入职新公司,大众Cariad&nbsp;以外部公司的身份进组,项目组签了三年,后续三年应该都可以在这里呆,不知道有没有希望原地跳槽。2026.3.10&nbsp;电网考试居然说我通过资格审查了,短信约我去参加资格审查,请假一天,买了&nbsp;12&nbsp;号晚上的机票回成都2026.3.15&nbsp;参加国家电网计算机类笔试2026.3.17&nbsp;电网出成绩了,感觉很低。觉得已经🈚️了2026.3.18&nbsp;收到电网面试通知,通知&nbsp;3.22-3.25&nbsp;这个时间去面试,我的岗位只招&nbsp;1&nbsp;个人。据说面试只有&nbsp;2-3&nbsp;人,不知道能不能成功----------分割线-----------2026.3.21&nbsp;电网面试结束,感觉回答的还勉勉强强,大概是2个岗位分别招1个人,一共11人面试,实际来了9人2026.3.27&nbsp;出面试成绩,满分100分,早上10:20左右发现面试成绩46,我震惊了,没截图,后面过了十分钟重新看发现面试成绩给我改成58了。但同样震惊。朋友问我是不是把面试官打了,哈哈
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务