codeforces #441 B Divisiblity of Differences【数学/hash】
1 second
512 megabytes
standard input
standard output
You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.
Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.
First line contains three integers n, k and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.
Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.
If it is not possible to select k numbers in the desired way, output «No» (without the quotes).
Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them.
3 2 3
1 8 4
Yes
1 4
3 3 3
1 8 4
No
4 3 5
2 7 7 7
Yes
2 7 7 【题意】:给你n个数a[i],让你找出一个大小为k的集合,使得集合中的数两两之差为m的倍数。 若有多解,输出任意一个集合即可。
【分析】:若一个集合中的数,两两之差为m的倍数,则他们 mod m 的值均相等。所以O(N)扫一遍,对于每个数a:vector v[a%m].push_back(a) 一旦有一个集合大小为k,则输出。
【代码】:
#include<bits/stdc++.h>
using namespace std; int main(){
int n,k,m;
cin>>n>>k>>m;
int arr[m]={};
long int val[n];
for(int i=;i<n;i++){
cin>>val[i];
arr[val[i]%m]++;
}
int pos=-;
for(int i=;i<m;i++){
if(arr[i]>=k){
pos=i;
break;
}
}
if(pos==-){
cout<<"No"<<endl;
}
else{
cout<<"Yes"<<endl;
int i=;
while(k--){
while(val[i]%m!=pos){
i++;
}
cout<<val[i]<<" ";
i++;
}
cout<<endl;
}
return ;
}
#include<bits/stdc++.h>
using namespace std; int a[], b[]; int main()
{
int n, k, m;
scanf("%d%d%d", &n, &k, &m);
memset(b, , sizeof(b));
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
b[a[i]%m]++;
}
int len = ;
for(int i = ; i <= ; i++)
{
if(b[i] >= k)
{
for(int j = ; j <= n && len < k; j++) if(a[j] % m == i) a[len++] = a[j];
}
}
if(len == ) puts("No");
else
{
puts("Yes");
for(int i = ; i < len; i++) printf("%d%c", a[i], i == len - ? '\n' : ' ');
}
return ;
}
codeforces #441 B Divisiblity of Differences【数学/hash】的更多相关文章
- Codeforces 876B:Divisiblity of Differences(数学)
B. Divisiblity of Differences You are given a multiset of n integers. You should select exactly k of ...
- Codeforces B. Divisiblity of Differences
B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...
- Codeforces#441 Div.2 四小题
Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...
- B. Divisiblity of Differences
B. Divisiblity of Differencestime limit per test1 secondmemory limit per test512 megabytesinputstand ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
http://codeforces.com/contest/876/problem/B 题意: 给出n个数,要求从里面选出k个数使得这k个数中任意两个的差能够被m整除,若不能则输出no. 思路: 差能 ...
- Codeforces 876B Divisiblity of Differences:数学【任意两数之差为k的倍数】
题目链接:http://codeforces.com/contest/876/problem/B 题意: 给你n个数a[i],让你找出一个大小为k的集合,使得集合中的数两两之差为m的倍数. 若有多解, ...
- CodeForces - 876B Divisiblity of Differences
题意:给定n个数,从中选取k个数,使得任意两个数之差能被m整除,若能选出k个数,则输出,否则输出“No”. 分析: 1.若k个数之差都能被m整除,那么他们两两之间相差的是m的倍数,即他们对m取余的余数 ...
- Codeforces Beta Round #7 D. Palindrome Degree hash
D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...
- 【codeforces 514C】Watto and Mechanism(字符串hash)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
随机推荐
- line-height用法总结
Line-height是前端用语,经常被前端开发人员经常使用. line-height设置1.5和150%有什么区别?这是一个比较常见的前端面试题. 定义: line-height指的是文本行基线间的 ...
- 【NOIP 模拟赛】改造二叉树 最长上升子序列
biubiu~~~ 这道题我一眼就以为是线段树优化dp并且有了清晰的思路但是发现,我不会线段树区间平移,我以为只是我不会,然而根本就不行........ 正解是把序列排出来然后我们让他们减去他们的下标 ...
- 如何使用Photoshop批量扫描保存文档
以笔主手头上的Canon LIDE 100为例 先安装好扫描仪驱动程序,可使用自带驱动光盘或驱动精灵等程序完成. 打开Photoshop程序,以CS5为例,找到扫描仪入口: 点开高级模式进行配置,笔主 ...
- Ubuntu下安装LNMP之独立添加php扩展模块
使用php的过程中,发现某个扩展没有添加,又不想重新编译php,这个时候我们就需要单独添加需要的扩展模块. 下面以mysqli扩展模块为例,具体介绍安装步骤. 1.安装mysql 具体参考:Ubunt ...
- PHP 扒一扒这些题目都考了哪些知识点
1.模除 题目: <?php echo -10%3; *结果* -1 分析:其实这道题的知识点是在考模除和正负号的关系,那么我们看一段进阶的代码 <?php echo "10%3 ...
- [转]使用 LDAP 组或角色限制访问,包含部分单点登录SSO说明
参考:http://www-01.ibm.com/support/knowledgecenter/api/content/SSEP7J_10.2.2/com.ibm.swg.ba.cognos.crn ...
- WebComponents001
Sample1: ShadowDom 隔离style,替换显示内容 <button>Hello, world!</button> <script> var host ...
- bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401
这道题就是典型的单调队列优化dp了 很明显状态转移的方式有三种 1.前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.前i-W-1天买进一些股: dp[i][j ...
- DotNet 学习笔记 MVC模型
Model Binding Below is a list of model binding attributes: •[BindRequired]: This attribute adds a mo ...
- urlparse获取url后面的参数
copyfrom: http://www.cnpythoner.com/post/263.html 如果给定你一个URL,比如: http://url/api?param=2¶m2=4 ...