B. Divisiblity of Differences

B. Divisiblity of Differences
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.

如果集合中两两之差能被m整除,那么它们%m之后的余数应该相等。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register int i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.1
17 using namespace std;
18 int n,k,m;
19 int a[100010];
20 int b[100010];
21 int l;
22 int cnt;
23 
24 void in(int &x)
25 {
26     int y=1;
27     char c=g();x=0;
28     while(c<'0'||c>'9')
29     {
30     if(c=='-')
31     y=-1;
32     c=g();
33     }
34     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
35     x*=y;
36 }
37 void o(int x)
38 {
39     if(x<0)
40     {
41         p('-');
42         x=-x;
43     }
44     if(x>9)o(x/10);
45     p(x%10+'0');
46 }
47 int main()
48 {
49     in(n),in(k),in(m);
50     For(i,1,n)
51     {
52     in(a[i]);    
53     b[a[i]%m]++;
54     if(cnt<b[a[i]%m])
55     {
56     cnt=b[a[i]%m];    
57     l=a[i]%m;
58     }
59     }
60     if(cnt<k)
61     {
62         puts("No");
63         exit(0);
64     }
65     puts("Yes");
66     For(i,1,n)
67     {
68         if(a[i]%m==l)
69         {
70             k--;
71             o(a[i]),p(' ');
72         }
73         if(k==0)
74         break;
75     }
76      return 0;
77 }

 

posted @ 2017-11-01 21:18  WeiAR  阅读(157)  评论(0编辑  收藏  举报