Codeforces-876B-Divisiblity of Differences(取模)
2017-10-17 13:46
344 查看
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard 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.
Input
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.
Output
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.
Examples
input
3 2 3
1 8 4
output
Yes
1 4
input
3 3 3
1 8 4
output
No
input
4 3 5
2 7 7 7
output
Yes
2 7 7
题意:给定n个正整数(可以重复),找出其中的k个数(可以重复),这k个数需要满足的条件的任意选出两个数作差之后,都是m的倍数,如果存在的,输出yes,然后输出随便一个符合的结果。如果不存在的话,输出no。
题解:一开始想暴力去计数,但是发现开不了那么的空间。。。后来稍微思考一下,这其实就是按照modm的结果进行分类的一道题,所以直接遍历一遍,记录modm的结果的数量存数组,然后在找一遍这个数组,找到>=k的结果就是有了。
注意点:打印的时候注意只能打印k个,不要打印多了。
代码:
#include<iostream>
#include<fstream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<utility>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const int mod = 1e9+7;
const int INF = 1<<30;
const ll llINF = 1e18+999;
int n, k, m, ans, cnt, max_num, in[maxn], num[maxn];
void init( )
{
memset(num, 0, sizeof(num));
}
int main( )
{
//freopen("input.txt", "r", stdin);
while(~scanf("%d%d%d", &n, &k, &m))
{
init( );
for(int i=0; i<n; i++)
{
scanf("%d", in+i);
num[in[i]%m]++;
}
int flag = 0, ans = 0;
for(int i=0; i<maxn && flag==0; i++)
if(num[i] >= k)
flag = 1, ans = i;
if(flag)
{
cout<<"Yes"<<endl;
int t = 0;
for(int i=0; i<n && t<k; i++)
if(in[i]%m == ans)
cout<<in[i]<<" ", t++;
cout<<endl;
}
else
cout<<"No"<<endl;
}
return 0;
}
相关文章推荐
- Codeforces 876 B Divisiblity of Differences
- codeforce_876B_ B - Divisiblity of Differences
- Codeforces Round #441 (Div. 2)B. Divisiblity of Differences(哈希的运用)
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
- codeforces round #441 B. Divisiblity of Differences
- Codeforces Round #441 div2 B. Divisiblity of Differences
- Codeforces Round #441 B. Divisiblity of Differences
- Codeforces 876 B Divisiblity of Differences 基础数学
- ACM刷题之codeforce————Divisiblity of Differences
- CF B. Divisiblity of Differences【water+WA9】
- Codeforces Round #441 B.Divisiblity of Differences
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
- Codeforces Round #441 B. Divisiblity of Differences
- Codeforces Round #441 B. Divisiblity of Differences
- - Divisiblity of Differences CodeForces - 876B
- Codeforces Round #441 Div 2 B. Divisiblity of Differences
- B. Divisiblity of Differences
- Codeforces876B-Divisiblity of Differences
- codeforces 85D D. Sum of Medians Vector的妙用
- Codeforces 27E. Number With The Given Amount Of Divisors (暴力)