Code forces 275C-----思维---二分
来源:互联网 发布:mysql 高级sql语句 编辑:程序博客网 时间:2024/06/18 11:19
Description
A 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}.
Sample Input
6 22 3 6 5 4 10
3
这个题感觉还是公式,找规律应该可以找到,应该会非常非常麻烦,直接用规律做了。就是从小到大遍历,某个数和它的k倍不能同时出现,总是把它的k倍踢出去才是最优解,数学真奇妙---
因为数据多而且大,所以用二分了,否则超时
#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>using namespace std;#define maxn 500005#define eps 2e12__int64 a[maxn];bool b[maxn];int main(){int n, k;__int64 s, e, m, cnt;scanf("%d%d", &n, &k);for(int i = 0; i < n; i++){scanf("%I64d", &a[i]);}cnt = n;memset(b, true, sizeof(b));sort(a, a + n);for(int i = 0; i < n; i++){if(b[i]){s = i+1;e = n-1;if(a[i]*k > a[e]){break;}while(e >= s){m = (e+s) >> 1;if(a[m] > k*a[i]){e = m-1;}else if(a[m] < k*a[i]){s = m+1;}else{b[m] = false;cnt--;break;}}}}printf("%I64d\n", cnt);return 0;}
- Code forces 275C-----思维---二分
- Code Forces #5C
- Code forces 612C
- Code forces 448D-----二分
- Code forces 670D2-----二分
- Code Forces 612A-----思维题
- Code forces 348A-----思维题
- code forces 370C. Mittens
- code forces 401C Team
- Code Forces 26C Dijkstra?
- Code forces 612C-----栈
- Code forces 200C---Football Championship
- Code Forces 369 C 深搜,图
- Code forces 376 C Divisible by Seven
- code forces 405C Unusual Product
- Code Forces 298 C Polycarpus' Dice
- Code Forces 469C 24 Game
- Code Forces 525C Ilya and Sticks
- java中生成伪随机数的一些简单实现方法
- android 实现类似知乎 上推隐藏头尾 下拉出现头尾的Demo
- 云存储应用场景全总结 – 音视频篇
- 使用日期和时间表单帮助方法
- zookeeper知识点二(更新节点数据)
- Code forces 275C-----思维---二分
- Android DEX方法超过64K和gradle编译OOM解决方案
- 图解Android Studio 2.0安装步骤
- 110. Balanced Binary Tree
- (总结)CentOS Linux下VNC Server远程桌面配置详解
- 详解多旋翼飞行器的传感器技术
- hdu5753(2016多校第三场,数学题)
- 【POJ】-1979-Red and Black(DFS)
- 判断一个十进制数是几位数