其他分享
首页 > 其他分享> > Codeforces 876B:Divisiblity of Differences(数学)

Codeforces 876B:Divisiblity of Differences(数学)

作者:互联网

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.

Input

First line contains three integers nk 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整除。

 思路

两个数相减能够被m整除,也就是说这两个数对m取模得到的结果是相同的

所以只要统计数组中的每个元素对m取模的结果的个数,判断是否大于k,如果没有大于k的,那么输出No

否则,找到出现次数最多的那个值(任意一个都可以,只要大于等于k),假设为mo,遍历数组,如果对m取模等于mo,输出,直到输出k个,停止

代码

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;
 6 const ll INF=0x3f3f3f3f3f3f3f3f;
 7 const int maxn=1e6+10;
 8 const int mod=1e9+7;
 9 const int maxm=1e3+10;
10 using namespace std;
11 int a[maxn];
12 int vis[maxn];
13 bool cmp(int a,int b)
14 {
15     return a>b;
16 }
17 int main(int argc, char const *argv[])
18 {
19     #ifndef ONLINE_JUDGE
20         freopen("/home/wzy/in.txt", "r", stdin);
21         freopen("/home/wzy/out.txt", "w", stdout);
22         srand((unsigned int)time(NULL));
23     #endif
24     ios::sync_with_stdio(false);
25     cin.tie(0);
26     int n,m,k;
27     cin>>n>>k>>m;
28     int cnt=0;
29     int num;
30     int maxx=0;
31     for(int i=0;i<n;i++)
32     {
33         cin>>a[i];
34         if(!vis[a[i]%m])
35             cnt++;
36         vis[a[i]%m]++;
37         if(maxx<vis[a[i]%m])
38             maxx=vis[a[i]%m],num=a[i]%m;
39     }
40     if(maxx<k)
41         cout<<"No\n";
42     else
43     {
44         int res=0;
45         cout<<"Yes\n";
46         for(int i=0;i<n;i++)
47         {
48             if(a[i]%m==num)
49             {
50                 res++;
51                 cout<<a[i]<<" ";
52             }
53             if(res==k)            
54                 break;
55         }
56         cout<<"\n";
57     }
58     #ifndef ONLINE_JUDGE
59         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
60     #endif
61     return 0;
62 }

 

标签:integers,Divisiblity,const,int,Differences,Codeforces,number,output,multiset
来源: https://www.cnblogs.com/Friends-A/p/11372979.html