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;
}

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