rakcode1998

Untitled

Apr 19th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.io.PrintWriter;
  5. import java.util.InputMismatchException;
  6. import java.util.SortedMap;
  7. import java.util.TreeMap;
  8.  
  9. /**
  10.  * Created by Rakshit on 15/4/17.
  11.  * Java is Love.. :)
  12.  */
  13.  
  14. public class Kpairs {
  15.  
  16.     public static void main(String args[])
  17.     {
  18.         InputStream stream=System.in;
  19.         OutputStream out1=System.out;
  20.         InputReader in = new InputReader(stream);
  21.         PrintWriter out=new PrintWriter(out1);
  22.         Task solver=new Task();
  23.         solver.solve(in,out);
  24.     }
  25.  
  26.     static class Task
  27.     {
  28.         int n;
  29.         int k;
  30.         SortedMap<Integer,Integer>check=new TreeMap<>();
  31.         int a[]=new int [11111];
  32.         public void solve(InputReader in , PrintWriter out)
  33.         {
  34.              Integer c;
  35.              n=in.nextInt();
  36.              k=in.nextInt();
  37.  
  38.              for(int i=0;i<n;i++)
  39.              {
  40.                  c=in.nextInt();
  41.  
  42.                  if (check.containsKey(c))
  43.                  {
  44.                     check.put(c,check.get(c)+1);
  45.                  }
  46.                  else
  47.                      check.put(c,1);
  48.              }
  49.  
  50.              long res = 0;
  51.  
  52.              if (k!=0) {
  53.                  for (int i = 0; i < 10003; i++) {
  54.                      for (int j = i + 1; j < 10004; j++) {
  55.                          if (Integer.bitCount(i ^ j) == k) {
  56.                              if (check.containsKey(i) && check.containsKey(j)) {
  57.                                  res += (long) (check.get(i)) * (long) (check.get(j));
  58.                                  //        out.println(i+" "+j);
  59.                              }
  60.                          }
  61.                      }
  62.                  }
  63.              }
  64.  
  65.              else
  66.              {
  67.                  for(int i=0;i < 10003 ;i++)
  68.                  {
  69.                      if (check.containsKey(i) && check.get(i) > 1)
  70.                      {
  71.                          long j = (long) (check.get(i));
  72.                          res += (j)*(j-1)/2;
  73.                      }
  74.                  }
  75.              }
  76.  
  77.              out.println(res);
  78.  
  79.              out.flush();
  80.         }
  81.     }
  82.  
  83.     static class InputReader {
  84.  
  85.         private final InputStream stream;
  86.         private final byte[] buf = new byte[8192];
  87.         private int curChar, snumChars;
  88.  
  89.         public InputReader(InputStream st) {
  90.             this.stream = st;
  91.         }
  92.  
  93.         public int read() {
  94.             if (snumChars == -1)
  95.                 throw new InputMismatchException();
  96.             if (curChar >= snumChars) {
  97.                 curChar = 0;
  98.                 try {
  99.                     snumChars = stream.read(buf);
  100.                 } catch (IOException e) {
  101.                     throw new InputMismatchException();
  102.                 }
  103.                 if (snumChars <= 0)
  104.                     return -1;
  105.             }
  106.             return buf[curChar++];
  107.         }
  108.  
  109.         public int nextInt() {
  110.             int c = read();
  111.             while (isSpaceChar(c)) {
  112.                 c = read();
  113.             }
  114.             int sgn = 1;
  115.             if (c == '-') {
  116.                 sgn = -1;
  117.                 c = read();
  118.             }
  119.             int res = 0;
  120.             do {
  121.                 res *= 10;
  122.                 res += c - '0';
  123.                 c = read();
  124.             } while (!isSpaceChar(c));
  125.             return res * sgn;
  126.         }
  127.  
  128.         public long nextLong() {
  129.             int c = read();
  130.             while (isSpaceChar(c)) {
  131.                 c = read();
  132.             }
  133.             int sgn = 1;
  134.             if (c == '-') {
  135.                 sgn = -1;
  136.                 c = read();
  137.             }
  138.             long res = 0;
  139.             do {
  140.                 res *= 10;
  141.                 res += c - '0';
  142.                 c = read();
  143.             } while (!isSpaceChar(c));
  144.             return res * sgn;
  145.         }
  146.  
  147.         public int[] nextIntArray(int n) {
  148.             int a[] = new int[n];
  149.             for (int i = 0; i < n; i++) {
  150.                 a[i] = nextInt();
  151.             }
  152.             return a;
  153.         }
  154.  
  155.         public String readString() {
  156.             int c = read();
  157.             while (isSpaceChar(c)) {
  158.                 c = read();
  159.             }
  160.             StringBuilder res = new StringBuilder();
  161.             do {
  162.                 res.appendCodePoint(c);
  163.                 c = read();
  164.             } while (!isSpaceChar(c));
  165.             return res.toString();
  166.         }
  167.  
  168.         public String nextLine() {
  169.             int c = read();
  170.             while (isSpaceChar(c))
  171.                 c = read();
  172.             StringBuilder res = new StringBuilder();
  173.             do {
  174.                 res.appendCodePoint(c);
  175.                 c = read();
  176.             } while (!isEndOfLine(c));
  177.             return res.toString();
  178.         }
  179.  
  180.         public boolean isSpaceChar(int c) {
  181.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  182.         }
  183.  
  184.         private boolean isEndOfLine(int c) {
  185.             return c == '\n' || c == '\r' || c == -1;
  186.         }
  187.     }
  188. }
Add Comment
Please, Sign In to add comment