Codeforces div3 Preparation for International Women's Day (同余定理)

International Women’s Day is coming soon! Polycarp is preparing for the holiday.
There are
n
n
candy boxes in the shop for sale. The
i
i
-th box contains
d
i
di
candies.
Polycarp wants to prepare the maximum number of gifts for
k
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
k
. In other words, two boxes
i
i
and
j
j
(
i≠j
i≠j
) can be combined as a gift if
d
i
+
d
j
di+dj
is divisible by
k
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
n
and
k
k
(
1≤n≤2⋅
10
5
,1≤k≤100
1≤n≤2⋅105,1≤k≤100
) — the number the boxes and the number the girls.
The second line of the input contains
n
n
integers
d
1
,
d
2
,…,
d
n
d1,d2,…,dn
(
1≤
d
i

10
9
1≤di≤109
), where
d
i
di
is the number of candies in the
i
i
-th box.
Output
Print one integer — the maximum number of the boxes Polycarp can give as gifts.
Examples
Input
Copy
7 2
1 2 2 3 2 4 10
Output
Copy
6
Input
Copy
8 2
1 2 2 3 2 4 6 10
Output
Copy
8
Input
Copy
7 3
1 2 2 3 2 4 5
Output
Copy
4
Note
In the first example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes):
(2,3)
(2,3)
;
(5,6)
(5,6)
;
(1,4)
(1,4)
.
So the answer is
6
6
.
In the second example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes):
(6,8)
(6,8)
;
(2,3)
(2,3)
;
(1,4)
(1,4)
;
(5,7)
(5,7)
.
So the answer is
8
8
.
In the third example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes):
(1,2)
(1,2)
;
(6,7)
(6,7)
.
So the answer is
4
4
.
问题链接: http://codeforces.com/contest/1133/problem/B
问题简述: 给两个数n和k,求n个数每两个数求和mod k为0的最大数量
问题分析: 根据同余定理,一对数模k为0的情况有两种:
1:两个数各自mod k余0。
2:两个数各自mod k余数求和为k
只要将每个输入的数mod k累加到一个数组即可,再遍历即可(注意分k为奇偶数的情况,为偶数时,可以分为k/2+k/2),具体可看代码及注释
AC通过的C++语言程序如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;

const int N=105;

int main()
{
    int cnt[N];//存储mod k余数的个数
    memset(cnt,0,sizeof(cnt));//初始化
    int n,k,ans=0;
    cin>>n>>k;
    while(n--)//输入
    {
        int a;
        cin>>a;
        cnt[a%k]++;
    }
    ans+=cnt[0]/2;//同余:求两数和mod k为0的个数分为两种:
    //1.n1%k==0&&n2%k==0;
    //2.n1%k+n2%k==k;
    if(k%2==0) ans+=cnt[k/2]/2;
    //当k为偶数,先算出为其对半的情况
    for(int i=1;i<(k+1)/2;i++)//算出其余情况,遍历到(k+1)/2
    {
        ans+=min(cnt[i],cnt[k-i]);//取相加和为k的数量的较小值
    }
    cout<<ans*2;//对数*2为数量
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章