codeforces1133B. Preparation for International Women’s Day
International Women’s Day is coming soon! Polycarp is preparing for the holiday.
There are n candy boxes in the shop for sale. The i-th box contains di candies.
Polycarp wants to prepare the maximum number of gifts for k girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by k. In other words, two boxes i and j (i≠j) can be combined as a gift if di+dj is divisible by k.
How many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes “partially” or redistribute candies between them.
Input
The first line of the input contains two integers n and k (1≤n≤2⋅105,1≤k≤100) — the number the boxes and the number the girls.
The second line of the input contains n integers d1,d2,…,dn (1≤di≤109), where di is the number of candies in the i-th box.
Output
Print one integer — the maximum number of the boxes Polycarp can give as gifts.
Examples
inputCopy
7 2
1 2 2 3 2 4 10
outputCopy
6
inputCopy
8 2
1 2 2 3 2 4 6 10
outputCopy
8
inputCopy
7 3
1 2 2 3 2 4 5
outputCopy
4
Hint
题意:
给出一组数, 求最多能找出几对和为k的倍数的数
题解:
题目很类似小乐乐的组合数那道题, 比较经典的一种思维
首先对所有数进行%k的预处理并存入一个大小为k的数组中, 然后按照贪心遍历一遍该数组即可
注意对相等情况的判断
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1<<30;
const LL maxn = 2*1e5+10;
int n, k, a[maxn];
int main()
{
int ans = 0, input;
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> input;
a[input%k]++;
}
ans += a[0]/2;
for(int i=1;i<k;i++)
{
if(a[i%k]!=a[k-i%k])
ans += min(a[i%k], a[k-i%k]);
else if(i%k == k-i%k)
ans += a[i%k]/2;
else if(a[i%k]==a[k-i%k])
ans += a[i%k];
a[i%k]=a[k-i%k]=0;
}
cout << ans*2 << endl;
return 0;
}