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 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.
Examples
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
题意
给出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(数学)的更多相关文章
- Codeforces 876B Divisiblity of Differences:数学【任意两数之差为k的倍数】
题目链接:http://codeforces.com/contest/876/problem/B 题意: 给你n个数a[i],让你找出一个大小为k的集合,使得集合中的数两两之差为m的倍数. 若有多解, ...
- CodeForces - 876B Divisiblity of Differences
题意:给定n个数,从中选取k个数,使得任意两个数之差能被m整除,若能选出k个数,则输出,否则输出“No”. 分析: 1.若k个数之差都能被m整除,那么他们两两之间相差的是m的倍数,即他们对m取余的余数 ...
- Codeforces B. Divisiblity of Differences
B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...
- codeforces #441 B Divisiblity of Differences【数学/hash】
B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...
- B. Divisiblity of Differences
B. Divisiblity of Differencestime limit per test1 secondmemory limit per test512 megabytesinputstand ...
- codeforces 876B
B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...
- 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. 思路: 差能 ...
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- Codeforces 627 A. XOR Equation (数学)
题目链接:http://codeforces.com/problemset/problem/627/A 题意: 告诉你s 和 x,a + b = s a xor b = x a, b > ...
随机推荐
- python APScheduler模块
简介 一般来说Celery是python可以执行定时任务, 但是不支持动态添加定时任务 (Django有插件可以动态添加), 而且对于不需要Celery的项目, 就会让项目变得过重. APSchedu ...
- 学习java 7.19
学习内容: 接口的组成中加入了默认方法,静态方法,私有方法 接口中默认方法:public default 返回值类型 方法名(参数列表){ } public default void show() ...
- day10 负载均衡
day10 负载均衡 负载均衡反向代理 正向代理:即是客户端代理, 代理客户端, 服务端不知道实际发起请求的客户端. # (内部上网) 客户端 <-> 代理 -> 服务端 反向代理即 ...
- Fllin(七)【Flink CDC实践】
目录 FlinkCDC 1.简介 2.依赖 3.flink stream api 4.flink sql 5.自定义反序列化器 6.打包测试 FlinkCDC 1.简介 CDC是Change Data ...
- 学习Vue源码前的几项必要储备(一)
从接下来的一段时间里,Mg要进行阅读源码的工作.再阅读源码前,梳理一下准备工作. 7项重要储备 Flow 基本语法 发布/订阅模式 ES6+ 语法 原型链.闭包 函数柯里化 event loop 1. ...
- NuxtJS的AsyncData和Fetch使用详解
asyncData 简介 asyncData 可以用来在客户端加载 Data 数据之前对其做一些处理,也可以在此发起异步请求,提前设置数据,这样在客户端加载页面的时候,就会直接加载提前渲染好并带有数据 ...
- LeetCode33题——搜索旋转排序数组
1.题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存 ...
- MySQL批量数据脚本示例
一.建表 # 新建库 create database bigData; use bigData; #1 建表dept CREATE TABLE dept( id INT UNSIGNED PRIMAR ...
- Next_day()函数的用法
一.定义 NEXT_DAY(date,char) date参数为日期型, char:为1~7或Monday/Mon~Sunday/ 指定时间的下一个星期几(由char指定)所在的日期, c ...
- java中关于string类和常量池的一点猜想
public class StringTest { /** * @param args */ public static void main(String[] args) { test1 ...