CodeForces - 876B H - 差异的可分割性

现在有n个整数,在这n个数中找出k个数,保证这k个数中任意两个数差的绝对值可以被m整除。

Input第一行输入三个整数n,k,m(2<=k<=n<=100000,1<=m<=100000)。 
第二行包含n个整数a1,a2,...,  an(0 <= ai <= 10^9 )。Output如果不存在这样的k个数,输出"No";
否则输出"Yes"后,在下一行输出这k个数,数与数之间用空格隔开。 (存在多种情况,输出任意一种)。Example

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
如果两个数对m取余得到的值相等那么二者的差值一定可以被m整除。所以余数相同的值我们把他放在一起。
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
const int N=1e5+7;
vector<int >ve[N];
int main(){
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    int x;
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        int y=x%k;
        ve[y].push_back(x);
    }
    
    bool check=false;
    
    for(int i=0;i<k;i++){
        if(ve[i].size()>=m){
            check=true;
            puts("Yes");
            for(int j=0;j<m;j++){
                printf("%d ",ve[i][j]);
            }
            break;
        }
    }
    if(!check) puts("No");
     return 0;
}


猜你喜欢

转载自www.cnblogs.com/Accepting/p/11373427.html