CodeForces - 876B Divisiblity of Differences
题意:给定n个数,从中选取k个数,使得任意两个数之差能被m整除,若能选出k个数,则输出,否则输出“No”。
分析:
1、若k个数之差都能被m整除,那么他们两两之间相差的是m的倍数,即他们对m取余的余数是相同的。
2、记录n个数对m取余的余数,计算出数量最多的余数ma。
3、ma>=k,才能选出,并输出即可。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int yu[MAXN];
map<int, int> mp;
int main(){
int n, k, m;
scanf("%d%d%d", &n, &k, &m);
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
for(int i = 0; i < n; ++i){
yu[i] = a[i] % m;
++mp[yu[i]];
}
int ma = 0;
int id = 0;
for(map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it){
if((*it).second > ma){
ma = (*it).second;
id = (*it).first;
}
}
if(ma < k){
printf("No\n");
}
else{
printf("Yes\n");
bool flag = true;
int t = 0;
for(int i = 0; i < n; ++i){
if(yu[i] == id){
if(flag) flag = false;
else printf(" ");
printf("%d", a[i]);
++t;
if(t == k) break;
}
}
printf("\n");
}
return 0;
}
CodeForces - 876B Divisiblity of Differences的更多相关文章
- Codeforces 876B Divisiblity of Differences:数学【任意两数之差为k的倍数】
题目链接:http://codeforces.com/contest/876/problem/B 题意: 给你n个数a[i],让你找出一个大小为k的集合,使得集合中的数两两之差为m的倍数. 若有多解, ...
- Codeforces B. Divisiblity of Differences
B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...
- Codeforces 876B:Divisiblity of Differences(数学)
B. Divisiblity of Differences You are given a multiset of n integers. You should select exactly k of ...
- 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 - 876B H - 差异的可分割性
现在有n个整数,在这n个数中找出k个数,保证这k个数中任意两个数差的绝对值可以被m整除. Input第一行输入三个整数n,k,m(2<=k<=n<=100000,1<=m< ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
A. Trip For Meal 题目链接:http://codeforces.com/contest/876/problem/A 题目意思:现在三个点1,2,3,1-2的路程是a,1-3的路程是b, ...
随机推荐
- Java的下载与安装
下载Java 如果要是想聊天,那我们就需要腾讯QQ,先把它下载过来安装上,就可以聊天了,那如果是想要开发java程序呢,我们就需要下载一个开发工具包“jdk”,安装上就可以开发了.当然java是属于哪 ...
- 第二周之Hadoop学习(二)
这篇博客根据http://dblab.xmu.edu.cn/blog/install-hadoop-in-centos/中的教程进行学习: 首先按照教程的操作的第一步的时候,电脑直接蓝屏了. 但是依然 ...
- git pull 之后怎么找回别覆盖掉的内容
[半夜吓出冷汗,git这个原理还真得好好学学] 不小心把本地写的东西pull了下,然后,全部覆盖掉了,以为就这样没了. 后面想到有“时光穿梭机”,“历史回滚”,在各大群友的帮助下,终于找回了. git ...
- 15条MySQL改善经验让系统更稳定
MySQL 作为目前的主流开源数据库, 既能挑战 “双11”,又能扛起 “618”,比起网红版数据库,MySQL 绝对是实力担当. 1. 为查询缓存优化查询 像 NOW() 和 RAND() 或是其它 ...
- Mybatis的逆向工程以及Example的实例函数及详解
Mybatis-generator是Mybatis的逆向工程 (根据数据库中的表生成java代码) Mybatis的逆向工程会生成实例及实例对应的example,example用于添加条件,相当于w ...
- 区分 for...in 和 for...of
我们都知道在 JavaScript 中 for...in 和 for...of 都可以迭代一个数组,但他们之间也有着很大的区别: 区别一:用于迭代器的返回值不同 for...in 和 for...of ...
- Spring boot 启动图片
生成工具:http://patorjk.com/software/taag/#p=testall&h=0&v=0&f=Graffiti&t=Type%20Somethi ...
- Python学习笔记009
不换行 print("Hello,world!",end='')print("Hello,world!",end='')print("Hello,wo ...
- TensorFlow样例一
假设原函数为 f(x) = 5x^2 + 3,为了估计出这个函数,定义参数未知的函数g(x, w) = w0 x^2 + w1 x + w2,现要找出适合的w使g(x, w) ≍ f(x).将这个问题 ...
- Java中正确使用hashCode和equals方法
在这篇文章中,我将告诉大家我对hashCode和equals方法的理解.我将讨论他们的默认实现,以及如何正确的重写他们.我也将使用Apache Commons提供的工具包做一个实现. 目录: hash ...