现在有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必须定义成数组形式。