Guest User

Untitled

a guest
Jan 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. /**
  2.  * Created by IntelliJ IDEA.
  3.  * User: jte
  4.  * Date: 10/16/11
  5.  * Time: 11:17 PM
  6.  * To change this template use File | Settings | File Templates.
  7.  */
  8.  
  9. import java.io.*;
  10. import java.util.*;
  11.  
  12. public class Pairs extends Thread {
  13.     public Pairs(int what) {
  14.         try {
  15.             if (what == 0) {
  16.                 this.input = new BufferedReader(new InputStreamReader(System.in));
  17.                 this.output = new PrintWriter(System.out);
  18.             } else {
  19.                 this.input = new BufferedReader(new FileReader(INPUT_FILE));
  20.                 this.output = new PrintWriter(OUTPUT_FILE);
  21.             }
  22.         } catch (Throwable e) {
  23.             e.printStackTrace();
  24.             System.exit(666);
  25.         }
  26.     }
  27.  
  28.     private void solve() throws Throwable {
  29.  
  30.         int []a = new int[MAX];
  31.         int []b = new int[MAX];
  32.         int []primes = new int[8];
  33.         int SIZE = 0;
  34.         for (;;) {
  35.             int n = nextInt(), X = nextInt();
  36.             if (n == 0) {
  37.                 break;
  38.             }
  39.             for (int i = 0; i < n; ++i) {
  40.                 a[i] = nextInt();
  41.             }
  42.             SIZE = 0;
  43.             int temporary = X;
  44.             for (int factor = 2; factor * factor <= X; ++factor) {
  45.                 if (temporary == 1) {
  46.                     break;
  47.                 }
  48.                 if (temporary % factor == 0) {
  49.                     primes[SIZE++] = factor;
  50.                     while (temporary % factor == 0) {
  51.                         temporary /= factor;
  52.                     }
  53.                 }
  54.             }
  55.             if (temporary > 1) {
  56.                 primes[SIZE++] = temporary;
  57.             }
  58.             int subSets = 1 << SIZE;
  59.             long result = (long)n * (long)(n - 1), bad = 0;
  60.             result >>= 1;
  61.             for (int mask = 1; mask < subSets; ++mask) {
  62.                 int MODULO = 1, cardinality = 0;
  63.                 long total = 0;
  64.                 for (int where = 0; where < SIZE; ++where) {
  65.                     if (((1 << where) & mask) != 0) {
  66.                         ++cardinality;
  67.                         MODULO *= primes[where];
  68.                     }
  69.                 }
  70.  
  71.                 for (int i = 0; i < n; ++i) {
  72.                     b[i] = a[i] % MODULO;
  73.                 }
  74.                 Arrays.sort(b, 0, n);
  75.                 int last = -1;
  76.                 long cnt = 0;
  77.                 for (int i = 0; i < n; ++i) {
  78.                     int t = b[i];
  79.                     if (t == last) {
  80.                         ++cnt;
  81.                     } else {
  82.                         last = t;
  83.                         total += cnt * (cnt - 1) / 2;
  84.                         cnt = 1;
  85.                     }
  86.                 }
  87.                 total += cnt * (cnt - 1) / 2;
  88.                 if (cardinality % 2 == 0) {
  89.                     bad -= total;
  90.                 } else {
  91.                     bad += total;
  92.                 }
  93.             }
  94.             output.println(result - bad);
  95.         }
  96.  
  97.     }
  98.  
  99.     public void run() {
  100.         try {
  101.             solve();
  102.         } catch (Throwable e) {
  103.             e.printStackTrace();
  104.             System.exit(666);
  105.         } finally {
  106.             output.flush();
  107.             output.close();
  108.         }
  109.     }
  110.  
  111.  
  112.     public static void main(String... args) {
  113.         new Pairs(1).start();
  114.     }
  115.  
  116.     private String nextToken() throws IOException {
  117.         while (tokens == null || !tokens.hasMoreTokens()) {
  118.             tokens = new StringTokenizer(input.readLine());
  119.         }
  120.         return tokens.nextToken();
  121.     }
  122.  
  123.     private int nextInt() throws IOException {
  124.         return Integer.parseInt(nextToken());
  125.     }
  126.  
  127.     private double nextDouble() throws IOException {
  128.         return Double.parseDouble(nextToken());
  129.     }
  130.  
  131.     private long nextLong() throws IOException {
  132.         return Long.parseLong(nextToken());
  133.     }
  134.     static final int MAX = 100000;
  135.     static final private String INPUT_FILE = "pairs.in";
  136.     static final private String OUTPUT_FILE = "pairs.out";
  137.     private BufferedReader input;
  138.     private PrintWriter output;
  139.     private StringTokenizer tokens = null;
  140. }
Add Comment
Please, Sign In to add comment