Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.util.InputMismatchException;
- import java.util.SortedMap;
- import java.util.TreeMap;
- /**
- * Created by Rakshit on 15/4/17.
- * Java is Love.. :)
- */
- public class Kpairs {
- public static void main(String args[])
- {
- InputStream stream=System.in;
- OutputStream out1=System.out;
- InputReader in = new InputReader(stream);
- PrintWriter out=new PrintWriter(out1);
- Task solver=new Task();
- solver.solve(in,out);
- }
- static class Task
- {
- int n;
- int k;
- SortedMap<Integer,Integer>check=new TreeMap<>();
- int a[]=new int [11111];
- public void solve(InputReader in , PrintWriter out)
- {
- Integer c;
- n=in.nextInt();
- k=in.nextInt();
- for(int i=0;i<n;i++)
- {
- c=in.nextInt();
- if (check.containsKey(c))
- {
- check.put(c,check.get(c)+1);
- }
- else
- check.put(c,1);
- }
- long res = 0;
- if (k!=0) {
- for (int i = 0; i < 10003; i++) {
- for (int j = i + 1; j < 10004; j++) {
- if (Integer.bitCount(i ^ j) == k) {
- if (check.containsKey(i) && check.containsKey(j)) {
- res += (long) (check.get(i)) * (long) (check.get(j));
- // out.println(i+" "+j);
- }
- }
- }
- }
- }
- else
- {
- for(int i=0;i < 10003 ;i++)
- {
- if (check.containsKey(i) && check.get(i) > 1)
- {
- long j = (long) (check.get(i));
- res += (j)*(j-1)/2;
- }
- }
- }
- out.println(res);
- out.flush();
- }
- }
- static class InputReader {
- private final InputStream stream;
- private final byte[] buf = new byte[8192];
- private int curChar, snumChars;
- public InputReader(InputStream st) {
- this.stream = st;
- }
- public int read() {
- if (snumChars == -1)
- throw new InputMismatchException();
- if (curChar >= snumChars) {
- curChar = 0;
- try {
- snumChars = stream.read(buf);
- } catch (IOException e) {
- throw new InputMismatchException();
- }
- if (snumChars <= 0)
- return -1;
- }
- return buf[curChar++];
- }
- public int nextInt() {
- int c = read();
- while (isSpaceChar(c)) {
- c = read();
- }
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = read();
- }
- int res = 0;
- do {
- res *= 10;
- res += c - '0';
- c = read();
- } while (!isSpaceChar(c));
- return res * sgn;
- }
- public long nextLong() {
- int c = read();
- while (isSpaceChar(c)) {
- c = read();
- }
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = read();
- }
- long res = 0;
- do {
- res *= 10;
- res += c - '0';
- c = read();
- } while (!isSpaceChar(c));
- return res * sgn;
- }
- public int[] nextIntArray(int n) {
- int a[] = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = nextInt();
- }
- return a;
- }
- public String readString() {
- int c = read();
- while (isSpaceChar(c)) {
- c = read();
- }
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = read();
- } while (!isSpaceChar(c));
- return res.toString();
- }
- public String nextLine() {
- int c = read();
- while (isSpaceChar(c))
- c = read();
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = read();
- } while (!isEndOfLine(c));
- return res.toString();
- }
- public boolean isSpaceChar(int c) {
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
- private boolean isEndOfLine(int c) {
- return c == '\n' || c == '\r' || c == -1;
- }
- }
- }
Add Comment
Please, Sign In to add comment