[二分]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 }

 

全部评论

相关推荐

双飞二本嵌入式求拷打我是在&nbsp;BOSS&nbsp;上投递的简历,好多都没人回复,这是开场白和简历求大神帮忙看看。您好!我是2025届应届生,最快可在一周内上岗,能够实习六个月以上,并接受加班。以下是我的核心优势和相关经验:1.&nbsp;嵌入式开发能力:&nbsp;&nbsp;&nbsp;熟练掌握STM32系列单片机及其外设(如GPIO、定时器、ADC、DAC、I2C、SPI、UART等),能够独立完成硬件驱动开发和调试。&nbsp;&nbsp;熟悉FreeRTOS实时操作系统,具备多任务调度和资源管理经验。&nbsp;&nbsp;熟悉LVGL图形库开发,能够实现嵌入式设备的图形界面设计。2.&nbsp;硬件设计能力:&nbsp;&nbsp;&nbsp;具备PCB设计经验,曾为2023年工创赛物流搬运赛道设计小车主板,带领团队获得国家级银奖。&nbsp;&nbsp;&nbsp;熟悉硬件原理图分析,能够快速理解并调试硬件电路。3.&nbsp;机器人开发与竞赛经验:&nbsp;&nbsp;&nbsp;在全国大学生智能车竞赛、ROS机器人竞赛中多次获得国家级奖项,具备丰富的机器人开发经验。&nbsp;&nbsp;&nbsp;熟悉Linux环境,对ROS和ROS&nbsp;2有一定了解,能够进行机器人系统的开发与调试。4.&nbsp;编程能力:&nbsp;&nbsp;&nbsp;熟悉C/C++,熟悉Python,能够高效完成嵌入式开发和算法实现。&nbsp;&nbsp;&nbsp;具备良好的代码规范和文档编写能力。5.&nbsp;团队协作与领导能力:&nbsp;&nbsp;&nbsp;在多个项目中担任核心开发或团队负责人,具备良好的沟通能力和团队协作精神。&nbsp;&nbsp;&nbsp;在工创赛中带领团队完成项目规划、任务分配和技术攻关,展现了较强的领导力。我对嵌入式开发、机器人技术和智能硬件充满热情,期待加入贵公司,与团队共同成长,为公司创造价值!如果有合适的岗位,欢迎随时联系我,期待进一步沟通!
沉淀一会:嵌入式就是狗屎
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务