差异的可分割性

8 篇文章 0 订阅

题目链接

现在有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的倍数。

比如:(a-b)%p=0         a%p=m,b%p=n,,那么m和n一定相等。

把n个整数模m的值算出来,用vector实现计数,把模m后相等的值放在一起就行了。

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+10;

vector<int> ve[maxn];

int main()
{
//	freopen("input.txt","r",stdin); //提交时关闭
	std::ios::sync_with_stdio(false);
	
	int n,k,m,x;
	cin>>n>>k>>m;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		ve[x%m].push_back(x);
	}
	for(int i=0;i<m;i++)
	{
		if(ve[i].size()>=k)
		{
			cout<<"Yes"<<endl;
			int f=0;
			for(int j=0;j<k;j++)
			{
				if(f) cout<<" ";
				cout<<ve[i][j];
				f=1;
			}
			return 0;
		}
	}
	cout<<"No"<<endl;
		
	return 0;
}

涉及知识点:

1、vector实现计数,注意vector计数的时候vector必须定义成数组形式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值