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 31 8 4

output

Yes1 4 

input

3 3 31 8 4

output

No

input

4 3 52 7 7 7

output

Yes2 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 }

原文地址:https://www.cnblogs.com/Friends-A/p/11372979.html

时间: 2024-10-10 15:06:38

Codeforces 876B:Divisiblity of Differences(数学)的相关文章

CodeForces 449C Jzzhu and Apples 数学+素数

这道题目晚上本来就花了很多把都××了,着实觉得自己思路没错啊,回顾一下思路,给你n个数,分成两两组合一对,分成最多组如何分,但是组合的两个数 不能互素,所以呢 偶数肯定是好的了,所以先放着,先把素数给搞定,10^5所以枚举所有包含该素数因子的数,如果刚好分组则最好,不然的话其中有偶数的踢掉一个给下面的偶数处理部分,最后再处理偶数的部分,这样肯定满足组数最多,完全没有问题,后来方法确实是没问题啊,只是代码有问题,我靠!真是脑残!,今天看到一位大牛的想法,我跟他是一样的,只是代码写搓了,后来改了又改

Codeforces Round #259(div2)C(数学期望)

数学题. 关键是求最大值为k时有多少种情况,结果是kn-(k-1)n-1.可以这么想:每一次都从1至k里选,共kn种,这里需要再减去每一次都从1至k-1里面选的情况.当然也可以分类计数法:按出现几次k来分类,然后逆着用一下二项式定理得出结论. 整个的期望是Σk(kn-(k-1)n-1)/mn,其中k=1......n. 这里的技巧在于:由于n<=105,   kn显然会RE,那么就先把分母除上,每次算一个小于1的浮点数的n次方,肯定不会RE.C++中乘方用pow函数算是很快的. #include

CodeForces 567C. Geometric Progression(map 数学啊)

题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Polycarp loves geometric progressions very much. Since he was on

CodeForces 215B Olympic Medal(数学啊)

题目链接:http://codeforces.com/problemset/problem/215/B Description The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0?<?r2?<?r1) made of metal wi

CodeForces 469B. Chat Online(数学)

题目链接:http://codeforces.com/problemset/problem/469/B B. Chat Online time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little X and Little Z are good friends. They always chat online. But both

CodeForces 468A. 24 Game(数学构造)

题目链接:http://codeforces.com/problemset/problem/468/A A. 24 Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little X used to play a card game called "24 Game", but recently he has f

Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】

题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解: 其实就是要找一些数字,使得之和mod m为0. 开一个vector,存当前已经能够构成的数字之和mod m之后的值. 一开始vector为空,然后枚举n个数字a[i],对于每个数字枚举当前vector中的值v[i],将没有出现过的(a[i]+v[i])%m值加入vector中. 最后判断下vec

codeforces 616E. Sum of Remainders 数学

题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发现  1/4 = 2/4 = 3/4 = 0, 4/4 = 5/4 = 6/4 = 7/4 = 1. 所以可以将这些结果分成很多块, 按块算结果. 注意计算过程中时刻避免爆longlong. #include <iostream> #include <vector> #include

Codeforces Amr and Chemistry(数学+乱搞)

题意:给n个数,每个数每次可以乘二或除以二(向下取整相当于左移或右移),问最少经过多少次操作可以使这n个数变相等. 思路:首先考虑每个数的可能取值,将一个数表示成s*2^k的形式,s是奇数. 那么这个数的所有可能取值为s'*2^x,(s'=s/2,(s/2)/2,.....)且s'*2^x<=100000 因为这题数据范围不大,而且每个值可能的取值不多最多几百个,所以记录1到100000每个值可能被取到的次数以及总操作数,最后从1遍历到100000取最小的ans即可 ps:个人赛这道题做了一下午