Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
地址:
题目:
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
思路:
如果(a-b)%m==0 ==> a%m = b%m.
所以判断求出每个数膜m余多少就好了。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 int n,m,k,a[K],cnt[K]; 15 16 int main(void) 17 { 18 cin>>n>>k>>m; 19 for(int i=1,x;i<=n;i++) 20 scanf("%d",a+i),cnt[a[i]%m]++; 21 for(int i=0;i<m;i++) 22 if(cnt[i]>=k) 23 { 24 printf("Yes\n"); 25 for(int j=1,num=0;j<=n;j++) 26 if(a[j]%m==i) 27 { 28 printf("%d ",a[j]); 29 if(++num==k) 30 return 0; 31 } 32 } 33 printf("No\n"); 34 return 0; 35 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。