1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#include "dzilib.h"
#include <bits/stdc++.h>
#define ll long long
#define debug if (0)

int t, q;
ll n, c;

const int N = 1e6 + 200;
int cnt_divs[N + 5];
int sieve[N + 5];

void calc_sieve() {
  sieve[0] = sieve[1] = 1;

  for (ll i = 2; i * i <= N; i++)
    if (sieve[i] == 0) {
      sieve[i] = i;
      for (ll k = i * i; k <= N; k += i)
        if (sieve[k] == 0)
          sieve[k] = i;
    }

  for (int i = 2; i <= N; i++)
    if (sieve[i] == 0)
      sieve[i] = i;

  cnt_divs[1] = 1;
  std::vector<int> divs;
  for (int x = 2; x <= N; x++) {
    divs.clear();
    int y = x;
    while (y > 1) {
      divs.push_back(sieve[y]);
      y /= sieve[y];
    }

    int i = 0;
    int j = i;
    cnt_divs[x] = 1;

    while (i < divs.size()) {
      while (j < divs.size() - 1 && divs[j + 1] == divs[i])
        j++;
      cnt_divs[x] *= (j - i + 2);
      i = j + 1;
    }
  }
}

std::vector<int> calc_ps(std::vector<int> &A) {
  int n = A.size();
  std::vector<int> ps(n + 1, 0);
  int pref = 0;
  for (int i = 2; i <= n; i++) {
    while (pref > 0 && A[i] != A[pref + 1])
      pref = ps[pref];
    if (A[i] == A[pref + 1])
      pref++;
    ps[i] = pref;
  }
  return ps;
}

int find_match(std::vector<int> w) {
  int tt = w.size();
  w.insert(w.begin(), -1);
  w.push_back(-1);
  for (int i = 1; i <= N; i++)
    w.push_back(cnt_divs[i]);

  std::vector<int> ps = calc_ps(w);

  // match of length t
  for (int i = 0; i <= w.size(); i++)
    if (ps[i] == tt) {
      return i - 2 * tt;
    }
  return -1;
}

int main() {
  t = GetT();
  n = GetN();
  q = GetQ();
  c = GetC();

  // Comment this!
  // lib_calc_sieve();

  calc_sieve();

  while (t--) {
    // Comment this!
    // InputX();

    std::vector<int> word;
    for (int i = 0; i < 100; i++)
      word.push_back(Ask(i));
    Answer(find_match(word));
  }
}