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 }

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

  1. Codeforces 876B Divisiblity of Differences:数学【任意两数之差为k的倍数】

    题目链接:http://codeforces.com/contest/876/problem/B 题意: 给你n个数a[i],让你找出一个大小为k的集合,使得集合中的数两两之差为m的倍数. 若有多解, ...

  2. CodeForces - 876B Divisiblity of Differences

    题意:给定n个数,从中选取k个数,使得任意两个数之差能被m整除,若能选出k个数,则输出,否则输出“No”. 分析: 1.若k个数之差都能被m整除,那么他们两两之间相差的是m的倍数,即他们对m取余的余数 ...

  3. Codeforces B. Divisiblity of Differences

    B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...

  4. codeforces #441 B Divisiblity of Differences【数学/hash】

    B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...

  5. B. Divisiblity of Differences

    B. Divisiblity of Differencestime limit per test1 secondmemory limit per test512 megabytesinputstand ...

  6. codeforces 876B

    B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...

  7. Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences

    http://codeforces.com/contest/876/problem/B 题意: 给出n个数,要求从里面选出k个数使得这k个数中任意两个的差能够被m整除,若不能则输出no. 思路: 差能 ...

  8. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  9. Codeforces 627 A. XOR Equation (数学)

    题目链接:http://codeforces.com/problemset/problem/627/A 题意: 告诉你s 和 x,a + b = s    a xor b = x   a, b > ...

随机推荐

  1. STM32一些特殊引脚做IO使用的注意事项

    1 PC13.PC14.PC15的使用 这三个引脚与RTC复用,<STM32参考手册>中这样描述: PC13 PC14 PC15需要将VBAT与VDD连接,实测采用以下程序驱动4个74HC ...

  2. DBMS_RANDOM包详解

    DBMS_RAMDOM包中一共包含9个存储过程和函数,其中6个是现在用的,3个是已经过时的: 当前版本11gR2 每次生成一个随机数oracle都会初始化一个种子,也可以调用seed过程自己初始化一个 ...

  3. 【面试】【Linux】【Web】基础概念

    1. HTTP https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol 2. TCP handshake https://en.wikipe ...

  4. Spring MVC入门(一)—— RestTemplate组件

    ClientHttpRequestFactory 它是个函数式接口,用于根据URI和HttpMethod创建出一个ClientHttpRequest来发送请求~ ClientHttpRequest它代 ...

  5. 关于使用Topshelf创建服务

    目录 0. 背景说明 1. 使用Topshelf组件创建Windows服务 1.1 依赖Quartz.net实现定时任务 1.2 依赖于Topshelf创建服务类 1.3 log4net的配置文件lo ...

  6. array_filter()用法

    第一种情况: 通过函数,过滤数组中的元素 array_filter($arr,'函数名称') 函数里可以写相应的过滤原则,下面举个栗子,过滤掉不是数字的元素 $arr=array('a','b','c ...

  7. 转:Intent 操作常用URI代码示例

    以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent 一.打开一个网页,类别是Intent.ACTION_VIEW 1 2 Uri uri = Uri.parse(&q ...

  8. .NET 云原生架构师训练营(建立系统观)--学习笔记

    目录 目标 ASP .NET Core 什么是系统 什么是系统思维 系统分解 什么是复杂系统 作业 目标 通过整体定义去认识系统 通过分解去简化对系统的认识 ASP .NET Core ASP .NE ...

  9. Blazor中的无状态组件

    声明:本文将RenderFragment称之为组件DOM树或者是组件DOM节点,将*.razor称之为组件. 1. 什么是无状态组件 如果了解React,那就应该清楚,React中存在着一种组件,它只 ...

  10. Python软件目录结构

    目录组织方式 关于如何组织一个较好的Python工程目录结构,已经有一些得到了共识的目录结构.在Stackoverflow的这个问题上,能看到大家对Python目录结构的讨论. 这里面说的已经很好了, ...