题意:给定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 "); } else{ printf("Yes "); 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(" "); } return 0; }