Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by IntelliJ IDEA.
- * User: jte
- * Date: 10/16/11
- * Time: 11:17 PM
- * To change this template use File | Settings | File Templates.
- */
- import java.io.*;
- import java.util.*;
- public class Pairs extends Thread {
- public Pairs(int what) {
- try {
- if (what == 0) {
- this.input = new BufferedReader(new InputStreamReader(System.in));
- this.output = new PrintWriter(System.out);
- } else {
- this.input = new BufferedReader(new FileReader(INPUT_FILE));
- this.output = new PrintWriter(OUTPUT_FILE);
- }
- } catch (Throwable e) {
- e.printStackTrace();
- System.exit(666);
- }
- }
- private void solve() throws Throwable {
- int []a = new int[MAX];
- int []b = new int[MAX];
- int []primes = new int[8];
- int SIZE = 0;
- for (;;) {
- int n = nextInt(), X = nextInt();
- if (n == 0) {
- break;
- }
- for (int i = 0; i < n; ++i) {
- a[i] = nextInt();
- }
- SIZE = 0;
- int temporary = X;
- for (int factor = 2; factor * factor <= X; ++factor) {
- if (temporary == 1) {
- break;
- }
- if (temporary % factor == 0) {
- primes[SIZE++] = factor;
- while (temporary % factor == 0) {
- temporary /= factor;
- }
- }
- }
- if (temporary > 1) {
- primes[SIZE++] = temporary;
- }
- int subSets = 1 << SIZE;
- long result = (long)n * (long)(n - 1), bad = 0;
- result >>= 1;
- for (int mask = 1; mask < subSets; ++mask) {
- int MODULO = 1, cardinality = 0;
- long total = 0;
- for (int where = 0; where < SIZE; ++where) {
- if (((1 << where) & mask) != 0) {
- ++cardinality;
- MODULO *= primes[where];
- }
- }
- for (int i = 0; i < n; ++i) {
- b[i] = a[i] % MODULO;
- }
- Arrays.sort(b, 0, n);
- int last = -1;
- long cnt = 0;
- for (int i = 0; i < n; ++i) {
- int t = b[i];
- if (t == last) {
- ++cnt;
- } else {
- last = t;
- total += cnt * (cnt - 1) / 2;
- cnt = 1;
- }
- }
- total += cnt * (cnt - 1) / 2;
- if (cardinality % 2 == 0) {
- bad -= total;
- } else {
- bad += total;
- }
- }
- output.println(result - bad);
- }
- }
- public void run() {
- try {
- solve();
- } catch (Throwable e) {
- e.printStackTrace();
- System.exit(666);
- } finally {
- output.flush();
- output.close();
- }
- }
- public static void main(String... args) {
- new Pairs(1).start();
- }
- private String nextToken() throws IOException {
- while (tokens == null || !tokens.hasMoreTokens()) {
- tokens = new StringTokenizer(input.readLine());
- }
- return tokens.nextToken();
- }
- private int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- private double nextDouble() throws IOException {
- return Double.parseDouble(nextToken());
- }
- private long nextLong() throws IOException {
- return Long.parseLong(nextToken());
- }
- static final int MAX = 100000;
- static final private String INPUT_FILE = "pairs.in";
- static final private String OUTPUT_FILE = "pairs.out";
- private BufferedReader input;
- private PrintWriter output;
- private StringTokenizer tokens = null;
- }
Add Comment
Please, Sign In to add comment