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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
// clang-format on

#include "dzilib.h"


using lhash_t       = unsigned __int128;
using hash_t        = uint64_t;
const hash_t hash_p = 1423;
const hash_t hash_m = 9034297094444475691;
hash_t
mul(const hash_t& a, const hash_t& b)
{
    return (unsigned __int128)1 * a * b % hash_m;
}


// Global variables
const LL max_interest_sqrt = 1 << 15;
int ntests, ntotal_queries, nqueries;
LL max_x, query_limit, max_interest;
unordered_map<LL, int> tau_cache;
vector<LL> small_prime;


int get_tau(const LL& val);
void gen_small_prime();


int
main()
{
    cin.tie(0)->sync_with_stdio(0);
    ntests = GetT(), max_x = GetN();
    ntotal_queries = GetQ(), query_limit = GetC();
    nqueries     = ntotal_queries / ntests;
    max_interest = max_x + nqueries - 1;

    if (max_x <= 1000000) {
        // the smallest prime dividing x
        vector<LL> small_prime(max_interest + 1);
        small_prime[1] = 1;
        for (LL p = 3; p <= max_interest; p += 2) {
            if (small_prime[p]) continue;
            for (LL mul = p; mul <= max_interest; mul += p)
                small_prime[mul] = p;
        }

        // calculate tau for every number
        vector<int> tau(max_interest + 1, 1);
        for (LL val = 2; val <= max_interest; ++val) {
            LL x = val, last_prime = 0, exp = 1;
            x >>= __builtin_ctzll(val);
            tau[val] *= __builtin_ctzll(val) + 1;
            while (last_prime != 1) {
                const auto& p = small_prime[x];
                if (last_prime == p)
                    ++exp;
                else {
                    tau[val] *= exp;
                    last_prime = p, exp = 2;
                }
                x /= p;
            }
        }

        nqueries = 17;
        map<vector<int>, LL> seq_to_val;
        for (LL x = 1; x <= max_x; ++x)
            seq_to_val[vector<int>(tau.begin() + x, tau.begin() + x + nqueries)]
              = x;

        while (ntests--) {
            vector<int> seq(nqueries);
            REP (i, nqueries) seq[i] = Ask(i);
            debug(seq);
            Answer(seq_to_val[seq]);
        }
        return 0;
    }

    gen_small_prime();
    nqueries = min(1000, nqueries);
    while (ntests--) {
        vector<int> seq(nqueries);
        unordered_set<hash_t> ranges;
        REP (i, nqueries) seq[i] = Ask(i);
        REP (start, nqueries) {
            lhash_t h = 0;
            FOR (i, start, nqueries - 1) {
                h *= hash_p;
                h += seq[i];
                h %= hash_m;
                ranges.insert(h);
            }
        }
        debug(ssize(ranges));

        for (LL base = nqueries; base <= max_interest; base += nqueries) {
            lhash_t h = get_tau(base);
            if (!ranges.contains(h)) continue;
            LL from = base, to = base;

            // try to go further as much as its possible
            lhash_t h_pow = hash_p;
            while (to < max_interest && from + nqueries - 1 > to) {
                lhash_t next_h = (h * hash_p + get_tau(to + 1)) % hash_m;
                if (!ranges.contains(next_h)) break;
                h = next_h;
                to += 1;
                h_pow *= hash_p;
                h_pow %= hash_m;
            }

            // maybe we will have to go back
            while (from + nqueries - 1 > to) {
                lhash_t next_h = (h + h_pow * get_tau(from - 1)) % hash_m;
                if (!ranges.contains(next_h)) break;
                h = next_h;
                from -= 1;
                h_pow *= hash_p;
                h_pow %= hash_m;
            }

            // success
            if (from + nqueries - 1 == to) {
                Answer(from);
                break;
            }
        }
    }

#ifdef LOCAL
    system("grep VmPeak /proc/$PPID/status >&2");
#endif
    return 0;
}


int
get_tau(const LL& val)
{
    auto it = tau_cache.find(val);
    if (it == tau_cache.end()) {
        it   = tau_cache.insert({val, 1}).first;
        LL x = val, mul;
        x >>= __builtin_ctzll(val);
        it->second *= __builtin_ctzll(val) + 1;
        for (const auto& p : small_prime) {
            if (p * p > x) break;
            mul = 1;
            while (x % p == 0) ++mul, x /= p;
            it->second *= mul;
        }
        if (x > 1) it->second *= 2;  // one prime exponent left
    }
    return it->second;
}

void
gen_small_prime()
{
    vector<bool> sieve(max_interest_sqrt);
    small_prime.emplace_back(2);
    for (LL p = 3; p <= max_interest_sqrt; p += 2) {
        if (sieve[p]) continue;
        small_prime.emplace_back(p);
        for (LL mul = p << 1; mul * mul <= max_interest_sqrt; mul += p)
            sieve[p] = true;
    }
}