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个字符串 对于每一个询问字符串 你需要在 ...
随机推荐
- BZOJ4570 SCOI2016妖怪(三分)
strength=atk*(1+b/a)+dnf*(1+a/b).设a/b=x,可以发现这是一个关于x的对勾函数.开口向上的一堆凸函数取max还是凸函数,三分即可. 然而无良出题人既卡精度又卡时间.众 ...
- 从统计学statistics的观点看概率分布
已知数据x,希望得到未知label y,即得到映射x-->y: 几个概念: 1)p(x): data distribution 数据分布 2)p(y): prior distribution 先 ...
- [洛谷P4838]P哥破解密码
题目大意:求长度为$n$的$01$串中,没有连续至少$3$个$1$的串的个数 题解:令$a_1$为结尾一个$1$的串个数,$a_2$为结尾两个$1$的串的个数,$b$为结尾是$0$的串的个数.$a_1 ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 工具——SVN常用命令
SVN一般都是团队合作做一个项目所需用到的,为了是版本的统一 ;1. Check out——从服务器端取得代码 把服务器资料库里存放的某个项目代码取出来,放到本地主机中,这个动作叫做“check ...
- 移动端去掉a标签点击时出现的背景
之前做移动端的Portal时,手机上测试,点击a标签总是出现一个背景框 在CSS中添加 -webkit-tap-highlight-color: rgba(0, 0, 0, 0);就可以了 a:act ...
- JavaScript中cookie使用
转自:http://www.cnblogs.com/yjzhu/archive/2012/11/26/2789032.html 一.什么是 cookie? cookie 就是页面用来保存信息,比如自动 ...
- 慕课网javascript 进阶篇 第九章 编程练习
把平常撸的码来博客上再撸一遍既可以加深理解,又可以理清思维.还是很纯很纯的小白,各位看官老爷们,不要嫌弃.最近都是晚睡,昨晚也不例外,两点多睡的.故,八点起来的人不是很舒服,脑袋有点晕呼呼,鉴于昨晚看 ...
- eclipse 4.2生成wsdl 客户端
eclipse版本 4.2 64位 ,jdk 1.6 64位 Eclipse Java EE IDE for Web Developers. Version: Juno Service Rel ...
- Junit使用的超简单介绍
Junit使用的超简单介绍 前言:我对Junit了解的并不多,只是今天突然听到有人提到了它,而且现在时间还早,所以我觉得我不妨更一篇关于Junit4的超级超级简单的用法,全当是为了省去看官网demo的 ...