個人覺得比較難想誒
如果i+j可以被k整除,那么i%k+j%k == 0或者k
利用這個性質(zhì)可以將數(shù)按照取余的余數(shù)分割為k個數(shù)組磕蛇,然后如果a[i]+a[j]==k景描,則比較a[i]和a[j]的長度,誰長選擇哪一個添加到subset中秀撇,對于a[0]超棺,如果大于0則result+1,如果k%2==0呵燕,則對于a[k/2]棠绘,如果大于0則result+1,具體代碼如下:
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by huliang on 16/7/25.
*/
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
int[] a = new int[n];
int[][] arr = new int[k][1];
// 獲取原始數(shù)據(jù)
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
arr[a[i]%k][0]++;
}
int result = 0;
if (arr[0][0] > 0) {
result++;
}
for (int i = 1; i < k / 2 + 1; i++) {
if (i == k - i) {
result += 1;
} else {
int length1 = arr[i][0];
int length2 = arr[k-i][0];
result += Math.max(length1, length2);
}
}
System.out.println(result);
}
}